Skip to content

Instantly share code, notes, and snippets.

@creichert
Last active July 21, 2016 13:04
Show Gist options
  • Save creichert/b220ba604d85b4e84b44 to your computer and use it in GitHub Desktop.
Save creichert/b220ba604d85b4e84b44 to your computer and use it in GitHub Desktop.
Google Protobuf Example in Haskell
{-# 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