### Example Usage of Encoded/Decoded Data Source: https://github.com/bitwalker/exprotobuf/blob/master/imports_upgrade_guide.md Demonstrates encoding and decoding a Basic message using the generated Elixir module. ```elixir Test.Basic.new(id: 123, color: :RED) |> Test.Basic.encode # => <<8, 123, 16, 3>> Test.Basic.decode(<<8, 123, 16, 3>>) # => %Test.Basic{color: :RED, id: 123} ``` -------------------------------- ### Define Protobuf Messages from String Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Define Elixir modules and structs from a Protocol Buffer schema provided as a string. This example defines a message with a nested message and an enum. ```elixir defmodule Messages do use Protobuf, """ message Msg { message SubMsg { required uint32 value = 1; } enum Version { V1 = 1; V2 = 2; } required Version version = 2; optional SubMsg sub = 1; } """ end ``` -------------------------------- ### Define Protobuf Messages with Oneof Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Defines a Protocol Buffer message using the `oneof` construct, allowing a field to hold one of several types. The example shows how to represent this in Elixir using tuples. ```elixir defmodule Messages do use Protobuf, """ message Msg { oneof choice { string first = 1; int32 second = 2; } } """ end ``` -------------------------------- ### New Exprotobuf Import Behavior Source: https://github.com/bitwalker/exprotobuf/blob/master/imports_upgrade_guide.md Shows the current recommended way to use Exprotobuf by explicitly listing all proto files. ```elixir defmodule Test do use Protobuf, from: ["./test/basic.proto","./test/colors.proto"] end ``` -------------------------------- ### Load All Protobuf Definitions from Wildcard Path Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Loads all `.proto` files within a specified directory and its subdirectories. This is useful for managing multiple protobuf definitions. ```elixir defmodule Protobufs do use Protobuf, from: Path.wildcard(Path.expand("../definitions/**/*.proto", __DIR__)) end ``` -------------------------------- ### Instantiate Messages Loaded from Wildcard Path Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Demonstrates creating instances of messages defined in `.proto` files loaded using a wildcard path. This shows how messages can share definitions like enums. ```elixir iex> Protobufs.Msg.new(v: :V1) %Protobufs.Msg{v: :V1} iex> %Protobufs.OtherMessage{middle_name: "Danger"} %Protobufs.OtherMessage{middle_name: "Danger"} ``` -------------------------------- ### Old Exprotobuf Import Behavior Source: https://github.com/bitwalker/exprotobuf/blob/master/imports_upgrade_guide.md Illustrates the previous method of using Exprotobuf where imports were automatically handled. ```elixir defmodule Test do use Protobuf, from: "./test/basic.proto" end ``` -------------------------------- ### Define Protobuf Messages from File Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Loads Protocol Buffer definitions from a specified `.proto` file. This is an alternative to defining schemas directly as strings. ```elixir defmodule Messages do use Protobuf, from: Path.expand("../proto/messages.proto", __DIR__) end ``` -------------------------------- ### Instantiate Message with Package Name Namespace Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Creates an instance of a protobuf message loaded with `use_package_names: true`. The module name reflects the package structure defined in the `.proto` file. ```elixir iex> Definitions.World.Example.new(continent: :EUROPE) %Definitions.World.Example{continent: :EUROPE} ``` -------------------------------- ### Basic Proto Definition Source: https://github.com/bitwalker/exprotobuf/blob/master/imports_upgrade_guide.md Defines a basic message with an ID and an optional color, importing definitions from colors.proto. ```protobuf import "colors.proto"; message Basic { required uint32 id = 1; optional Color color = 2; } ``` -------------------------------- ### Add exprotobuf to Applications Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Ensure exprotobuf is included in your application's list of applications. ```elixir def application do [applications: [:exprotobuf]] end ``` -------------------------------- ### Load Protobufs with Package Names Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Loads protobuf definitions from files while preserving their original package names as Elixir namespaces. This is useful when integrating with existing protobuf schemas. ```elixir defmodule Definitions do use Protobuf, from: Path.wildcard("protobufs/*.proto"), use_package_names: true end ``` -------------------------------- ### Instantiate Message with Explicit Top-Level Namespace Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Creates an instance of a protobuf message when all definitions are placed under an explicit top-level namespace. The module name is directly the message name, prefixed by the specified namespace. ```elixir iex> World.Example.new(continent: :EUROPE) %World.Example{continent: :EUROPE} ``` -------------------------------- ### Load Protobufs with Explicit Top-Level Namespace Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Loads protobuf definitions from files and places all generated modules under a specified top-level Elixir namespace. This allows for consistent naming regardless of the protobuf package structure. ```elixir defmodule Definitions do use Protobuf, from: Path.wildcard("protobufs/*.proto"), use_package_names: true, namespace: :"Elixir" end ``` -------------------------------- ### Interact with Injected Module Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Demonstrates creating and updating a message using the injected module definition. ```elixir iex> %Msg{} iiex> Msg.update(%Msg{}, :v, :V2) %Msg{v: :V2} ``` -------------------------------- ### Add exprotobuf Dependency Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Add exprotobuf to your project's dependencies in `mix.exs`. ```elixir defp deps do [{:exprotobuf, "~> x.x.x"}] end ``` -------------------------------- ### Extend Generated Modules with use_in Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Extends generated modules by defining helper functions within a macro. Use this when direct injection is not feasible. ```elixir defmodule Messages do use Protobuf, " message Msg { enum Version { V1 = 1; V2 = 1; } required Version v = 1; } " defmodule MsgHelpers do defmacro __using__(_opts) do quote do def convert_to_record(msg) do msg |> Map.to_list |> Enum.reduce([], fn {_key, value}, acc -> [value | acc] end) |> Enum.reverse |> list_to_tuple end end end end use_in "Msg", MsgHelpers end ``` -------------------------------- ### Use Extended Module Functionality Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Demonstrates calling a custom helper function defined via `use_in` on a generated message struct. ```elixir iex> Messages.Msg.new |> Messages.Msg.convert_to_record {Messages.Msg, :V1} ``` -------------------------------- ### Encode and Decode Protobuf Messages Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Demonstrates encoding an Elixir struct into binary protobuf format and decoding it back. This uses the `Messages.Msg` module generated from the string schema. ```elixir iex> msg = Messages.Msg.new(version: :'V2') %Messages.Msg{version: :V2, sub: nil} iex> encoded = Messages.Msg.encode(msg) <<16, 2>> iex> Messages.Msg.decode(encoded) %Messages.Msg{version: :V2, sub: nil} ``` -------------------------------- ### Colors Proto Definition Source: https://github.com/bitwalker/exprotobuf/blob/master/imports_upgrade_guide.md Defines an enum for colors used in protobuf messages. ```protobuf enum Color { WHITE = 0; BLACK = 1; GRAY = 2; RED = 3; } ``` -------------------------------- ### Inject Module Definition Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Injects a module definition directly into the current module. Useful for single types or when consolidating definitions. ```elixir defmodule Msg do use Protobuf, from: Path.expand("../proto/messages.proto", __DIR__), inject: true def update(msg, key, value), do: Map.put(msg, key, value) end ``` -------------------------------- ### Inject Specific Types Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Injects only specified types (e.g., TypeA, TypeB) from a larger schema into a module. Be mindful of type dependencies. ```elixir defmodule Messages do use Protobuf, from: Path.expand("../proto/messages.proto", __DIR__), only: [:TypeA, :TypeB] end ``` -------------------------------- ### Encode Protobuf Messages with Oneof Source: https://github.com/bitwalker/exprotobuf/blob/master/README.md Encodes a message containing a `oneof` field. The `choice` field is set to a tuple representing the field name and its value. ```elixir iex> msg = Messages.Msg.new(choice: {:second, 42}) %Messages.Msg{choice: {:second, 42}} iex> encoded = Messages.Msg.encode(msg) <<16, 42>> ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.