Skip to content

Commit 8b48969

Browse files
committed
feat: add full_name callback to proto messages
1 parent 64d4ced commit 8b48969

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

lib/protobuf.ex

+8
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ defmodule Protobuf do
8282
Protobuf.Builder.new!(__MODULE__, attrs)
8383
end
8484

85+
@impl unquote(__MODULE__)
86+
def full_name(), do: @options[:full_name]
87+
8588
@impl unquote(__MODULE__)
8689
def transform_module() do
8790
nil
@@ -175,6 +178,11 @@ defmodule Protobuf do
175178
"""
176179
@callback transform_module() :: module() | nil
177180

181+
@doc """
182+
Returns the fully qualified name of the descriptor's target.
183+
"""
184+
@callback full_name() :: String.t()
185+
178186
@doc """
179187
Decodes the given binary data interpreting it as the Protobuf message `module`.
180188

lib/protobuf/protoc/generator/enum.ex

+6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ defmodule Protobuf.Protoc.Generator.Enum do
1919
def generate(%Context{namespace: ns} = ctx, %Google.Protobuf.EnumDescriptorProto{} = desc) do
2020
msg_name = Util.mod_name(ctx, ns ++ [Macro.camelize(desc.name)])
2121

22+
full_name =
23+
[ctx.package | ns ++ [Macro.camelize(desc.name)]]
24+
|> Enum.reject(&is_nil/1)
25+
|> Enum.join(".")
26+
2227
use_options =
2328
Util.options_to_str(%{
2429
syntax: ctx.syntax,
30+
full_name: "\"#{full_name}\"",
2531
enum: true,
2632
protoc_gen_elixir_version: "\"#{Util.version()}\""
2733
})

lib/protobuf/protoc/generator/message.ex

+5-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ defmodule Protobuf.Protoc.Generator.Message do
3535
msg_name = Util.mod_name(ctx, new_ns)
3636
fields = get_fields(ctx, desc)
3737
extensions = get_extensions(desc)
38+
full_name = [ctx.package | new_ns] |> Enum.reject(&is_nil/1) |> Enum.join(".")
3839

3940
descriptor_fun_body =
4041
if ctx.gen_descriptors? do
@@ -52,7 +53,7 @@ defmodule Protobuf.Protoc.Generator.Message do
5253
message_template(
5354
comment: Comment.get(ctx),
5455
module: msg_name,
55-
use_options: msg_opts_str(ctx, desc.options),
56+
use_options: msg_opts_str(ctx, desc.options, full_name),
5657
oneofs: desc.oneof_decl,
5758
fields: gen_fields(ctx.syntax, fields),
5859
descriptor_fun_body: descriptor_fun_body,
@@ -98,14 +99,15 @@ defmodule Protobuf.Protoc.Generator.Message do
9899
":#{f[:name]}, #{f[:number]}, #{label_str}type: #{f[:type]}#{opts_str}"
99100
end
100101

101-
defp msg_opts_str(%{syntax: syntax}, opts) do
102+
defp msg_opts_str(%{syntax: syntax}, opts, full_name) do
102103
msg_options = opts
103104

104105
opts = %{
105106
syntax: syntax,
106107
map: msg_options && msg_options.map_entry,
107108
deprecated: msg_options && msg_options.deprecated,
108-
protoc_gen_elixir_version: "\"#{Util.version()}\""
109+
protoc_gen_elixir_version: "\"#{Util.version()}\"",
110+
full_name: "\"#{full_name}\""
109111
}
110112

111113
str = Util.options_to_str(opts)

test/protobuf/protoc/generator/enum_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ defmodule Protobuf.Protoc.Generator.EnumTest do
6868
assert inspect(compiled_mod) == module
6969

7070
assert msg =~ "defmodule #{module} do\n"
71-
assert msg =~ "use Protobuf, enum: true, protoc_gen_elixir_version: \"#{Util.version()}\"\n"
71+
assert msg =~ "use Protobuf, enum: true, full_name: \"#{module}\", protoc_gen_elixir_version: \"#{Util.version()}\"\n"
7272

7373
refute msg =~ "defstruct "
7474

0 commit comments

Comments
 (0)