{-# LANGUAGE RecordWildCards #-}
module Proto3.Wire.Tutorial where
import Data.ByteString ( ByteString )
import Data.Text.Lazy ( Text )
import Data.Word ( Word64 )
import Proto3.Wire
import qualified Proto3.Wire.Encode as Encode
import qualified Proto3.Wire.Decode as Decode
data EchoRequest = EchoRequest { EchoRequest -> Text
echoRequestMessage :: Text }
encodeEchoRequest :: EchoRequest -> Encode.MessageBuilder
encodeEchoRequest :: EchoRequest -> MessageBuilder
encodeEchoRequest EchoRequest{Text
echoRequestMessage :: EchoRequest -> Text
echoRequestMessage :: Text
..} =
FieldNumber -> Text -> MessageBuilder
Encode.text FieldNumber
1 Text
echoRequestMessage
decodeEchoRequest :: ByteString -> Either Decode.ParseError EchoRequest
decodeEchoRequest :: ByteString -> Either ParseError EchoRequest
decodeEchoRequest = Parser RawMessage EchoRequest
-> ByteString -> Either ParseError EchoRequest
forall a. Parser RawMessage a -> ByteString -> Either ParseError a
Decode.parse Parser RawMessage EchoRequest
echoRequestParser
echoRequestParser :: Decode.Parser Decode.RawMessage EchoRequest
echoRequestParser :: Parser RawMessage EchoRequest
echoRequestParser = Text -> EchoRequest
EchoRequest (Text -> EchoRequest)
-> Parser RawMessage Text -> Parser RawMessage EchoRequest
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser RawPrimitive Text -> Text -> Parser RawField Text
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Text
Decode.text Text
forall a. Monoid a => a
mempty Parser RawField Text -> FieldNumber -> Parser RawMessage Text
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
1)
data EchoResponse = EchoResponse { EchoResponse -> Text
echoResponseMessage :: Text
, EchoResponse -> Word64
echoResponseTimestamp :: Word64
}
encodedEchoResponse :: EchoResponse -> Encode.MessageBuilder
encodedEchoResponse :: EchoResponse -> MessageBuilder
encodedEchoResponse EchoResponse{Word64
Text
echoResponseMessage :: EchoResponse -> Text
echoResponseTimestamp :: EchoResponse -> Word64
echoResponseMessage :: Text
echoResponseTimestamp :: Word64
..} =
FieldNumber -> Text -> MessageBuilder
Encode.text FieldNumber
1 Text
echoResponseMessage MessageBuilder -> MessageBuilder -> MessageBuilder
forall a. Semigroup a => a -> a -> a
<>
FieldNumber -> Word64 -> MessageBuilder
Encode.uint64 FieldNumber
2 Word64
echoResponseTimestamp
decodeEchoResponse :: ByteString -> Either Decode.ParseError EchoResponse
decodeEchoResponse :: ByteString -> Either ParseError EchoResponse
decodeEchoResponse = Parser RawMessage EchoResponse
-> ByteString -> Either ParseError EchoResponse
forall a. Parser RawMessage a -> ByteString -> Either ParseError a
Decode.parse Parser RawMessage EchoResponse
echoResponseParser
echoResponseParser :: Decode.Parser Decode.RawMessage EchoResponse
echoResponseParser :: Parser RawMessage EchoResponse
echoResponseParser = Text -> Word64 -> EchoResponse
EchoResponse (Text -> Word64 -> EchoResponse)
-> Parser RawMessage Text
-> Parser RawMessage (Word64 -> EchoResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser RawPrimitive Text -> Text -> Parser RawField Text
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Text
Decode.text Text
forall a. Monoid a => a
mempty Parser RawField Text -> FieldNumber -> Parser RawMessage Text
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
1)
Parser RawMessage (Word64 -> EchoResponse)
-> Parser RawMessage Word64 -> Parser RawMessage EchoResponse
forall a b.
Parser RawMessage (a -> b)
-> Parser RawMessage a -> Parser RawMessage b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser RawPrimitive Word64 -> Word64 -> Parser RawField Word64
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word64
Decode.uint64 Word64
0 Parser RawField Word64 -> FieldNumber -> Parser RawMessage Word64
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
2)
data EchoManyRequest = EchoManyRequest { EchoManyRequest -> [EchoRequest]
echoManyRequestRequests :: [EchoRequest]
}
encodeEchoManyRequest :: EchoManyRequest -> Encode.MessageBuilder
encodeEchoManyRequest :: EchoManyRequest -> MessageBuilder
encodeEchoManyRequest = (EchoRequest -> MessageBuilder) -> [EchoRequest] -> MessageBuilder
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (FieldNumber -> MessageBuilder -> MessageBuilder
Encode.embedded FieldNumber
1 (MessageBuilder -> MessageBuilder)
-> (EchoRequest -> MessageBuilder) -> EchoRequest -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
EchoRequest -> MessageBuilder
encodeEchoRequest) ([EchoRequest] -> MessageBuilder)
-> (EchoManyRequest -> [EchoRequest])
-> EchoManyRequest
-> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
EchoManyRequest -> [EchoRequest]
echoManyRequestRequests
decodeEchoManyRequest :: ByteString -> Either Decode.ParseError EchoManyRequest
decodeEchoManyRequest :: ByteString -> Either ParseError EchoManyRequest
decodeEchoManyRequest = Parser RawMessage EchoManyRequest
-> ByteString -> Either ParseError EchoManyRequest
forall a. Parser RawMessage a -> ByteString -> Either ParseError a
Decode.parse Parser RawMessage EchoManyRequest
echoManyRequestParser
echoManyRequestParser :: Decode.Parser Decode.RawMessage EchoManyRequest
echoManyRequestParser :: Parser RawMessage EchoManyRequest
echoManyRequestParser = [EchoRequest] -> EchoManyRequest
EchoManyRequest ([EchoRequest] -> EchoManyRequest)
-> Parser RawMessage [EchoRequest]
-> Parser RawMessage EchoManyRequest
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser RawPrimitive EchoRequest -> Parser RawField [EchoRequest]
forall a. Parser RawPrimitive a -> Parser RawField [a]
repeated (Parser RawMessage EchoRequest -> Parser RawPrimitive EchoRequest
forall a. Parser RawMessage a -> Parser RawPrimitive a
Decode.embedded' Parser RawMessage EchoRequest
echoRequestParser) Parser RawField [EchoRequest]
-> FieldNumber -> Parser RawMessage [EchoRequest]
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
`at` FieldNumber
1)