Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/protobuf/protoc/generator/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ defmodule Protobuf.Protoc.Generator.Message do
{fmt_type_name(f.name, longest_width), "{atom, any}"}
end)


types =
types ++
Enum.map(fields, fn f ->
Expand Down Expand Up @@ -153,7 +154,7 @@ defmodule Protobuf.Protoc.Generator.Message do
end

defp fmt_type(%{label: "repeated", type_enum: type_enum, type: type}) do
"[#{type_to_spec(type_enum, type, true)}]"
type_to_spec(type_enum, type, true)
end

defp fmt_type(%{type_enum: type_enum, type: type}) do
Expand Down
2 changes: 1 addition & 1 deletion lib/protobuf/type_util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Protobuf.TypeUtil do
def enum_to_spec(:TYPE_SINT32), do: "integer"
def enum_to_spec(:TYPE_SINT64), do: "integer"
def enum_to_spec(_), do: "any"
def enum_to_spec(:TYPE_MESSAGE, type, true = _repeated), do: "#{type}.t"
def enum_to_spec(:TYPE_MESSAGE, type, true = _repeated), do: "[#{type}.t]"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just see that message type doesn't add | nil into generated type. Should we need to add into this patch?

def enum_to_spec(:TYPE_MESSAGE, type, false = _repeated), do: "#{type}.t | nil"
def enum_to_spec(:TYPE_ENUM, type, true = _repeated), do: "[#{type}.t]"
def enum_to_spec(:TYPE_ENUM, type, false = _repeated), do: "#{type}.t"
Expand Down
27 changes: 27 additions & 0 deletions test/protobuf/protoc/generator/message_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,31 @@ defmodule Protobuf.Protoc.Generator.MessageTest do
assert msg =~ "field :the_field_name, 1, required: true, type: :string\n"
end
end

test "generate/2 repeated enum field in typespec" do
ctx = %Context{
package: "foo_bar.ab_cd",
dep_type_mapping: %{
".foo_bar.ab_cd.EnumFoo" => %{type_name: "FooBar.AbCd.EnumFoo"}
}
}

desc =
Google.Protobuf.DescriptorProto.new(
name: "Foo",
field: [
Google.Protobuf.FieldDescriptorProto.new(
name: "a",
json_name: "a",
number: 1,
type: :TYPE_ENUM,
label: :LABEL_REPEATED,
type_name: ".foo_bar.ab_cd.EnumFoo"
)
]
)

{[], [msg]} = Generator.generate(ctx, desc)
assert msg =~ "a: [FooBar.AbCd.EnumFoo.t]"
end
end