3

The following allows to convert a tuple or object back to an object in erlang:

{ok, Tokens, _} = erl_scan:string("{'abc',123}."),
{ok, X} = erl_parse:parse_term(Tokens).

But when you have a record represented as a string, such as:

-record(myrecord,{firstname,lastname,age}).
...
RecString = "#myrecord{firstname='john',lastname='doe',age=22}.",
{ok, Tokens, _} = erl_scan:string(RecString),
{ok, X} = erl_parse:parse_term(Tokens).

... the above will fail with the message:

** exception error: no match of right hand side value {error,{1,erl_parse,["syntax error before: ",[]]}}

Thoughts on how to achieve that? Thanks.

2240
  • 1,547
  • 2
  • 12
  • 30
gextra
  • 8,439
  • 8
  • 40
  • 62

1 Answers1

6

First you must remember that a record does not exist as a data-type, internally records are tuples where the first element is the name of the record. So with your record definition:

-record(myrecord,{firstname,lastname,age}).

the creating the record with

#myrecord{firstname='john',lastname='doe',age=22}

would result in the tuple

{myrecord,john,doe,22}

which just contains the actual data. This is how records are defined, see here.

Second point is that records are purely compile-time syntactic constructions which compiler transforms in tuple operations. So the definition of a record does not exist as such as data anywhere. Only the compiler knows of record definitions. So when you print a record all you see is the tuple. However you can define record inside the shell so you can use record syntax in the shell, see in the shell documentation.

So in this sense you cannot really convert a record to/from its string representation. You can parse the string but this only returns the abstract syntax which is not what you are after. They are expressions so you need to end the string with a . and use erl_parse:exprs/1.

Hope this helps. What are you trying to do? Or rather why are you trying to do it?

rvirding
  • 20,848
  • 2
  • 37
  • 56
  • Thanks for the clarification. I actually did have the dot after the string representation, but forgot to write in the example above. It still did not parse. Let me explain... – gextra Feb 17 '13 at 22:22
  • ...the string does not come from a record itself, it was constructed on a Java application from a Java object. I created a Java method "toErlangRecord()" that converts a Java object's getter method into an Erlang record. The string is such as#rec{x='a'}. This is then taken from an Erlang process as a message then the idea is to build a record. I am trying to use a record as a schema, instead of json or xml. – gextra Feb 17 '13 at 22:51
  • 1
    What you are describing is, to the best of my knowledge, impossible. See http://stackoverflow.com/questions/4132554/java-to-erlang-messages or pick json, bson, xml, msgpack... – macintux Feb 17 '13 at 23:28
  • Just to conclude my findings based on the good suggestions above, I chose to adopt a **dict** type of proplist considering that it offers a native (lists of tuples) approach and the module allows to access the data by key in an easy way. A better option would be **orddict** but to construct it from another project the keys must be defined in ascending order, which is a not so flexible from a non-erlang environment. More details on **dict** or **orddict** here [link](http://www.erlang.org/doc/man/dict.html) – gextra Feb 18 '13 at 17:14