bitwalker / exprotobuf

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

inject does not work without only #70

Closed rpihlak closed 7 years ago

rpihlak commented 7 years ago

I wrote a .proto file containing a single message:

message Message {
  required string field = 1;
}

I tried to inject this definition into a module as shown in README.md:

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

Creating the message fails:

iex(1)> %Message{}
** (CompileError) iex:1: Message.__struct__/1 is undefined, cannot expand struct Message

iex(1)> Message.new
** (UndefinedFunctionError) function Message.new/0 is undefined or private
    (example) Message.new()

However, it started to work after after adding only option:

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

Creating the message succeeds:

iex(1)> %Message{}
%Message{field: nil}
iex(2)> Message.new
%Message{field: nil}
bitwalker commented 7 years ago

I'll take a look!

bitwalker commented 7 years ago

This is fixed in master, I'll push a new release today

Cohedrin commented 7 years ago

I seem to still be running into this issue using 1.2.8 (or I'm just doing something horribly obvious wrong).

Lockfile:

  "exprotobuf": {:git, "https://github.com/bitwalker/exprotobuf.git", "fc6159d7f6a0b85930b2060d93be6fe3edf25fd9", [tag: "1.2.8"]}

Module (without only)

defmodule MessageTest do
  use Protobuf, from: ["priv/protos/message.proto"], inject: true
end

Output

iex(1)> MessageTest.__info__(:functions)
[defs: 0]

Module (with only)

defmodule MessageTest do
  use Protobuf, from: ["priv/protos/message.proto"], inject: true, only: :Message
end

Output

iex(1)> MessageTest.__info__(:functions)
[__struct__: 0, __struct__: 1, decode: 1, decode_delimited: 1, defs: 0, defs: 2,
 defs: 3, encode: 1, encode_delimited: 1, new: 0, new: 1, record: 0, syntax: 0]

If there's anymore info I can provide here that would be helpful, or anything else I can do, please let me know!