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

Using inject breaks enumerations with proto3 #76

Open
Cohedrin opened this issue Jul 6, 2017 · 1 comment
Open

Using inject breaks enumerations with proto3 #76

Cohedrin opened this issue Jul 6, 2017 · 1 comment

Comments

@Cohedrin
Copy link

Cohedrin commented Jul 6, 2017

When using inject alongside enumerations, the namespace of the enumeration modules is inconsistent, causing the call to get_default to error with a no match, coming from here.

Reference protobuf

syntax = "proto3";

message Message {
  enum Status {
    STATE_1 = 0;
    STATE_2 = 1;
  }

  required int64 id = 1;
  optional Status status = 2;
}

Example

Module

defmodule MessageTest do
  use Protobuf, from: Path.expand("priv/protos/message.proto"), inject: true, only: :Message
end

Defs

iex(1)> MessageTest.defs()
[{{:enum, :"Message.Status"}, [STATE_1: 0, STATE_2: 1]},
 {{:msg, :Message},
  [%Protobuf.Field{fnum: 1, name: :id, occurrence: :required, opts: [], rnum: 2,
    type: :int64},
   %Protobuf.Field{fnum: 2, name: :status, occurrence: :optional, opts: [],
    rnum: 3, type: {:enum, MessageTest.Message.Status}}]}]

Because of mismatch of the naming (MessageTest.Message.Status in the field vs :enum, :"Message.Status" in the key), when the field is looked up in proto3_type_default, lists:keyfind/3 returns false, rather than a match.

I was able to circumvent this by adding use_module_namespace: true, unfortunately this has the side effect of ignoring the inject and only option, as there is no function clause that takes it into account

When not using inject, the field defs are generated as expected

Example

Module

defmodule MessageTest do
  use Protobuf, from: Path.expand("priv/protos/message.proto")
end

Defs

iex(1)> MessageTest.defs()
[{{:enum, MessageTest.Message.Status}, [STATE_1: 0, STATE_2: 1]},
 {{:msg, MessageTest.Message},
  [%Protobuf.Field{fnum: 1, name: :id, occurrence: :required, opts: [], rnum: 2,
    type: :int64},
   %Protobuf.Field{fnum: 2, name: :status, occurrence: :optional, opts: [],
    rnum: 3, type: {:enum, MessageTest.Message.Status}}]}]

I'm happy to make a pr solving this issue, but would like to get some feedback on the direction to take with this before making a pr.

@bitwalker
Copy link
Owner

If you open a PR with a fix, I'll be happy to work with you on it there, I think we'll have to iterate on it a bit, but if you kick things off I can pick up where you leave off if time is an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants