From 91d6159ebd598f0c20d88b0fc777f8e46a5e0490 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Thu, 17 Aug 2023 13:04:06 +0200 Subject: [PATCH 1/3] Add #{items := list()} type for encode/decode --- src/aeserialization.erl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/aeserialization.erl b/src/aeserialization.erl index 633c586..ded52fd 100644 --- a/src/aeserialization.erl +++ b/src/aeserialization.erl @@ -28,12 +28,14 @@ | 'binary' | 'id' %% As defined in aec_id.erl | [type()] %% Length one in the type. This means a list of any length. + | #{items := [{field_name(), type()}]} | tuple(). %% Any arity, containing type(). This means a static size array. -type encodable_term() :: non_neg_integer() | binary() | boolean() | [encodable_term()] %% Of any length + | #{atom() => encodable_term()} | tuple() %% Any arity, containing encodable_term(). | aeser_id:id(). @@ -101,6 +103,12 @@ decode_fields(Template, Values) -> encode_field([Type], L) when is_list(L) -> [encode_field(Type, X) || X <- L]; +encode_field(#{items := Items}, Map) -> + lists:map( + fun({K, Type}) -> + V = maps:get(K, Map), + encode_field(Type, V) + end, Items); encode_field(Type, T) when tuple_size(Type) =:= tuple_size(T) -> Zipped = lists:zip(tuple_to_list(Type), tuple_to_list(T)), [encode_field(X, Y) || {X, Y} <- Zipped]; @@ -117,6 +125,12 @@ encode_field(Type, Val) -> error({illegal, Type, Val}). decode_field([Type], List) when is_list(List) -> [decode_field(Type, X) || X <- List]; +decode_field(#{items := Items}, List) when length(List) =:= length(Items) -> + Zipped = lists:zip(Items, List), + lists:foldl( + fun({{K, Type}, V}, Map) -> + Map#{ K => decode_field(Type, V) } + end, #{}, Zipped); decode_field(Type, List) when length(List) =:= tuple_size(Type) -> Zipped = lists:zip(tuple_to_list(Type), List), list_to_tuple([decode_field(X, Y) || {X, Y} <- Zipped]); -- 2.30.2 From 05c4f87619246afcc937abcaab620d3ea611c6f9 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Thu, 17 Aug 2023 20:42:48 +0200 Subject: [PATCH 2/3] Update src/aeserialization.erl with type comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From code review Co-authored-by: Radosław Rowicki <35342116+radrow@users.noreply.github.com> --- src/aeserialization.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aeserialization.erl b/src/aeserialization.erl index ded52fd..0853e91 100644 --- a/src/aeserialization.erl +++ b/src/aeserialization.erl @@ -28,7 +28,7 @@ | 'binary' | 'id' %% As defined in aec_id.erl | [type()] %% Length one in the type. This means a list of any length. - | #{items := [{field_name(), type()}]} + | #{items := [{field_name(), type()}]} %% Record with named fields represented as a map. Encoded as a list in the given order. | tuple(). %% Any arity, containing type(). This means a static size array. -type encodable_term() :: non_neg_integer() -- 2.30.2 From 893ebb69f61e03640b615b19614682ca33eda124 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Thu, 17 Aug 2023 20:43:59 +0200 Subject: [PATCH 3/3] Update src/aeserialization.erl with duplicates check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From code review Co-authored-by: Radosław Rowicki <35342116+radrow@users.noreply.github.com> --- src/aeserialization.erl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/aeserialization.erl b/src/aeserialization.erl index 0853e91..11d70a8 100644 --- a/src/aeserialization.erl +++ b/src/aeserialization.erl @@ -129,6 +129,7 @@ decode_field(#{items := Items}, List) when length(List) =:= length(Items) -> Zipped = lists:zip(Items, List), lists:foldl( fun({{K, Type}, V}, Map) -> + maps:is_key(K, Map) andalso error(badarg, duplicate_field), Map#{ K => decode_field(Type, V) } end, #{}, Zipped); decode_field(Type, List) when length(List) =:= tuple_size(Type) -> -- 2.30.2