mirror of
https://github.com/valitydev/elixir-thrift.git
synced 2024-11-07 10:38:50 +00:00
Add exception serialization (#70)
This commit is contained in:
parent
8776c40aac
commit
0e339417fa
@ -41,7 +41,7 @@ defmodule Thrift.Generator.StructBinaryProtocol do
|
||||
alias Thrift.Generator.Utils
|
||||
alias Thrift.Parser.FileGroup
|
||||
alias Thrift.Parser.Models.{
|
||||
# Exception,
|
||||
Exception,
|
||||
Field,
|
||||
Struct,
|
||||
StructRef,
|
||||
@ -282,6 +282,19 @@ defmodule Thrift.Generator.StructBinaryProtocol do
|
||||
end
|
||||
end
|
||||
end
|
||||
def field_deserializer(struct=%Exception{}, field, name, file_group) do
|
||||
dest_module = FileGroup.dest_module(file_group, struct)
|
||||
quote do
|
||||
defp unquote(name)(<<unquote(@struct), unquote(field.id)::16-signed, rest::binary>>, acc) do
|
||||
case unquote(dest_module).BinaryProtocol.deserialize(rest) do
|
||||
{value, rest} ->
|
||||
unquote(name)(rest, %{acc | unquote(field.name) => value})
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
def field_deserializer({:map, {key_type, value_type}}, field, name, file_group) do
|
||||
key_name = :"#{name}__#{field.name}__key"
|
||||
value_name = :"#{name}__#{field.name}__value"
|
||||
@ -406,6 +419,19 @@ defmodule Thrift.Generator.StructBinaryProtocol do
|
||||
end
|
||||
end
|
||||
end
|
||||
def map_key_deserializer(struct=%Exception{}, key_name, value_name, file_group) do
|
||||
dest_module = FileGroup.dest_module(file_group, struct)
|
||||
quote do
|
||||
defp unquote(key_name)(<<rest::binary>>, stack) do
|
||||
case unquote(dest_module).BinaryProtocol.deserialize(rest) do
|
||||
{key, rest} ->
|
||||
unquote(value_name)(rest, key, stack)
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
def map_key_deserializer({:map, {key_type, value_type}}, key_name, value_name, file_group) do
|
||||
child_key_name = :"#{key_name}__key"
|
||||
child_value_name = :"#{key_name}__value"
|
||||
@ -527,6 +553,19 @@ defmodule Thrift.Generator.StructBinaryProtocol do
|
||||
end
|
||||
end
|
||||
end
|
||||
def map_value_deserializer(struct=%Exception{}, key_name, value_name, file_group) do
|
||||
dest_module = FileGroup.dest_module(file_group, struct)
|
||||
quote do
|
||||
defp unquote(value_name)(<<rest::binary>>, key, [map, remaining | stack]) do
|
||||
case unquote(dest_module).BinaryProtocol.deserialize(rest) do
|
||||
{value, rest} ->
|
||||
unquote(key_name)(rest, [Map.put(map, key, value), remaining - 1 | stack])
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
def map_value_deserializer({:map, {key_type, value_type}}, key_name, value_name, file_group) do
|
||||
child_key_name = :"#{value_name}__key"
|
||||
child_value_name = :"#{value_name}__value"
|
||||
@ -646,6 +685,19 @@ defmodule Thrift.Generator.StructBinaryProtocol do
|
||||
end
|
||||
end
|
||||
end
|
||||
def list_deserializer(struct=%Exception{}, name, file_group) do
|
||||
dest_module = FileGroup.dest_module(file_group, struct)
|
||||
quote do
|
||||
defp unquote(name)(<<rest::binary>>, [list, remaining | stack]) do
|
||||
case unquote(dest_module).BinaryProtocol.deserialize(rest) do
|
||||
{element, rest} ->
|
||||
unquote(name)(rest, [[element | list], remaining - 1 | stack])
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
def list_deserializer({:map, {key_type, value_type}}, name, file_group) do
|
||||
key_name = :"#{name}__key"
|
||||
value_name = :"#{name}__value"
|
||||
@ -743,7 +795,13 @@ defmodule Thrift.Generator.StructBinaryProtocol do
|
||||
]
|
||||
end
|
||||
end
|
||||
def value_serializer(struct=%Struct{name: _name}, var, file_group) do
|
||||
def value_serializer(struct=%Struct{}, var, file_group) do
|
||||
dest_module = FileGroup.dest_module(file_group, struct)
|
||||
quote do
|
||||
unquote(dest_module).serialize(unquote(var))
|
||||
end
|
||||
end
|
||||
def value_serializer(struct=%Exception{}, var, file_group) do
|
||||
dest_module = FileGroup.dest_module(file_group, struct)
|
||||
quote do
|
||||
unquote(dest_module).serialize(unquote(var))
|
||||
@ -766,6 +824,7 @@ defmodule Thrift.Generator.StructBinaryProtocol do
|
||||
def type_id(:string, _file_group), do: 11
|
||||
def type_id(:binary, _file_group), do: 11
|
||||
def type_id(%Struct{}, _file_group), do: 12
|
||||
def type_id(%Exception{}, _file_group), do: 12
|
||||
def type_id({:map, _}, _file_group), do: 13
|
||||
def type_id({:set, _}, _file_group), do: 14
|
||||
def type_id({:list, _}, _file_group), do: 15
|
||||
|
@ -58,6 +58,7 @@ defmodule Thrift.Generator.StructGenerator do
|
||||
defp zero(_schema, {:set, _}), do: quote do: nil
|
||||
defp zero(_schema, %{values: [{_, value} | _]}), do: value
|
||||
defp zero(_schema, %Thrift.Parser.Models.Struct{}), do: nil
|
||||
defp zero(_schema, %Thrift.Parser.Models.Exception{}), do: nil
|
||||
|
||||
# Zero values for user defined types
|
||||
defp zero(schema, %{referenced_type: type}=ref) do
|
||||
@ -99,6 +100,9 @@ defmodule Thrift.Generator.StructGenerator do
|
||||
def to_thrift(%Thrift.Parser.Models.Struct{name: name}, _file_group) do
|
||||
"#{name}"
|
||||
end
|
||||
def to_thrift(%Thrift.Parser.Models.Exception{name: name}, _file_group) do
|
||||
"#{name}"
|
||||
end
|
||||
def to_thrift(%Thrift.Parser.Models.StructRef{referenced_type: type}, file_group) do
|
||||
FileGroup.resolve(file_group, type) |> to_thrift(file_group)
|
||||
end
|
||||
|
@ -221,6 +221,31 @@ defmodule Thrift.Generator.BinaryProtocolTest do
|
||||
assert_serializes %Struct{val_list: [%Val{num: 91}]}, <<15, 0, 4, 12, 0, 0, 0, 1, 3, 0, 99, 91, 0, 0>>
|
||||
end
|
||||
|
||||
@thrift_file name: "exception.thrift", contents: """
|
||||
exception Ex {
|
||||
99: byte num;
|
||||
}
|
||||
struct Exception {
|
||||
1: Ex val;
|
||||
2: map<Ex, Ex> val_map;
|
||||
3: set<Ex> val_set;
|
||||
4: list<Ex> val_list;
|
||||
}
|
||||
"""
|
||||
|
||||
thrift_test "exception serialization" do
|
||||
assert_serializes %Exception{}, <<0>>
|
||||
assert_serializes %Exception{val: %Ex{}}, <<12, 0, 1, 0, 0>>
|
||||
assert_serializes %Exception{val: %Ex{num: 91}}, <<12, 0, 1, 3, 0, 99, 91, 0, 0>>
|
||||
assert_serializes %Exception{val_map: %{}}, <<13, 0, 2, 12, 12, 0, 0, 0, 0, 0>>
|
||||
assert_serializes %Exception{val_map: %{%Ex{num: 91} => %Ex{num: 92}}},
|
||||
<<13, 0, 2, 12, 12, 0, 0, 0, 1, 3, 0, 99, 91, 0, 3, 0, 99, 92, 0, 0>>
|
||||
assert_serializes %Exception{val_set: MapSet.new}, <<14, 0, 3, 12, 0, 0, 0, 0, 0>>
|
||||
assert_serializes %Exception{val_set: MapSet.new([%Ex{num: 91}])}, <<14, 0, 3, 12, 0, 0, 0, 1, 3, 0, 99, 91, 0, 0>>
|
||||
assert_serializes %Exception{val_list: []}, <<15, 0, 4, 12, 0, 0, 0, 0, 0>>
|
||||
assert_serializes %Exception{val_list: [%Ex{num: 91}]}, <<15, 0, 4, 12, 0, 0, 0, 1, 3, 0, 99, 91, 0, 0>>
|
||||
end
|
||||
|
||||
@thrift_file name: "composite.thrift", contents: """
|
||||
struct Composite {
|
||||
1: map<map<byte, byte>, map<byte, byte>> map_of_maps;
|
||||
|
Loading…
Reference in New Issue
Block a user