[BUG]
eddy147 opened this issue · comments
Describe the bug
The generated code, if the output is in 1 file, is wrapped in a list.
To Reproduce
Have a nested message.
I ran the generate command like so:
MIX_ENV=prod mix protox.generate --output-path=./lib/protos.ex --namespace=Protos ./path/to/file.proto
Expected behaviour
Have the defmodules listed not in List form
Actual behaviour
[
defmodule Protos.CalibrationErrorStatus do
@moduledoc false
(
defstruct []
(
@spec default() :: :CALIBRATION_ERROR_STATUS_UNINIT
def default() do
:CALIBRATION_ERROR_STATUS_UNINIT
end
)
...
which is weird: why the List?
Also, the Message that is calling all other messages is Request, not CalibrationErrorStatus.
Environment (please complete the following information):
- Elixir version [1.13-otp-24]
- Erlang version [24.2]
- OS: Linux Mint
-
- proto version [2]
Additional context
When using --multiple-files, and thus having the 1 defmodule per file, the main wrapping list is gone, but the functions are wrapped in a List again:
@spec encode(atom()) :: integer() | atom()
[
(
def encode(:TRIP_TYPE_UNDEFINED) do
0
end
def encode("TRIP_TYPE_UNDEFINED") do
0
end
),
(
d
Maybe it's not a bug, and I am doing something not right.
Hello !
While I agree it might seem strange to have this output, it's perfectly normal as it's the output of macros as string used by protox.
So yes, it's not a bug 🙂. Unless, of course, you observe the wrong behavior or it fails to compile.
Regarding this part of your message:
Also, the Message that is calling all other messages is Request, not CalibrationErrorStatus.
I don't understand the problem here: do you mean the generated code is incorrect? If so, can you share the *.proto
file(s)?
Aha.In that case, no bug of course, pls disregard my question about CalibrationErrorStatus.
I just do not know how to use the generated code, since they are now a product if Macro.to_string/2.
How can I get the defmodules or the defs?
I'd suggest you don't look at the generated code, it's easier to start from your *.proto
definitions. If, for instance, you have the following definition:
syntax = "proto3";
enum E {
ZERO = 0;
ONE = 1;
}
message Msg{
int32 a = 1;
E b = 2;
}
Then, you can create, encode and decode a Msg
like this:
iex> msg = %Msg{a: 42, b: :ONE}
iex> encoded = Msg.encode!(msg)
iex> decoded = encoded |> :binary.list_to_bin() |> Msg.decode!()
So, you just have to manipulate messages as if they were ordinary Elixir structures.
I'll close this issue as it's not a bug, but feel free to comment on this issue if you need help!
thank you for your time & effort. really appreciate the unit tests, the reason I chose this.