{-# LANGUAGE BangPatterns, DeriveDataTypeable, DeriveGeneric,
  FlexibleInstances, MultiParamTypeClasses #-}

module Network.Monitoring.Riemann.Proto.Attribute
  ( Attribute(..)
  ) where

import qualified Data.Data as Prelude'
import qualified GHC.Generics as Prelude'
import Prelude ((+))
import qualified Prelude as Prelude'
import qualified Text.ProtocolBuffers.Header as P'

data Attribute = Attribute
  { key :: !P'.Utf8
  , value :: !(P'.Maybe P'.Utf8)
  } deriving ( Prelude'.Show
             , Prelude'.Eq
             , Prelude'.Ord
             , Prelude'.Typeable
             , Prelude'.Data
             , Prelude'.Generic
             )

instance P'.Mergeable Attribute where
  mergeAppend (Attribute x'1 x'2) (Attribute y'1 y'2) =
    Attribute (P'.mergeAppend x'1 y'1) (P'.mergeAppend x'2 y'2)

instance P'.Default Attribute where
  defaultValue = Attribute P'.defaultValue P'.defaultValue

instance P'.Wire Attribute where
  wireSize ft' self'@(Attribute x'1 x'2) =
    case ft' of
      10 -> calc'Size
      11 -> P'.prependMessageSize calc'Size
      _ -> P'.wireSizeErr ft' self'
    where
      calc'Size = P'.wireSizeReq 1 9 x'1 + P'.wireSizeOpt 1 9 x'2
  wirePut ft' self'@(Attribute x'1 x'2) =
    case ft' of
      10 -> put'Fields
      11 -> do
        P'.putSize (P'.wireSize 10 self')
        put'Fields
      _ -> P'.wirePutErr ft' self'
    where
      put'Fields = do
        P'.wirePutReq 10 9 x'1
        P'.wirePutOpt 18 9 x'2
  wireGet ft' =
    case ft' of
      10 -> P'.getBareMessageWith update'Self
      11 -> P'.getMessageWith update'Self
      _ -> P'.wireGetErr ft'
    where
      update'Self wire'Tag old'Self =
        case wire'Tag of
          10 ->
            Prelude'.fmap
              (\ !new'Field -> old'Self {key = new'Field})
              (P'.wireGet 9)
          18 ->
            Prelude'.fmap
              (\ !new'Field -> old'Self {value = Prelude'.Just new'Field})
              (P'.wireGet 9)
          _ ->
            let (field'Number, wire'Type) = P'.splitWireTag wire'Tag
             in P'.unknown field'Number wire'Type old'Self

instance P'.MessageAPI msg' (msg' -> Attribute) Attribute where
  getVal m' f' = f' m'

instance P'.GPB Attribute

instance P'.ReflectDescriptor Attribute where
  getMessageInfo _ =
    P'.GetMessageInfo
      (P'.fromDistinctAscList [10])
      (P'.fromDistinctAscList [10, 18])
  reflectDescriptorInfo _ =
    Prelude'.read
      "DescriptorInfo {descName = ProtoName {protobufName = FIName \".Proto.Attribute\", haskellPrefix = [MName \"Network\",MName \"Monitoring\",MName \"Riemann\"], parentModule = [MName \"Proto\"], baseName = MName \"Attribute\"}, descFilePath = [\"Network\",\"Monitoring\",\"Riemann\",\"Proto\",\"Attribute.hs\"], isGroup = False, fields = fromList [FieldInfo {fieldName = ProtoFName {protobufName' = FIName \".Proto.Attribute.key\", haskellPrefix' = [MName \"Network\",MName \"Monitoring\",MName \"Riemann\"], parentModule' = [MName \"Proto\",MName \"Attribute\"], baseName' = FName \"key\", baseNamePrefix' = \"\"}, fieldNumber = FieldId {getFieldId = 1}, wireTag = WireTag {getWireTag = 10}, packedTag = Nothing, wireTagLength = 1, isPacked = False, isRequired = True, canRepeat = False, mightPack = False, typeCode = FieldType {getFieldType = 9}, typeName = Nothing, hsRawDefault = Nothing, hsDefault = Nothing},FieldInfo {fieldName = ProtoFName {protobufName' = FIName \".Proto.Attribute.value\", haskellPrefix' = [MName \"Network\",MName \"Monitoring\",MName \"Riemann\"], parentModule' = [MName \"Proto\",MName \"Attribute\"], baseName' = FName \"value\", baseNamePrefix' = \"\"}, fieldNumber = FieldId {getFieldId = 2}, wireTag = WireTag {getWireTag = 18}, packedTag = Nothing, wireTagLength = 1, isPacked = False, isRequired = False, canRepeat = False, mightPack = False, typeCode = FieldType {getFieldType = 9}, typeName = Nothing, hsRawDefault = Nothing, hsDefault = Nothing}], descOneofs = fromList [], keys = fromList [], extRanges = [], knownKeys = fromList [], storeUnknown = False, lazyFields = False, makeLenses = False}"

instance P'.TextType Attribute where
  tellT = P'.tellSubMessage
  getT = P'.getSubMessage

instance P'.TextMsg Attribute where
  textPut msg = do
    P'.tellT "key" (key msg)
    P'.tellT "value" (value msg)
  textGet = do
    mods <- P'.sepEndBy (P'.choice [parse'key, parse'value]) P'.spaces
    Prelude'.return (Prelude'.foldl (\v f -> f v) P'.defaultValue mods)
    where
      parse'key =
        P'.try
          (do v <- P'.getT "key"
              Prelude'.return (\o -> o {key = v}))
      parse'value =
        P'.try
          (do v <- P'.getT "value"
              Prelude'.return (\o -> o {value = v}))