Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #{items := list()} type for encode/decode #20

Merged
merged 3 commits into from
Aug 18, 2023
Merged

Conversation

uwiger
Copy link
Contributor

@uwiger uwiger commented Aug 17, 2023

Add support for decoding sub-structures.

Example:

serialization_template(ping, ?PING_VSN) ->
    [ {versions, [#{items => [ {protocol, binary}
                             , {vsns, [int]}]
                   }]
      }
    , {port, int}
...

Previously, the template supported only [type()], denoting a list where all elements are of the same type. This made it tricky to extend the list pattern. Also, complex subtypes aren't necessarily that descriptive:

capabilities_template(<<"chain_poi">>, ?PING_VSN) ->
    [{chain_poi, #{ items => [ {height, int}
                             , {root_hash, binary}
                             , {genesis, binary}
                             , {top, binary}
                             , {poi, binary} ]}
     }].

The serialization would look like this:

(aeternity@localhost)1> Cs = [{chain_poi,[#{height => 1000, root_hash => <<"rx">>, genesis => <<"gx">>, top => <<"tx">>, poi => <<"px">>}]}].
[{chain_poi,[#{genesis => <<"gx">>,height => 1000,poi => <<"px">>,
               root_hash => <<"rx">>,top => <<"tx">>}]}]
(aeternity@localhost)2> aec_peer_messages:serialize_capabilities(2,Cs).
<<221,220,137,99,104,97,105,110,95,112,111,105,145,208,
  207,130,3,232,130,114,120,130,103,120,130,116,120,130,
  112,...>>
(aeternity@localhost)3> aeser_rlp:decode(v(2)).
[[<<"chain_poi">>,
  <<208,207,130,3,232,130,114,120,130,103,120,130,116,120,
    130,112,120>>]]
(aeternity@localhost)4> [[_,Sub]] = v(3).
[[<<"chain_poi">>,
  <<208,207,130,3,232,130,114,120,130,103,120,130,116,120,
    130,112,120>>]]
(aeternity@localhost)5> aeser_rlp:decode(Sub).
[[<<3,232>>,<<"rx">>,<<"gx">>,<<"tx">>,<<"px">>]]

src/aeserialization.erl Outdated Show resolved Hide resolved
src/aeserialization.erl Outdated Show resolved Hide resolved
uwiger and others added 2 commits August 17, 2023 20:42
From code review

Co-authored-by: Radosław Rowicki <35342116+radrow@users.noreply.github.com>
From code review

Co-authored-by: Radosław Rowicki <35342116+radrow@users.noreply.github.com>
@uwiger uwiger merged commit 270ebec into master Aug 18, 2023
@uwiger uwiger deleted the uw-encode-maps branch August 18, 2023 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants