From cc1443ea7811bc5defe468b77bd77b50c8106a70 Mon Sep 17 00:00:00 2001 From: Francesco Zanitti Date: Wed, 4 Dec 2019 16:29:49 +0100 Subject: [PATCH 1/5] filter tuples when converting from gpb representation --- lib/exprotobuf.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/exprotobuf.ex b/lib/exprotobuf.ex index c7d04fd..132fbe2 100644 --- a/lib/exprotobuf.ex +++ b/lib/exprotobuf.ex @@ -197,7 +197,7 @@ defmodule Protobuf do # Apply namespace to top-level types defp namespace_types(parsed, ns, inject) do - for {{type, name}, fields} <- parsed, is_atom(name) do + for {{type, name}, fields} <- parsed, is_atom(name) and type != :reserved_numbers do parsed_type = if :gpb.is_msg_proto3(name, parsed), do: :proto3_msg, else: type if inject do From 056e164ec115e40730a91aebfa2312f976301bae Mon Sep 17 00:00:00 2001 From: Francesco Zanitti Date: Wed, 4 Dec 2019 16:57:34 +0100 Subject: [PATCH 2/5] skip :reserved_names too --- lib/exprotobuf.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/exprotobuf.ex b/lib/exprotobuf.ex index 132fbe2..fed3561 100644 --- a/lib/exprotobuf.ex +++ b/lib/exprotobuf.ex @@ -197,7 +197,8 @@ defmodule Protobuf do # Apply namespace to top-level types defp namespace_types(parsed, ns, inject) do - for {{type, name}, fields} <- parsed, is_atom(name) and type != :reserved_numbers do + for {{type, name}, fields} <- parsed, + is_atom(name) and type != :reserved_numbers and type != :reserved_names do parsed_type = if :gpb.is_msg_proto3(name, parsed), do: :proto3_msg, else: type if inject do From fef9dea5e7509b0e4ce65dd38fa6f7cd1be87476 Mon Sep 17 00:00:00 2001 From: Francesco Zanitti Date: Wed, 4 Dec 2019 16:57:49 +0100 Subject: [PATCH 3/5] added test for messages with reserved numbers and names --- test/protobuf_test.exs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/protobuf_test.exs b/test/protobuf_test.exs index d2d4d09..10f6214 100644 --- a/test/protobuf_test.exs +++ b/test/protobuf_test.exs @@ -1,6 +1,23 @@ defmodule ProtobufTest do use Protobuf.Case + test "can handle reserved fields" do + defmodule ReservedProto3 do + use Protobuf, """ + syntax = "proto3"; + + message Foo { + reserved 2, 15, 9 to 11; + reserved "foo", "bar"; + string f1 = 1; + } + """ + end + + # just the compilation pass should suffice as a test itself + _ = ReservedProto3.Foo.new(f1: "test") + end + test "can roundtrip encoding/decoding optional values in proto2" do defmodule RoundtripProto2 do use Protobuf, """ From 15c9f13798ebcb02c40d8a52b46727f34a342387 Mon Sep 17 00:00:00 2001 From: Francesco Zanitti Date: Thu, 5 Dec 2019 13:30:45 +0100 Subject: [PATCH 4/5] remove information about reserved fields at parsing stage --- lib/exprotobuf.ex | 3 +-- lib/exprotobuf/parser.ex | 8 +++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/exprotobuf.ex b/lib/exprotobuf.ex index fed3561..c7d04fd 100644 --- a/lib/exprotobuf.ex +++ b/lib/exprotobuf.ex @@ -197,8 +197,7 @@ defmodule Protobuf do # Apply namespace to top-level types defp namespace_types(parsed, ns, inject) do - for {{type, name}, fields} <- parsed, - is_atom(name) and type != :reserved_numbers and type != :reserved_names do + for {{type, name}, fields} <- parsed, is_atom(name) do parsed_type = if :gpb.is_msg_proto3(name, parsed), do: :proto3_msg, else: type if inject do diff --git a/lib/exprotobuf/parser.ex b/lib/exprotobuf/parser.ex index 99d3b22..8d4c936 100644 --- a/lib/exprotobuf/parser.ex +++ b/lib/exprotobuf/parser.ex @@ -18,10 +18,16 @@ defmodule Protobuf.Parser do |> finalize!(options) end + ## filter information about reserved numbers/names, + ## as they are useless in this context + defp filter_reserved({{:reserved_names, _}, _}), do: false + defp filter_reserved({{:reserved_numbers, _}, _}), do: false + defp filter_reserved(_), do: true + defp finalize!(defs, options) do case :gpb_parse.post_process_all_files(defs, options) do {:ok, defs} -> - defs + defs |> Enum.filter(&filter_reserved/1) {:error, error} -> msg = From bb3cc1edd3b674da070ef9fed07daca76d3bd943 Mon Sep 17 00:00:00 2001 From: Francesco Zanitti Date: Thu, 5 Dec 2019 13:30:57 +0100 Subject: [PATCH 5/5] added test for proto2 syntax --- test/protobuf_test.exs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/protobuf_test.exs b/test/protobuf_test.exs index 10f6214..c199f68 100644 --- a/test/protobuf_test.exs +++ b/test/protobuf_test.exs @@ -12,10 +12,21 @@ defmodule ProtobufTest do string f1 = 1; } """ - end + end + + defmodule ReservedProto2 do + use Protobuf, """ + message Foo { + reserved 2, 15, 9 to 11; + reserved "foo", "bar"; + string f1 = 1; + } + """ + end # just the compilation pass should suffice as a test itself _ = ReservedProto3.Foo.new(f1: "test") + _ = ReservedProto2.Foo.new(f1: "test") end test "can roundtrip encoding/decoding optional values in proto2" do