Last active
July 21, 2016 13:04
-
-
Save creichert/b220ba604d85b4e84b44 to your computer and use it in GitHub Desktop.
Google Protobuf Example in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE DataKinds #-} | |
import Data.Int | |
import Data.ProtocolBuffers | |
import Data.Text | |
import GHC.Generics (Generic) | |
import GHC.TypeLits | |
import Data.Serialize | |
import Data.Monoid | |
import Data.Hex | |
data Foo = Foo | |
{ | |
field1 :: Required 1 (Value Int64) -- ^ The last field with tag = 1 | |
, field2 :: Optional 2 (Value Text) -- ^ The last field with tag = 2 | |
, field3 :: Repeated 3 (Value Bool) -- ^ All fields with tag = 3, ordering is preserved | |
} deriving (Generic, Show) | |
instance Encode Foo | |
instance Decode Foo | |
main = do | |
let msg = Foo{field1 = putField 42, field2 = mempty, field3 = putField [True, False]} | |
let encoded = fmap hex runPut $ encodeMessage msg | |
print encoded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment