Safe Haskell | None |
---|---|
Language | Haskell98 |
Datatypes for reflection of protocol buffer messages.
- class Default msg => Message msg where
- newtype Tag = Tag {}
- data MessageDescriptor msg = MessageDescriptor {
- messageName :: Text
- fieldsByTag :: Map Tag (FieldDescriptor msg)
- fieldsByTextFormatName :: Map String (FieldDescriptor msg)
- data FieldDescriptor msg where
- FieldDescriptor :: String -> FieldTypeDescriptor value -> FieldAccessor msg value -> FieldDescriptor msg
- fieldDescriptorName :: FieldDescriptor msg -> String
- isRequired :: FieldDescriptor msg -> Bool
- data FieldAccessor msg value where
- PlainField :: WireDefault value -> Lens' msg value -> FieldAccessor msg value
- OptionalField :: Lens' msg (Maybe value) -> FieldAccessor msg value
- RepeatedField :: Packing -> Lens' msg [value] -> FieldAccessor msg value
- MapField :: (Ord key, Message entry) => Lens' entry key -> Lens' entry value -> Lens' msg (Map key value) -> FieldAccessor msg entry
- data WireDefault value where
- Required :: WireDefault value
- Optional :: (FieldDefault value, Eq value) => WireDefault value
- data Packing
- data FieldTypeDescriptor value where
- MessageField :: Message value => FieldTypeDescriptor value
- GroupField :: Message value => FieldTypeDescriptor value
- EnumField :: MessageEnum value => FieldTypeDescriptor value
- Int32Field :: FieldTypeDescriptor Int32
- Int64Field :: FieldTypeDescriptor Int64
- UInt32Field :: FieldTypeDescriptor Word32
- UInt64Field :: FieldTypeDescriptor Word64
- SInt32Field :: FieldTypeDescriptor Int32
- SInt64Field :: FieldTypeDescriptor Int64
- Fixed32Field :: FieldTypeDescriptor Word32
- Fixed64Field :: FieldTypeDescriptor Word64
- SFixed32Field :: FieldTypeDescriptor Int32
- SFixed64Field :: FieldTypeDescriptor Int64
- FloatField :: FieldTypeDescriptor Float
- DoubleField :: FieldTypeDescriptor Double
- BoolField :: FieldTypeDescriptor Bool
- StringField :: FieldTypeDescriptor Text
- BytesField :: FieldTypeDescriptor ByteString
- class FieldDefault value where
- class (Enum a, Bounded a) => MessageEnum a where
- class Default a where
- build :: Default a => (a -> a) -> a
- maybeLens :: b -> Lens' (Maybe b) b
- reverseRepeatedFields :: Map k (FieldDescriptor msg) -> msg -> msg
Reflection of Messages
class Default msg => Message msg where Source #
Every protocol buffer is an instance of Message
. This class enables
serialization by providing reflection of all of the fields that may be used
by this type.
descriptor :: MessageDescriptor msg Source #
A tag that identifies a particular field of the message when converting to/from the wire format.
data MessageDescriptor msg Source #
The description of a particular protocol buffer message type.
MessageDescriptor | |
|
data FieldDescriptor msg where Source #
A description of a specific field of a protocol buffer.
The String
parameter is the name of the field from the .proto file,
as used in TextFormat, with the same behavior for groups as
fieldsByTextFormatName
.
(Haddock doesn't support per-argument docs for GADTs.)
FieldDescriptor :: String -> FieldTypeDescriptor value -> FieldAccessor msg value -> FieldDescriptor msg |
fieldDescriptorName :: FieldDescriptor msg -> String Source #
The original name of the field in the .proto file.
isRequired :: FieldDescriptor msg -> Bool Source #
Whether the given field is required. Specifically, if its FieldAccessor
is a Required
PlainField
.
data FieldAccessor msg value where Source #
A Lens for accessing the value of an individual field in a protocol buffer message.
PlainField :: WireDefault value -> Lens' msg value -> FieldAccessor msg value | |
OptionalField :: Lens' msg (Maybe value) -> FieldAccessor msg value | |
RepeatedField :: Packing -> Lens' msg [value] -> FieldAccessor msg value | |
MapField :: (Ord key, Message entry) => Lens' entry key -> Lens' entry value -> Lens' msg (Map key value) -> FieldAccessor msg entry |
data WireDefault value where Source #
The default value (if any) for a PlainField
on the wire.
Required :: WireDefault value | |
Optional :: (FieldDefault value, Eq value) => WireDefault value |
data FieldTypeDescriptor value where Source #
A description of the type of a given field value.
Show (FieldTypeDescriptor value) Source # | |
class FieldDefault value where Source #
A proto3 field type with an implicit default value.
This is distinct from Default
to avoid orphan instances, and because
Bool
doesn't necessarily have a good Default instance for general usage.
fieldDefault :: value Source #
class (Enum a, Bounded a) => MessageEnum a where Source #
A class for protocol buffer enums that enables safe decoding.
Building protocol buffers
A class for types with a default value.
build :: Default a => (a -> a) -> a Source #
Utility function for building a message from a default value. For example:
instance Default A where ... x, y :: Lens' A Int m :: A m = build ((x .~ 5) . (y .~ 7))
Utilities for constructing protocol buffer lenses
maybeLens :: b -> Lens' (Maybe b) b Source #
A helper lens for accessing optional fields. This is used as part of code generation, and should generally not be needed explicitly.
Note that maybeLens
does not satisfy the lens laws, which expect that set
l (view l x) == x
. For example,
set (maybeLens 'a') (view (maybeLens 'a') Nothing) == Just 'a'
However, this is the behavior generally expected by users, and only matters if we're explicitly checking whether a field is set.
Internal utilities for parsing protocol buffers
reverseRepeatedFields :: Map k (FieldDescriptor msg) -> msg -> msg Source #
Reverse every repeated (list) field in the message.
During parsing, we store fields temporarily in reverse order, and then un-reverse them at the end. This helps avoid the quadratic blowup from repeatedly appending to lists. TODO: Benchmark how much of a problem this is in practice, and whether it's still a net win for small protobufs. If we decide on it more permanently, consider moving it to a more internal module.