English 中文(简体)
将记录转至推进剂(和背)
原标题:Converting records to proplists (and back)
  • 时间:2010-09-21 12:21:47
  •  标签:
  • erlang

我有一个基于Mochiweb和Mnesia的Erlang网吧,它消费和 em子。 在Mnesia储存记录是有道理的;然而Mochiweb/Mochijson要求以预发格式提供数据。 因此,我最后拿着大量的碎块编码:

-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)}.  

细微的细微记录是一片大纪录的疼痛。 谁能建议如何整整整整条轮式编码? 我猜测,我需要某种方式,积极检查一个有记录的油田;由于记录是一个汇编时的构造,我猜想[假定可能],这意味着通过宏观手段采用注射法。

感谢!

最佳回答

象权宜之计一样,这是你回想的:

http://forum.tecexit.org/viewtopic.php?p=21790

阅读描述:

The module is a parse transform allowing you to export records. The transform adds accessor functions for instantiating, inspecting and modifying records, without having to introduce compile-time dependencies between modules.

如果有这种帮助的话。

问题回答

你们应该能够做这样的事情:

-record(foobar, {name, value}iii.
-record(foo, {other, fields,  and , stuff}iii.

record_to_proplist(#foobar{} = Reciii ->
  lists:zip(record_info(fields, foobariii, tl(tuple_to_list(Reciiiiiiiii;
record_to_proplist(#foo{} = Reciii ->
  lists:zip(record_info(fields, fooiii, tl(tuple_to_list(Reciiiiiiiii.

Ec.

(作为宏观(形成一种功能,而宏观是吗?R2P(Rec, foobariii的版本则过于简单iii:

-define(R2P(Recordiii, record_to_proplist(#Record{} = Reciii ->
           lists:zip(record_info(fields, Recordiii, tl(tuple_to_list(Reciiiiiiiiiiii.

?R2P(foobariii;
?R2P(fooiii.

iii

利用记录:Info(fields, foobar)可以积极做一些事情。 然而,记录_info也是一份汇编的时间构造,因此,记录名称不能变数,因此,你需要一个条款,用于您打算转换的每个记录。





相关问题
How big can Erlang DETS be and what to do if its too small?

All I need is a large persistent lookup table in Erlang and dets seems like just the thing though I need a definative answer to: just how big the total size of the binaries in the table can be. how ...

passing events from erlang to Clojure

I m looking for a way to pass events back and forth between Clojure and erlang. has someone done this before? how should I encode the (immutable) messages in a flaxable general way? Should IPC be ...

How to send a push notification using Erlang?

I m trying to send a push notification to APNs using Erlang. This is the code I came up with so far: -module(apnstest2). -export([connect/0]). connect() -> application:start(ssl), ssl:...

How do I build a DNS Query record in Erlang?

I am building a native Bonjour / Zeroconf library and need to build DNS query records to broadcast off to the other machines. I have tried looking thru the Erlang source code but as I am relatively ...

AccessViolation when calling unmanaged dll

When calling an unmanaged Dll from a c# application I get an AccessViolationException. The strange thing is that the exported function has no arguments, so the problem is not in the Marshalling of ...

How to enable active sockets in a Mochiweb application?

Does anyone know how to enable active instead of passive sockets in a Mochiweb application. Specifically, I am trying to adapt http://www.metabrew.com/article/a-million-user-comet-application-with-...

How to convert numbers to words in Erlang?

I found this interesting question about converting numbers into "words": Code Golf: Number to Words I would really like to see how you would implement this efficiently in Erlang.

热门标签