livery_grpc_codec (livery_grpc v0.1.0)

View Source

Protobuf encode/decode glue over a gpb-generated module.

A gpb module (e.g. helloworld_pb, built from proto/helloworld.proto) exposes encode_msg/2 and decode_msg/2. This module wraps them behind a stable interface so the server and client do not call gpb directly, and so an alternative codec (for example JSON for application/grpc+json) can be slotted in later behind the same calls.

Encoding errors are surfaced as {error, {encode|decode, Reason}} rather than raised, so the caller can turn them into a gRPC internal status.

Summary

Functions

Recognised gRPC content-type values for proto framing.

The default gRPC content type.

Decode protobuf bytes into a message map (gpb maps mode).

Encode a message map/record to protobuf bytes.

Whether a content-type header value names a gRPC proto request. A trailing parameter (e.g. ;charset=...) is tolerated; the base type is matched.

Types

msg_name()

-type msg_name() :: atom().

proto_module()

-type proto_module() :: module().

Functions

content_subtypes()

-spec content_subtypes() -> [binary()].

Recognised gRPC content-type values for proto framing.

content_type()

-spec content_type() -> binary().

The default gRPC content type.

decode(Mod, MsgName, Bin)

-spec decode(proto_module(), msg_name(), binary()) -> {ok, map() | tuple()} | {error, {decode, term()}}.

Decode protobuf bytes into a message map (gpb maps mode).

encode(Mod, MsgName, Msg)

-spec encode(proto_module(), msg_name(), map() | tuple()) -> {ok, binary()} | {error, {encode, term()}}.

Encode a message map/record to protobuf bytes.

is_grpc_content_type/1

-spec is_grpc_content_type(binary() | undefined) -> boolean().

Whether a content-type header value names a gRPC proto request. A trailing parameter (e.g. ;charset=...) is tolerated; the base type is matched.