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

Generic Codec #68

Open
fogfish opened this issue Apr 24, 2019 · 0 comments
Open

Generic Codec #68

fogfish opened this issue Apr 24, 2019 · 0 comments

Comments

@fogfish
Copy link
Owner

fogfish commented Apr 24, 2019

We do have a ADT - Erlang record

-record(adt, {id, title, ...}).

external serialization works with maps

jsx:encode(#{<<"id">> => ..., <<"title">> => ...})

How to automate process of ADT to maps and back transformation

-ifndef(decode).
-define(decode(Type, Struct),
   list_to_tuple([Type | 
      [lens:get(lens:at(typecast:s(X)), Struct) || X <- record_info(fields, Type)]]
   )
).
-endif.

-ifndef(encode).
-define(encode(Type, Struct),
   maps:from_list(
      [{typecast:s(Key), Value} ||
         {Key, Value} <- lists:zip(
            record_info(fields, Type),
            tl(tuple_to_list(Struct))
         ),
         Value /= undefined,
         Value /= null
      ]
   )
).
-endif.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant