PB.Adapter (PB v0.1.0)

Copy Markdown View Source

Message-level app/proto conversion spec.

Adapters bridge the on-wire protobuf shape (a canonical map) and a caller-chosen application term such as a tagged tuple, struct, or scalar. Each adapter registers two {module, function} MFAs:

  • :to_proto — converts an application value to the protobuf-shaped map.
  • :from_proto — converts a decoded protobuf map back to the application value.

Callbacks must return {:ok, value} | {:error, reason}.

adapter =
  %PB.Adapter{
    to_proto: {MyApp.TimestampCodec, :to_proto},
    from_proto: {MyApp.TimestampCodec, :from_proto},
    type: quote(do: DateTime.t())
  }

PB.compile(descriptor_set,
  projections: [{:"google.protobuf.Timestamp", adapter: adapter}]
)

The optional :type field carries an Elixir typespec used for PB.Adapter-aware docs; it is otherwise inert. The fully-qualified message name an adapter targets is supplied by the surrounding :projections entry, not by the adapter struct itself.

Summary

Types

mfa_ref()

@type mfa_ref() :: {module(), atom()}

t()

@type t() :: %PB.Adapter{
  from_proto: mfa_ref(),
  to_proto: mfa_ref(),
  type: term() | nil
}