thrift_runtime_protocol behaviour (thrift_runtime v0.7.0)
View SourceWire protocol behaviour: value and message codecs as pure functions.
A protocol module encodes and decodes values described by the same
type descriptors the generated introspection functions return
(struct_info/1, enum_info/1, function_info/2). Decoding consumes
a binary and returns the remaining bytes, so callers can stream;
more marks a truncated but otherwise valid input.
thrift_runtime_binary and thrift_runtime_compact implement this behaviour.
Any other module passed as a thrift_runtime:protocol() — to the facade,
the client or the server — must export every callback with these exact
shapes.
The module also hosts what the implementations share:
- the descriptor type vocabulary (
type_desc(),field_desc(),message_type(), ...): the behaviour owns the contract, so the callbacks are typed through it;thrift_runtime_binaryre-exports the names for the existing call sites; - the
TMessageTypeids of the message envelope and theTApplicationExceptiondescriptor with its code-to-name table, plus the shared encode/decode of that exception payload; - cold-path helpers the three implementations would otherwise carry
three copies of: enum member conversion, the encode-time union and
required-field checks with the decode-time required marking, the
message-type table, struct resolution through
struct_info/1with the schema cache, the specialized-codec probe of the generated per-protocol index, and the per-process memo over inline descriptors.
Hot-path pieces stay in the implementations on purpose: the per-type codecs, the skip functions, varint/zigzag and the per-field/per-scalar helpers work on wire shapes only they understand.
Summary
Callbacks
Decodes one value of Type from the front of Binary; the rest comes
back, more marks a valid but truncated input.
Decodes a TApplicationException payload.
Decodes a message envelope from the front of Binary; more marks a
truncated one.
Encodes Value as Type, or fails with a tagged error.
Encodes a TApplicationException payload.
Wraps an already-encoded payload into a message envelope.
Types
-type application_exception() :: #{message := binary(), code := non_neg_integer(), name := binary()}.
-type base_type() :: bool | i8 | i16 | i32 | i64 | double | string | binary | uuid.
-type field_desc() :: {Id :: integer(), requiredness(), type_desc(), Name :: binary(), Default :: term()}.
-type message_type() :: call | reply | exception | oneway.
-type protocol() :: binary | compact | json.
-type requiredness() :: required | optional | default.
-type set_impl() :: list | sets | ordset.
-type struct_desc() :: {struct | union | exception, [field_desc()]} | {ref, module(), binary()}.
Callbacks
-callback decode(type_desc(), binary()) -> decode_result(term()).
Decodes one value of Type from the front of Binary; the rest comes
back, more marks a valid but truncated input.
-callback decode_application_exception(binary()) -> decode_result(application_exception()).
Decodes a TApplicationException payload.
-callback decode_message(binary()) -> {ok, message_type(), binary(), non_neg_integer(), binary()} | more | {error, term()}.
Decodes a message envelope from the front of Binary; more marks a
truncated one.
Encodes Value as Type, or fails with a tagged error.
-callback encode_application_exception(Message :: binary(), Code :: non_neg_integer()) -> {ok, iodata()} | {error, term()}.
Encodes a TApplicationException payload.
-callback encode_message(message_type(), Name :: binary(), SeqId :: non_neg_integer(), Payload :: iodata()) -> iodata().
Wraps an already-encoded payload into a message envelope.
Functions
-spec all_present([binary()], struct_value()) -> boolean().
-spec application_exception_desc() -> struct_desc().
-spec application_exception_name(non_neg_integer()) -> binary().
-spec decode_application_exception(module(), binary()) -> decode_result(application_exception()).
-spec encode_application_exception(module(), binary(), non_neg_integer()) -> {ok, iodata()} | {error, term()}.
-spec memo_compiled(module(), term(), struct | union | exception, [field_desc()], fun((struct | union | exception, [field_desc()]) -> Compiled)) -> Compiled.
-spec message_type(non_neg_integer()) -> {ok, message_type()} | error.
-spec message_type_id(message_type()) -> non_neg_integer().
-spec resolve_struct(module(), module(), binary(), fun((struct | union | exception, [field_desc()]) -> Compiled)) -> {ok, Compiled} | error.
-spec struct_fields(type_desc()) -> [field_desc()].