[erlang-questions] Inheriting functions that use record atoms

3 views
Skip to first unread message

Tristan Sloughter

unread,
Jul 9, 2011, 9:28:03 AM7/9/11
to erlang-q...@erlang.org
I will have a number of modules that define a single record and functions for using that record as a model stored in a database for a webapp.

There are these nice macros for converting a record to json and json to a record that will basically always look like:

to_json(Record) ->
    ?record_to_json(?MODULE, Record).

to_record(JSON) ->
    ?json_to_record(?MODULE, JSON).

But at times they'll need to be overridden for a specific module/record.

My problem is I'd like to not have to copy/paste these into every module (and they'll be more base functions like these) that is a model. extends would be best since that allows overriding but from the base module I can't reference the module that is inheriting the base module as a macro, so I can't use the json_to_record macro since I can't pass in a macro of the record atom name.

A parse transform won't work either since I don't think I can add a call to a macro like I need.

Does anyone have any suggestions? Or should I just not use records for this? Records are nice since I'm using mnesia and found those record_to json_to macros!

Thanks,
Tristan

Kenny Stone

unread,
Jul 9, 2011, 12:55:59 PM7/9/11
to Tristan Sloughter, erlang-q...@erlang.org
Records don't play nice with other languages.  I have a similar situation that I haven't tackled yet with my mnesia store, but I solved it with my ets store by doing something like:

{objtype, objid, ObjPropList, JSON}

You wouldn't have to keep a copy of the JSON around, but I do because ets is my last value cache for a web server so I might as well save the serialization step.  I imagine in mnesia it could be similar, or you could do something more generic

-record(mdoc, {objid, objtype, obj=[], json}).  % or something

Your record doesn't represent your object at all, it's just something that mnesia can deal with.  The real object is just a proplist, which is how JSON usually deserializes into Erlang.  

-Kenny

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions


Reply all
Reply to author
Forward
0 new messages