Move the field serializer into its own function (#85)

This helps reduce the complexity of struct_deserializer/2 a bit without
changing the functionality.
This commit is contained in:
Jon Parise 2016-12-26 16:24:14 -08:00 committed by GitHub
parent de5ac6f3f6
commit af6208ec8b

View File

@ -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 <<unquote(type_id(type, file_group)), unquote(id) :: size(16)>> 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 <<unquote(type_id(type, file_group)), unquote(id) :: size(16)>> 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)(<<unquote(@bool), unquote(field.id)::size(16), 1, rest::binary>>, acc) do