From 3c3b871a3016b69a60c005b109191ee446431490 Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Tue, 14 Jan 2025 18:10:58 -0300 Subject: [PATCH 1/3] Fix encode_to_iodata/1 not matching encode/1 behaviour Only encode/1 was applying transform in the outer struct --- lib/protobuf/encoder.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/protobuf/encoder.ex b/lib/protobuf/encoder.ex index 93ee98ff..feecda26 100644 --- a/lib/protobuf/encoder.ex +++ b/lib/protobuf/encoder.ex @@ -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) + |> encode_with_message_props(mod.__message_props__()) end @spec encode(struct()) :: binary() From 6e4a3c5f74797fb5b60ae9534a809393be10ec62 Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Tue, 14 Jan 2025 18:21:01 -0300 Subject: [PATCH 2/3] make it work --- lib/protobuf/encoder.ex | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/protobuf/encoder.ex b/lib/protobuf/encoder.ex index feecda26..30f42008 100644 --- a/lib/protobuf/encoder.ex +++ b/lib/protobuf/encoder.ex @@ -10,7 +10,7 @@ defmodule Protobuf.Encoder do def encode_to_iodata(%mod{} = struct) do struct |> transform_module(mod) - |> encode_with_message_props(mod.__message_props__()) + |> do_encode_to_iodata() end @spec encode(struct()) :: binary() @@ -21,6 +21,11 @@ defmodule Protobuf.Encoder do |> IO.iodata_to_binary() end + defp do_encode_to_iodata(%mod{} = struct) do + struct + |> encode_with_message_props(mod.__message_props__()) + end + defp encode_with_message_props( struct, %MessageProps{syntax: syntax, field_props: field_props, ordered_tags: ordered_tags} = @@ -150,7 +155,7 @@ 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, @@ -158,7 +163,7 @@ defmodule Protobuf.Encoder do "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 From 045420247a5c394a66855a5aa092b527ad699f4d Mon Sep 17 00:00:00 2001 From: felipe stival <14948182+v0idpwn@users.noreply.github.com> Date: Tue, 14 Jan 2025 18:57:40 -0300 Subject: [PATCH 3/3] Update lib/protobuf/encoder.ex --- lib/protobuf/encoder.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/protobuf/encoder.ex b/lib/protobuf/encoder.ex index 30f42008..4c426e94 100644 --- a/lib/protobuf/encoder.ex +++ b/lib/protobuf/encoder.ex @@ -22,8 +22,7 @@ defmodule Protobuf.Encoder do end defp do_encode_to_iodata(%mod{} = struct) do - struct - |> encode_with_message_props(mod.__message_props__()) + encode_with_message_props(struct, mod.__message_props__()) end defp encode_with_message_props(