protocol-buffers-1.8.0: Parse Google Protocol Buffer specifications

Text.ProtocolBuffers.WireMessage

Contents

Description

Here are the serialization and deserialization functions.

This module cooperates with the generated code to implement the Wire instances. The encoding is mostly documented at http://code.google.com/apis/protocolbuffers/docs/encoding.html.

The user API functions are grouped into sections and documented. The rest are for internal use. The main functions are messageGet and messagePut (and messageSize). There are then several 'message*' variants which allow for finer control and for making delimited messages.

Synopsis

User API functions

Main encoding and decoding operations (non-delimited message encoding)

messageSize :: (ReflectDescriptor msg, Wire msg) => msg -> WireSizeSource

This computes the size of the message's fields with tags on the wire with no initial tag or length (in bytes). This is also the length of the message as placed between group start and stop tags.

messagePut :: (ReflectDescriptor msg, Wire msg) => msg -> ByteStringSource

This is runPut applied to messagePutM. It result in a ByteString with a length of messageSize bytes.

messageGet :: (ReflectDescriptor msg, Wire msg) => ByteString -> Either String (msg, ByteString)Source

This consumes the ByteString to decode a message. It assumes the ByteString is merely a sequence of the tagged fields of the message, and consumes until a group stop tag is detected or the entire input is consumed. Any ByteString past the end of the stop tag is returned as well.

This is runGetOnLazy applied to messageGetM.

messagePutM :: (ReflectDescriptor msg, Wire msg) => msg -> PutSource

This writes just the message's fields with tags to the wire. This Put monad can be composed and eventually executed with runPut.

This is actually wirePut 10 msg

messageGetM :: (ReflectDescriptor msg, Wire msg) => Get msgSource

This reads the tagged message fields until the stop tag or the end of input is reached.

This is actually wireGet 10 msg

These should agree with the length delimited message format of protobuf-2.10, where the message size preceeds the data.

messageWithLengthSize :: (ReflectDescriptor msg, Wire msg) => msg -> WireSizeSource

This computes the size of the message fields as in messageSize and add the length of the encoded size to the total. Thus this is the the length of the message including the encoded length header, but without any leading tag.

messageWithLengthPut :: (ReflectDescriptor msg, Wire msg) => msg -> ByteStringSource

This is runPut applied to messageWithLengthPutM. It results in a ByteString with a length of messageWithLengthSize bytes.

messageWithLengthGet :: (ReflectDescriptor msg, Wire msg) => ByteString -> Either String (msg, ByteString)Source

This runGetOnLazy applied to messageWithLengthGetM.

This first reads the encoded length of the message and will then succeed when it has consumed precisely this many additional bytes. The ByteString after this point will be returned.

messageWithLengthPutM :: (ReflectDescriptor msg, Wire msg) => msg -> PutSource

This writes the encoded length of the message's fields and then the message's fields with tags to the wire. This Put monad can be composed and eventually executed with runPut.

This is actually wirePut 11 msg

messageWithLengthGetM :: (ReflectDescriptor msg, Wire msg) => Get msgSource

This reads the encoded message length and then the message.

This is actually wireGet 11 msg

Encoding to write or read a single message field (good for delimited messages or incremental use)

messageAsFieldSize :: (ReflectDescriptor msg, Wire msg) => FieldId -> msg -> WireSizeSource

This computes the size of the messageWithLengthSize and then adds the length an initial tag with the given FieldId.

messageAsFieldPutM :: (ReflectDescriptor msg, Wire msg) => FieldId -> msg -> PutSource

This writes an encoded wire tag with the given FieldId and then the encoded length of the message's fields and then the message's fields with tags to the wire. This Put monad can be composed and eventually executed with runPut.

messageAsFieldGetM :: (ReflectDescriptor msg, Wire msg) => Get (FieldId, msg)Source

This reads a wire tag (must be of type '2') to get the FieldId. Then the encoded message length is read, followed by the message itself. Both the FieldId and the message are returned.

This allows for incremental reading and processing.

The Put monad from the binary package, and a custom binary Get monad (Text.ProtocolBuffers.Get)

type Put = PutM ()

Put merely lifts Builder into a Writer monad, applied to ().

runPut :: Put -> ByteString

Run the Put monad with a serialiser

runGet :: Get a -> ByteString -> Result aSource

runGet is the simple executor

getFromBS :: Get r -> ByteString -> rSource

This is runGetOnLazy with the Left results converted to error calls and the trailing ByteString discarded. This use of runtime errors is discouraged, but may be convenient.

The Wire monad itself. Users should beware that passing an incompatible FieldType is a runtime error or fail

class Wire b whereSource

The Wire class is for internal use, and may change. If there is a mis-match between the FieldType and the type of b then you will get a failure at runtime.

Users should stick to the message functions defined in Text.ProtocolBuffers.WireMessage and exported to use user by Text.ProtocolBuffers. These are less likely to change.

The internal exports, for use by generated code and the Text.ProtcolBuffer.Extensions module

prependMessageSize :: WireSize -> WireSizeSource

Used in generated code.

putSize :: WireSize -> PutSource

Used in generated code.

putVarUInt :: (Integral a, Bits a) => a -> PutSource

putLazyByteString :: ByteString -> Put

Write a lazy ByteString efficiently, simply appending the lazy ByteString chunks to the output buffer

wireSizeReq :: Wire v => Int64 -> FieldType -> v -> Int64Source

Used in generated code.

wireSizeOpt :: Wire v => Int64 -> FieldType -> Maybe v -> Int64Source

Used in generated code.

wireSizeRep :: Wire v => Int64 -> FieldType -> Seq v -> Int64Source

Used in generated code.

wireSizePacked :: Wire v => Int64 -> FieldType -> Seq v -> Int64Source

Used in generated code.

wirePutReq :: Wire v => WireTag -> FieldType -> v -> PutSource

Used in generated code.

wirePutOpt :: Wire v => WireTag -> FieldType -> Maybe v -> PutSource

Used in generated code.

wirePutRep :: Wire v => WireTag -> FieldType -> Seq v -> PutSource

Used in generated code.

wirePutPacked :: Wire v => WireTag -> FieldType -> Seq v -> PutSource

Used in generated code.

getMessageWith :: (Mergeable message, ReflectDescriptor message) => (WireTag -> message -> Get message) -> Get messageSource

getBareMessageWith :: (Mergeable message, ReflectDescriptor message) => (WireTag -> message -> Get message) -> Get messageSource

Used by generated code getBareMessageWith assumes the wireTag for the message, if it existed, has already been read. getBareMessageWith assumes that it does needs to read the Varint encoded length of the message. getBareMessageWith will consume the entire ByteString it is operating on, or until it finds any STOP_GROUP tag (wireType == 4)

wireGetEnum :: (Typeable e, Enum e) => (Int -> Maybe e) -> Get eSource

wireGetPackedEnum :: (Typeable e, Enum e) => (Int -> Maybe e) -> Get (Seq e)Source

wireGetFromWire :: FieldId -> WireType -> Get ByteStringSource

This reads in the raw bytestring corresponding to an field known only through the wiretag's FieldId and WireType.