FakeRiak.Protobuf (fake_riak v0.2.2)

Copy Markdown View Source

Minimal protobuf (proto2) wire-format codec, just enough for the Riak PBC messages FakeRiak.RiakPBC speaks.

Only the wire types Riak's KV messages use are decoded: varint (0) and length-delimited (2). Unknown fixed64 (1) and fixed32 (5) fields are skipped; the long-deprecated group types (3/4) are treated as malformed. Field types are not schema-checked — a decoded message is just a map of field number to the values seen, in order, and the caller picks out the fields it knows.

Summary

Types

A decoded field value: a varint as an integer, or raw bytes.

Field number => values in order of appearance.

Functions

Decode a message payload into its fields.

Encode fields, in the given order, into a message payload. Integers and booleans go as {:varint, n}; bytes, strings and embedded messages as {:bytes, iodata}.

The value of a field, with proto2 scalar semantics: when a scalar field appears more than once, the last occurrence wins.

Types

field_value()

@type field_value() :: non_neg_integer() | binary()

A decoded field value: a varint as an integer, or raw bytes.

fields()

@type fields() :: %{optional(pos_integer()) => [field_value()]}

Field number => values in order of appearance.

Functions

decode(payload)

@spec decode(binary()) :: {:ok, fields()} | :error

Decode a message payload into its fields.

Returns :error on malformed input: a truncated field, an over-long varint, a group wire type, or field number 0.

encode(fields)

@spec encode([{pos_integer(), {:varint, non_neg_integer()} | {:bytes, iodata()}}]) ::
  iodata()

Encode fields, in the given order, into a message payload. Integers and booleans go as {:varint, n}; bytes, strings and embedded messages as {:bytes, iodata}.

last(fields, field, default \\ nil)

@spec last(fields(), pos_integer(), field_value() | nil) :: field_value() | nil

The value of a field, with proto2 scalar semantics: when a scalar field appears more than once, the last occurrence wins.