View Source Protobuf.TransformModule behaviour (protobuf v0.14.1)
Behaviour for transformer modules.
By defining a transform_module/0 function on your protobuf message module
you can add custom encoding and decoding logic for your message.
The Protobuf.new/1 function will not be called for structs that have a transform module, if
you still want to emulate this behavior you can use Protobuf.TransformModule.InferFieldsFromEnum.
As an example we can use this to implement a message that will be decoded as a string value:
defmodule StringMessage do
  use Protobuf, syntax: :proto3
  field :value, 1, type: :string
  def transform_module(), do: MyTransformModule
endThe transformer behaviour implementation:
defmodule MyTransformModule do
  @behaviour Protobuf.TransformModule
  defmacro typespec(_default_ast) do
    quote do
      @type t() :: String.t()
    end
  end
  @impl true
  def encode(string, StringMessage) when is_binary(string), do: struct(StringMessage, value: string)
  @impl true
  def decode(%{value: string}, StringMessage), do: string
endNotice that since the typespec/1 macro was introduced, transform modules can't
depend on the types that they transform anymore in compile time, meaning struct
syntax can't be used.
Summary
Callbacks
Takes any protobuf message and the message type and encodes it into arbitrary Elixir term.
Takes any Elixir term and the protobuf message type and encodes it into that type.
Transforms the typespec for modules using this transformer.
Types
Callbacks
Takes any protobuf message and the message type and encodes it into arbitrary Elixir term.
Called after a message is decoded.
Takes any Elixir term and the protobuf message type and encodes it into that type.
Called before a message is encoded.
Transforms the typespec for modules using this transformer.
If this callback is not present, the default typespec will be used.