Skip to content

Commit

Permalink
Fix parse of unpacked repeated fields in proto3 files
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamez committed Jun 16, 2020
1 parent 9e89fbd commit 728be4e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/protox/parse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ defmodule Protox.Parse do
%FieldDescriptorProto{label: :repeated, options: %FieldOptions{packed: true}} ->
:packed

%FieldDescriptorProto{label: :repeated, options: %FieldOptions{packed: false}} ->
:unpacked

%FieldDescriptorProto{label: :repeated} ->
case {syntax, type} do
{:proto3, ty} when is_primitive(ty) -> :packed
Expand Down
7 changes: 7 additions & 0 deletions test/protox/parse_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ defmodule Protox.ParseTest do
{:enum, Proto2A.NestedEnum}}
end

test "Parse Proto3, packed and unpacked fields", %{messages: messages} do
fs = fields(messages, Abc.Def.Proto3)

assert field(fs, 200) == {:repeated, :repeated_int32_packed, :packed, :int32}
assert field(fs, 201) == {:repeated, :repeated_int32_unpacked, :unpacked, :int32}
end

defp field(fields, tag) do
fields
|> Enum.find(&(elem(&1, 0) == tag))
Expand Down
8 changes: 6 additions & 2 deletions test/protox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ defmodule ProtoxTest do
134 => {:oneof_2_int32, {:oneof, :oneof_2}, :int32},
135 =>
{:oneof_2_nested_enum, {:oneof, :oneof_2}, {:enum, Abc.Def.Proto3.NestedEnum}},
9999 => {:nested_enum, {:default, :FOO}, {:enum, Abc.Def.Proto3.NestedEnum}}
9999 => {:nested_enum, {:default, :FOO}, {:enum, Abc.Def.Proto3.NestedEnum}},
200 => {:repeated_int32_packed, :packed, :int32},
201 => {:repeated_int32_unpacked, :unpacked, :int32}
}
end

Expand Down Expand Up @@ -221,7 +223,9 @@ defmodule ProtoxTest do
{:oneof_2_nested_enum, {:oneof, :oneof_2},
{:enum, Namespace.Abc.Def.Proto3.NestedEnum}},
9999 =>
{:nested_enum, {:default, :FOO}, {:enum, Namespace.Abc.Def.Proto3.NestedEnum}}
{:nested_enum, {:default, :FOO}, {:enum, Namespace.Abc.Def.Proto3.NestedEnum}},
200 => {:repeated_int32_packed, :packed, :int32},
201 => {:repeated_int32_unpacked, :unpacked, :int32}
}
end

Expand Down
Binary file modified test/samples/file_descriptor_set.bin
Binary file not shown.
3 changes: 3 additions & 0 deletions test/samples/proto3.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ message Proto3 {
}

NestedEnum nested_enum = 9999;

repeated int32 repeated_int32_packed = 200 [packed = true];
repeated int32 repeated_int32_unpacked = 201 [packed = false];
}

message EmptyProto3 {
Expand Down

0 comments on commit 728be4e

Please sign in to comment.