Trogon.TypeProvider (Trogon.TypeProvider v0.3.0)

View Source

Provides a type mapping system for converting between string type names and Elixir struct modules.

Summary

Functions

Imports all the types from another module defined by Trogon.TypeProvider.

Registers a mapping from a Protobuf message module using its full_name/0 function as the type.

Registers a mapping from a type string to an Elixir Module that defines a struct.

Functions

import_type_provider(provider_mod)

(macro)
@spec import_type_provider(provider_mod :: module()) :: Macro.t()

Imports all the types from another module defined by Trogon.TypeProvider.

Example

defmodule UserTypeProvider do
  use Trogon.TypeProvider
  # ...
end

defmodule MyAppTypeProvider do
  use Trogon.TypeProvider

  import_type_provider UserTypeProvider
end

register_protobuf_message(struct_mod)

(macro)
@spec register_protobuf_message(struct_mod :: module()) :: Macro.t()

Registers a mapping from a Protobuf message module using its full_name/0 function as the type.

This macro requires the Protobuf message module to have a full_name/0 function that returns the fully qualified protobuf type name (e.g., "google.protobuf.Timestamp").

To generate protobuf modules with full_name/0, ensure your protobuf modules are generated with the gen_descriptors=true option, or define full_name/0 manually.

Example

defmodule MyTypeProvider do
  use Trogon.TypeProvider

  # Registers the module using its full_name/0 as the type
  register_protobuf_message MyApp.Proto.AccountCreated
end

This is equivalent to:

register_type "my_app.account_created", MyApp.Proto.AccountCreated

(assuming MyApp.Proto.AccountCreated.full_name() returns "my_app.account_created")

register_type(type, struct_mod)

(macro)
@spec register_type(type :: String.t(), struct_mod :: module()) :: Macro.t()

Registers a mapping from a type string to an Elixir Module that defines a struct.

Example

defmodule MyTypeProvider do
  use Trogon.TypeProvider,
    prefix: "accounts." # optional, adds the prefix to the type

  register_type "account_created", AccountCreated
end