Emit Tuples From Erlang Views In CouchDB

家住魔仙堡 提交于 2019-11-30 09:10:45
Jan Lehnardt

The JSON object {"foo":"bar","baz":1} is {[{<<"foo">>,<<"bar">>},{<<"baz">>,1}]}

In Erlang lingua it is a proplist wrapped in a tuple.

It's not pretty, but very efficient :)

To get a feel for it you can play with the JSON lib that ships with CouchDB:

  1. Start CouchDB with the -i (interactive) flag
  2. On the resulting erlang shell, type: couch_util:json_decode(<<"{\"foo\":\"bar\"}">>).
  3. Profit

// in later versions of CouchDB, this is ejson:decode()

Denis

For test_suite_reports bd, that has tests field:

[
   {
       "name": "basics",
       "status": "success",
       "duration": 21795
   },
   {
       "name": "all_docs",
       "status": "success",
       "duration": 385
   } ...

I have wrote this to get name and status:

fun({Doc}) ->
  Name = fun(L) ->  proplists:get_value(<<"name">>, L, null) end,
  Status = fun(L) -> proplists:get_value(<<"status">>, L, null) end,
  Tests = proplists:get_value(<<"tests">>, Doc, null),
  lists:foreach(fun({L}) -> Emit(Name(L), Status(L)) end, Tests)
end.

If you like experimental features (that still work...), you might want to have a look to Erlang exprecs.

I found it extremely helpful in creating a sort of dynamic records for Erlang.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!