Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter reserved_numbers and reserved_names tuples when converting from gpb representation #111

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion lib/exprotobuf/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
28 changes: 28 additions & 0 deletions test/protobuf_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
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

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
defmodule RoundtripProto2 do
use Protobuf, """
Expand Down