diff --git a/lib/thrift/generator/struct_binary_protocol.ex b/lib/thrift/generator/struct_binary_protocol.ex index 2cbf606..5b0e781 100644 --- a/lib/thrift/generator/struct_binary_protocol.ex +++ b/lib/thrift/generator/struct_binary_protocol.ex @@ -78,21 +78,8 @@ defmodule Thrift.Generator.StructBinaryProtocol do quote do: %unquote(name){unquote_splicing(field_matchers)} end - field_serializers = Enum.map(fields, - fn %Field{name: name, type: type, id: id} -> - var = Macro.var(name, nil) - quote do - case unquote(var) do - nil -> - <<>> - _ -> - unquote([ - quote do <> end, - value_serializer(type, var, file_group) - ] |> Utils.merge_binaries |> Utils.simplify_iolist) - end - end - end) + field_serializers = fields + |> Enum.map(&field_serializer(&1, file_group)) field_deserializers = fields |> Enum.map(&field_deserializer(&1.type, &1, :deserialize, file_group)) @@ -209,6 +196,21 @@ defmodule Thrift.Generator.StructBinaryProtocol do end end + defp field_serializer(%Field{name: name, type: type, id: id}, file_group) do + var = Macro.var(name, nil) + quote do + case unquote(var) do + nil -> + <<>> + _ -> + unquote([ + quote do <> end, + value_serializer(type, var, file_group) + ] |> Utils.merge_binaries |> Utils.simplify_iolist) + end + end + end + defp field_deserializer(:bool, field, name, _file_group) do quote do defp unquote(name)(<>, acc) do