elixir-protobuf / protobuf

A pure Elixir implementation of Google Protobuf.
https://hexdocs.pm/protobuf/readme.html
MIT License
823 stars 143 forks source link

Error on Google.Protobuf.Struct.encode #65

Closed Chervychnyk closed 5 years ago

Chervychnyk commented 5 years ago

I am receiving error when try to encode Google.Protobuf.Struct. Example of executed code: Google.Protobuf.Struct.encode(Google.Protobuf.Struct.new(fields: %{"valid" => {:bool_value, true}})) Stacktrace: ** (Protobuf.EncodeError) Got error when encoding Google.Protobuf.Struct#fields: %Protobuf.EncodeError{message: "Got error when encoding Google.Protobuf.Struct.FieldsEntry#value: %Protocol.UndefinedError{description: \"\", protocol: Enumerable, value: {:bool_value, true}}"} (elixir) /home/build/elixir/lib/elixir/lib/enum.ex:1: Enumerable.impl_for!/1 (elixir) /home/build/elixir/lib/elixir/lib/enum.ex:141: Enumerable.reduce/3 (elixir) lib/enum.ex:3015: Enum.reduce/3 (protobuf) lib/protobuf/builder.ex:12: Protobuf.Builder.new/2 (protobuf) deps/protobuf/lib/protobuf/encoder.ex:14: Protobuf.Encoder.encode/3 (protobuf) deps/protobuf/lib/protobuf/encoder.ex:83: anonymous fn/5 in Protobuf.Encoder.encode_field/3 (protobuf) deps/protobuf/lib/protobuf/encoder.ex:50: anonymous fn/5 in Protobuf.Encoder.encode!/2 (elixir) lib/enum.ex:1940: Enum."-reduce/3-lists^foldl/2-0-"/3 (protobuf) deps/protobuf/lib/protobuf/encoder.ex:20: Protobuf.Encoder.encode/2 (protobuf) deps/protobuf/lib/protobuf/encoder.ex:83: anonymous fn/5 in Protobuf.Encoder.encode_field/3 (elixir) lib/enum.ex:1331: anonymous fn/3 in Enum.map/2 (stdlib) maps.erl:232: :maps.fold_1/3 (elixir) lib/enum.ex:1956: Enum.map/2 (protobuf) deps/protobuf/lib/protobuf/encoder.ex:50: anonymous fn/5 in Protobuf.Encoder.encode!/2 (elixir) lib/enum.ex:1940: Enum."-reduce/3-lists^foldl/2-0-"/3 (protobuf) deps/protobuf/lib/protobuf/encoder.ex:20: Protobuf.Encoder.encode/

I think that is because nil passed as argument to Protobuf.Encoder.is_enum_default/2 and is_atom(v) guard thinks that it is valid. I can fix it by adding not is_nil check to function, but I don't know if it is a right way to solve this problem.

tony612 commented 5 years ago

The issue you had has been fixed. And the code should be

Google.Protobuf.Struct.new(fields: %{"valid" => %{kind: {:bool_value, true}}})

See this test https://github.com/tony612/protobuf-elixir/blob/3a35fdd62d99a97d0e2fea261e4a78a2775f1122/test/protobuf/encoder_test.exs#L165-L167