Protobuf.Extension (protobuf v0.11.0) View Source

Extensions let you set extra fields for previously defined messages(even for messages in other packages) without changing the original message.

To load extensions you should call Protobuf.load_extensions/0 when your application starts:

def start(_type, _args) do
  Protobuf.load_extensions()
  Supervisor.start_link([], strategy: :one_for_one)
end

Examples

# protoc should be used to generate the code instead of writing by hand.
defmodule Foo do
  use Protobuf, syntax: :proto2

  extensions([{100, 101}, {1000, 536_870_912}])
end

# This module is generated for all "extend" calls in one file.
# This module is needed in `*_extension` function because the field name is scoped
# in the proto file.
defmodule Ext.PbExtension do
  use Protobuf, syntax: :proto2

  extend Foo, :my_custom, 1047, optional: true, type: :string
end

foo = Foo.new()
Foo.put_extension(foo, Ext.PbExtension, :my_custom, "Custom field")
Foo.get_extension(foo, Ext.PbExtension, :my_custom)

Link to this section Summary

Functions

The actual function for get_extension

The actual function for put_extension

Link to this section Functions

Link to this function

get(struct, extension_mod, field, default)

View Source

Specs

get(map(), module(), atom(), any()) :: any()

The actual function for get_extension

Link to this function

put(mod, struct, extension_mod, field, value)

View Source

Specs

put(module(), map(), module(), atom(), any()) :: map()

The actual function for put_extension