protobuf v0.8.0-beta.1 Protobuf.Extension

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

This is an experimental feature, to use it, Erlang should be >= 21.2 because :persistent_term is used and config should be set:

# Without this, modules won't be scanned to get extensions metadata.
# Functions like `get_extension` and `put_extension` still exist, but they don't work.
config :protobuf, extensions: :enabled

To know what extensions a module has and what are their metadata, all modules are scanned when :protobuf application starts. Now :persistent_term is used to store the runtime information. The runtime info is used to validate the extension when calling put_extension and decode/encode the extensions.

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)
get(map(), module(), atom(), any()) :: any()

The actual function for get_extension

Link to this function

put(mod, struct, extension_mod, field, value)
put(module(), map(), module(), atom(), any()) :: map()

The actual function for put_extension