Skip to content

Commit 62dfa12

Browse files
authored
Fix encode_to_iodata/1 not matching encode/1 behaviour (#397)
Only encode/1 was applying transform in the outer struct
1 parent 0e6bbc5 commit 62dfa12

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/protobuf/encoder.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ defmodule Protobuf.Encoder do
88

99
@spec encode_to_iodata(struct()) :: iodata()
1010
def encode_to_iodata(%mod{} = struct) do
11-
encode_with_message_props(struct, mod.__message_props__())
11+
struct
12+
|> transform_module(mod)
13+
|> do_encode_to_iodata()
1214
end
1315

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

24+
defp do_encode_to_iodata(%mod{} = struct) do
25+
encode_with_message_props(struct, mod.__message_props__())
26+
end
27+
2228
defp encode_with_message_props(
2329
struct,
2430
%MessageProps{syntax: syntax, field_props: field_props, ordered_tags: ordered_tags} =
@@ -148,15 +154,15 @@ defmodule Protobuf.Encoder do
148154
defp encode_from_type(mod, msg) do
149155
case msg do
150156
%{__struct__: ^mod} ->
151-
encode_to_iodata(msg)
157+
do_encode_to_iodata(msg)
152158

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

158164
_ ->
159-
encode_to_iodata(struct(mod, msg))
165+
do_encode_to_iodata(struct(mod, msg))
160166
end
161167
end
162168

0 commit comments

Comments
 (0)