protocol-buffers-2.4.0: Parse Google Protocol Buffer specifications

Safe HaskellNone
LanguageHaskell98

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 -> WireSize Source #

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 -> ByteString Source #

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 -> Put Source #

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 msg Source #

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 -> WireSize Source #

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 -> ByteString Source #

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 -> Put Source #

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 msg Source #

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 -> WireSize Source #

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 -> Put Source #

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 ().

data Get a Source #

Instances

Monad Get Source # 

Methods

(>>=) :: Get a -> (a -> Get b) -> Get b #

(>>) :: Get a -> Get b -> Get b #

return :: a -> Get a #

fail :: String -> Get a #

Functor Get Source # 

Methods

fmap :: (a -> b) -> Get a -> Get b #

(<$) :: a -> Get b -> Get a #

Applicative Get Source # 

Methods

pure :: a -> Get a #

(<*>) :: Get (a -> b) -> Get a -> Get b #

(*>) :: Get a -> Get b -> Get b #

(<*) :: Get a -> Get b -> Get a #

Alternative Get Source # 

Methods

empty :: Get a #

(<|>) :: Get a -> Get a -> Get a #

some :: Get a -> Get [a] #

many :: Get a -> Get [a] #

MonadPlus Get Source # 

Methods

mzero :: Get a #

mplus :: Get a -> Get a -> Get a #

MonadError String Get Source # 

Methods

throwError :: String -> Get a #

catchError :: Get a -> (String -> Get a) -> Get a #

runPut :: Put -> ByteString #

Run the Put monad with a serialiser

runGet :: Get a -> ByteString -> Result a Source #

runGet is the simple executor

getFromBS :: Get r -> ByteString -> r Source #

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 where Source #

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.

Minimal complete definition

wireSize, wirePut, wireGet

Instances

Wire Bool Source # 
Wire Double Source # 
Wire Float Source # 
Wire Int Source # 
Wire Int32 Source # 
Wire Int64 Source # 
Wire Word32 Source # 
Wire Word64 Source # 
Wire ByteString Source # 
Wire Utf8 Source # 

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

prependMessageSize :: WireSize -> WireSize Source #

Used in generated code.

putSize :: WireSize -> Put Source #

Used in generated code.

putVarUInt :: (Integral a, Bits a) => a -> Put Source #

getVarInt :: (Show a, Integral a, Bits a) => Get a Source #

putLazyByteString :: ByteString -> Put #

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

wireSizeReq :: Wire v => Int64 -> FieldType -> v -> Int64 Source #

Used in generated code.

wireSizeOpt :: Wire v => Int64 -> FieldType -> Maybe v -> Int64 Source #

Used in generated code.

wireSizeRep :: Wire v => Int64 -> FieldType -> Seq v -> Int64 Source #

Used in generated code.

wireSizePacked :: Wire v => Int64 -> FieldType -> Seq v -> Int64 Source #

Used in generated code.

wirePutReq :: Wire v => WireTag -> FieldType -> v -> Put Source #

Used in generated code.

wirePutOpt :: Wire v => WireTag -> FieldType -> Maybe v -> Put Source #

Used in generated code.

wirePutRep :: Wire v => WireTag -> FieldType -> Seq v -> Put Source #

Used in generated code.

wirePutPacked :: Wire v => WireTag -> FieldType -> Seq v -> Put Source #

Used in generated code.

getMessageWith :: (Default message, ReflectDescriptor message) => (WireTag -> message -> Get message) -> Get message Source #

getBareMessageWith :: (Default message, ReflectDescriptor message) => (WireTag -> message -> Get message) -> Get message Source #

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 e Source #

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

wireGetFromWire :: FieldId -> WireType -> Get ByteString Source #

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