Skip to content

Commit

Permalink
Fix encode_to_iodata/1 not matching encode/1 behaviour (#397)
Browse files Browse the repository at this point in the history
Only encode/1 was applying transform in the outer struct
  • Loading branch information
v0idpwn authored Jan 14, 2025
1 parent 0e6bbc5 commit 62dfa12
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/protobuf/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ defmodule Protobuf.Encoder do

@spec encode_to_iodata(struct()) :: iodata()
def encode_to_iodata(%mod{} = struct) do
encode_with_message_props(struct, mod.__message_props__())
struct
|> transform_module(mod)
|> do_encode_to_iodata()
end

@spec encode(struct()) :: binary()
Expand All @@ -19,6 +21,10 @@ defmodule Protobuf.Encoder do
|> IO.iodata_to_binary()
end

defp do_encode_to_iodata(%mod{} = struct) do
encode_with_message_props(struct, mod.__message_props__())
end

defp encode_with_message_props(
struct,
%MessageProps{syntax: syntax, field_props: field_props, ordered_tags: ordered_tags} =
Expand Down Expand Up @@ -148,15 +154,15 @@ defmodule Protobuf.Encoder do
defp encode_from_type(mod, msg) do
case msg do
%{__struct__: ^mod} ->
encode_to_iodata(msg)
do_encode_to_iodata(msg)

%other_mod{} = struct ->
raise Protobuf.EncodeError,
message:
"struct #{inspect(other_mod)} can't be encoded as #{inspect(mod)}: #{inspect(struct)}"

_ ->
encode_to_iodata(struct(mod, msg))
do_encode_to_iodata(struct(mod, msg))
end
end

Expand Down

0 comments on commit 62dfa12

Please sign in to comment.