Skip to content

Instantly share code, notes, and snippets.

@stephb9959
Forked from gdamjan/record_to_list.hrl
Created August 22, 2019 04:52
Show Gist options
  • Save stephb9959/f4fb98caeaea76b7acfffd63a940939b to your computer and use it in GitHub Desktop.
Save stephb9959/f4fb98caeaea76b7acfffd63a940939b to your computer and use it in GitHub Desktop.
An erlang macro that creates a function to convert a record to a key-value list
%%% This macro will create a function that converts a record to
%%% a {key, value} list (a proplist)
-define(record_to_list(Record),
fun(Val) ->
Fields = record_info(fields, Record),
[_Tag| Values] = tuple_to_list(Val),
lists:zip(Fields, Values)
end
).
-record(rec, {bar = "baz", camp = "spam"}).
test() ->
Record = #rec{bar = "qux"},
Fun = ?make_record_to_list(rec),
Fun(Record).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment