I have a Erlang webapp, based on Mochiweb and Mnesia, which consumes and emits JSON. It makes sense to store records in Mnesia; however Mochiweb/Mochijson require data in proplist format. So I end up with a large amount of boilerplate code:
-record(foobar, {name, value}).
record_to_proplist(Record)->
[{name, Record#foobar.name},
{value, Record#foobar.value}].
proplist_to_record(Props)->
#foobar{name=proplists:get_value("name", Props),
value=proplists:get_value("value", Props)}.
Fine with a single small record, a pain with lots of large records. Can anyone suggest a way round all this boilerplate code ? I guess I need some way of inspecting a record's fields dynamically; and since records are a compile-time construct, I'm guessing [assuming it's possible] this means injecting code via a macro ..
Thanks!