alphaHeavy / protobuf

An implementation of Google's Protocol Buffers in Haskell.
http://hackage.haskell.org/package/protobuf
BSD 3-Clause "New" or "Revised" License
96 stars 18 forks source link

nested objects #14

Closed mxswd closed 9 years ago

mxswd commented 9 years ago

Is it possible to nest objects, something like this:

{-# LANGUAGE DeriveGeneric, DataKinds #-}
module Main where

import Data.Int
import Data.ProtocolBuffers
import Data.Text
import GHC.Generics (Generic)
import GHC.TypeLits

data Name = Name
  { firstname :: Optional 1 (Value Text)
  , lastname :: Optional 2 (Value Text)
  } deriving (Generic, Show)

instance Encode Name
instance Decode Name

data Person = Person
  { name :: Required 1 (Value Name)
  , id :: Required 2 (Value Int32)
  , email :: Optional 3 (Value Text)
  } deriving (Generic, Show)

instance Encode Person
instance Decode Person

I get an error trying to do this.

If not, one day you may want to consider adding it.

jacobstanley commented 9 years ago

You need to use Message instead of Value:

data Person = Person
  { name :: Required 1 (Message Name)
  , id :: Required 2 (Value Int32)
  , email :: Optional 3 (Value Text)
  } deriving (Generic, Show)
NathanHowell commented 9 years ago

Absolutely. The trick is picking the wrapper to encode messages, which are treated a bit differently (on the wire) from primitives and enums. Just choose Message instead of Value or Enumeration.. as @jystic just pointed out :+1:

mxswd commented 9 years ago

Awesome thanks! Works great.