bitwalker / exprotobuf

Protocol Buffers in Elixir made easy!
Apache License 2.0
486 stars 69 forks source link

UndefinedFunctionError in decoding #85

Closed zmagajna closed 5 years ago

zmagajna commented 6 years ago

Hi guys, First great job with the library it is nice and easy to use.

Now to the issue, a few days ago I have noticed that one of the parsers in my code keeps getting some strange errors:

** (UndefinedFunctionError) function :core_power_state.__struct__/0 is undefined (module :core_power_state is not available)
    :core_power_state.__struct__()
    (exprotobuf) lib/exprotobuf/utils.ex:28: Protobuf.Utils.convert_from_record/2
    (exprotobuf) lib/exprotobuf/decoder.ex:94: Protobuf.Decoder.convert_value/2
    (exprotobuf) lib/exprotobuf/decoder.ex:71: Protobuf.Decoder.convert_field/3
    (elixir) lib/enum.ex:1826: Enum."-reduce/3-lists^foldl/2-0-"/3

first, I just thought, there was something wrong with the encoding that I was, but as it turns out, I could encode the messages just fine, but when I try to decode same message I keep getting error above.

So I did a little bit of debugging and on line 39 in lib/exprotobuf/decoder.ex found the error: calling module.defs(:field, :gps_course_present) the module is returning wrong field:

%Protobuf.Field{fnum: 9, name: :gps_dop, occurrence: :optional, opts: [], rnum: 10, type: {:msg, Core.ProtofileFunctions.Values.Value.GpsDop}}

instead of:

%Protobuf.OneOfField{fields: [%Protobuf.Field{fnum: 10, name: :gps_course,
   occurrence: :optional, opts: [], rnum: 11, type: :uint32}],
 name: :gps_course_present, rnum: 11}

Is there something wrong with my syntax in the Protofile, or is this an issue in the builder of the Protofile?

PS: the file was created from the C++ side since I encode the messages on the device and decode them in the elixir app. On C++ side I can decode message.

zmagajna commented 6 years ago

83 This fixes problem, tested on my proto file and code.