PageRenderTime 571ms CodeModel.GetById 137ms app.highlight 19ms RepoModel.GetById 152ms app.codeStats 131ms

/ucengine/src/backends/db/mongodb/mongodb_helpers.erl

http://github.com/AF83/ucengine
Erlang | 130 lines | 81 code | 27 blank | 22 comment | 1 complexity | b46667bdf6a6005e400d5c3f4dec443c MD5 | raw file
  1%%
  2%%  U.C.Engine - Unified Collaboration Engine
  3%%  Copyright (C) 2011 af83
  4%%
  5%%  This program is free software: you can redistribute it and/or modify
  6%%  it under the terms of the GNU Affero General Public License as published by
  7%%  the Free Software Foundation, either version 3 of the License, or
  8%%  (at your option) any later version.
  9%%
 10%%  This program is distributed in the hope that it will be useful,
 11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13%%  GNU Affero General Public License for more details.
 14%%
 15%%  You should have received a copy of the GNU Affero General Public License
 16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17%%
 18-module(mongodb_helpers).
 19
 20-include("uce.hrl").
 21
 22-export([to_bson/1, ok/1, updated/1, collection_to_list/1]).
 23
 24-include_lib("eunit/include/eunit.hrl").
 25
 26to_bson([]) ->
 27    [];
 28to_bson({struct, Proplist}) ->
 29    to_bson(Proplist, []);
 30to_bson({array, Values}) ->
 31    {array, to_bson(Values, [])};
 32to_bson({Name, {struct, Values}}) ->
 33    {Name, to_bson(Values, [])};
 34to_bson({Name, {array, Values}}) ->
 35    {Name, {array, to_bson(Values, [])}};
 36to_bson({Name, Value}) ->
 37    {Name, Value};
 38to_bson(Value) ->
 39    Value.
 40
 41to_bson([Value|Values], Acc) ->
 42    to_bson(Values, [to_bson(Value)|Acc]);
 43to_bson([], Acc) ->
 44    lists:reverse(Acc).
 45
 46ok([Result]) ->
 47    case proplists:lookup(<<"err">>, Result) of
 48        {<<"err">>, null} ->
 49            ok;
 50        {<<"err">>, Err} ->
 51            ?ERROR_MSG("mongodb error ~p", [Err]),
 52            error
 53    end.
 54
 55updated(Result) ->
 56    ok(Result).
 57
 58collection_member_to_list({array, Value}) when is_list(Value) ->
 59    {array, [collection_member_to_list(Elem) || Elem <- Value]};
 60
 61collection_member_to_list(Value) when is_tuple(Value) ->
 62    tuple_to_list(Value);
 63
 64collection_member_to_list(Value) when is_integer(Value) ->
 65    Value;
 66
 67collection_member_to_list(Value) when is_list(Value) ->
 68    {struct, collection_to_list(Value)};
 69
 70collection_member_to_list(Value) when is_binary(Value) ->
 71    unicode:characters_to_list(Value);
 72
 73collection_member_to_list(Value) when is_atom(Value)->
 74    Value.
 75
 76%%--------------------------------------------------------------------
 77%% @spec ([{Key::binary, Value::Binary}, {Key::binary, Value::Binary}, ...] = Collection::list) -> [{Key::list, Value::list}, {Key::binary, Value:Binary}, ...] = NewCollection::list
 78%% @doc Convert list of tuple of two binaries returned by mongodb to list of tuple of 2 list
 79%% @end
 80%%--------------------------------------------------------------------
 81collection_to_list(Collection) ->
 82    lists:map(fun({Key, Value}) ->
 83                      {collection_member_to_list(Key), collection_member_to_list(Value)}
 84              end,
 85              Collection).
 86
 87-ifdef(TEST).
 88-include_lib("eunit/include/eunit.hrl").
 89
 90collection_to_list_test() ->
 91    ?assertEqual([{"id", "42"}], collection_to_list([{<<"id">>,<<"42">>}])),
 92    ?assertEqual([{"nickname", ""}], collection_to_list([{<<"nickname">>,<<>>}])),
 93    ?assertEqual([{"_id", [oid, <<"root">>]}], collection_to_list([{<<"_id">>,{oid,<<"root">>}}])).
 94
 95to_bson_test() ->
 96    ?assertEqual([],
 97                 to_bson([])),
 98
 99    ?assertEqual([{"name", "plop"}],
100                 to_bson({struct, [{"name", "plop"}]})),
101
102    ?assertEqual({array, [{"name", "plop"}]},
103                 to_bson({array, [{"name", "plop"}]})),
104
105    ?assertEqual({array, [10, [{"name", "plop"}]]},
106                 to_bson({array, [10, {struct, [{"name", "plop"}]}]})),
107
108    ?assertEqual([{"name", {array, ["plop", "plip"]}}],
109                 to_bson({struct, [{"name", {array, ["plop", "plip"]}}]})),
110
111    ?assertEqual([{"name", [{"plop", "plip"}]}],
112                 to_bson({struct, [{"name", {struct, [{"plop", "plip"}]}}]})),
113
114    ?assertEqual([{"name", [{"plop", {array, ["plip", "plaf"]}}]}],
115                 to_bson({struct, [{"name", {struct, [{"plop", {array, ["plip", "plaf"]}}]}}]})),
116
117    ?assertEqual({array, [10, [{"name", "plip"}]]},
118                 to_bson({array, [10, {struct, [{"name", "plip"}]}]})),
119
120    ?assertEqual([{"complex", {array, [10, [{"name", "plip"}]]}}],
121                 to_bson({struct, [{"complex", {array, [10, {struct, [{"name", "plip"}]}]}}]})),
122
123    ?assertEqual([{"score", null}],
124                 to_bson({struct, [{"score", null}]})),
125    ?assertEqual([{"score", true}],
126                 to_bson({struct, [{"score", true}]})),
127    ?assertEqual([{"score", false}],
128                 to_bson({struct, [{"score", false}]})).
129
130-endif.