PageRenderTime 153ms CodeModel.GetById 41ms RepoModel.GetById 28ms app.codeStats 0ms

/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. -include("uce.hrl").
  20. -export([to_bson/1, ok/1, updated/1, collection_to_list/1]).
  21. -include_lib("eunit/include/eunit.hrl").
  22. to_bson([]) ->
  23. [];
  24. to_bson({struct, Proplist}) ->
  25. to_bson(Proplist, []);
  26. to_bson({array, Values}) ->
  27. {array, to_bson(Values, [])};
  28. to_bson({Name, {struct, Values}}) ->
  29. {Name, to_bson(Values, [])};
  30. to_bson({Name, {array, Values}}) ->
  31. {Name, {array, to_bson(Values, [])}};
  32. to_bson({Name, Value}) ->
  33. {Name, Value};
  34. to_bson(Value) ->
  35. Value.
  36. to_bson([Value|Values], Acc) ->
  37. to_bson(Values, [to_bson(Value)|Acc]);
  38. to_bson([], Acc) ->
  39. lists:reverse(Acc).
  40. ok([Result]) ->
  41. case proplists:lookup(<<"err">>, Result) of
  42. {<<"err">>, null} ->
  43. ok;
  44. {<<"err">>, Err} ->
  45. ?ERROR_MSG("mongodb error ~p", [Err]),
  46. error
  47. end.
  48. updated(Result) ->
  49. ok(Result).
  50. collection_member_to_list({array, Value}) when is_list(Value) ->
  51. {array, [collection_member_to_list(Elem) || Elem <- Value]};
  52. collection_member_to_list(Value) when is_tuple(Value) ->
  53. tuple_to_list(Value);
  54. collection_member_to_list(Value) when is_integer(Value) ->
  55. Value;
  56. collection_member_to_list(Value) when is_list(Value) ->
  57. {struct, collection_to_list(Value)};
  58. collection_member_to_list(Value) when is_binary(Value) ->
  59. unicode:characters_to_list(Value);
  60. collection_member_to_list(Value) when is_atom(Value)->
  61. Value.
  62. %%--------------------------------------------------------------------
  63. %% @spec ([{Key::binary, Value::Binary}, {Key::binary, Value::Binary}, ...] = Collection::list) -> [{Key::list, Value::list}, {Key::binary, Value:Binary}, ...] = NewCollection::list
  64. %% @doc Convert list of tuple of two binaries returned by mongodb to list of tuple of 2 list
  65. %% @end
  66. %%--------------------------------------------------------------------
  67. collection_to_list(Collection) ->
  68. lists:map(fun({Key, Value}) ->
  69. {collection_member_to_list(Key), collection_member_to_list(Value)}
  70. end,
  71. Collection).
  72. -ifdef(TEST).
  73. -include_lib("eunit/include/eunit.hrl").
  74. collection_to_list_test() ->
  75. ?assertEqual([{"id", "42"}], collection_to_list([{<<"id">>,<<"42">>}])),
  76. ?assertEqual([{"nickname", ""}], collection_to_list([{<<"nickname">>,<<>>}])),
  77. ?assertEqual([{"_id", [oid, <<"root">>]}], collection_to_list([{<<"_id">>,{oid,<<"root">>}}])).
  78. to_bson_test() ->
  79. ?assertEqual([],
  80. to_bson([])),
  81. ?assertEqual([{"name", "plop"}],
  82. to_bson({struct, [{"name", "plop"}]})),
  83. ?assertEqual({array, [{"name", "plop"}]},
  84. to_bson({array, [{"name", "plop"}]})),
  85. ?assertEqual({array, [10, [{"name", "plop"}]]},
  86. to_bson({array, [10, {struct, [{"name", "plop"}]}]})),
  87. ?assertEqual([{"name", {array, ["plop", "plip"]}}],
  88. to_bson({struct, [{"name", {array, ["plop", "plip"]}}]})),
  89. ?assertEqual([{"name", [{"plop", "plip"}]}],
  90. to_bson({struct, [{"name", {struct, [{"plop", "plip"}]}}]})),
  91. ?assertEqual([{"name", [{"plop", {array, ["plip", "plaf"]}}]}],
  92. to_bson({struct, [{"name", {struct, [{"plop", {array, ["plip", "plaf"]}}]}}]})),
  93. ?assertEqual({array, [10, [{"name", "plip"}]]},
  94. to_bson({array, [10, {struct, [{"name", "plip"}]}]})),
  95. ?assertEqual([{"complex", {array, [10, [{"name", "plip"}]]}}],
  96. to_bson({struct, [{"complex", {array, [10, {struct, [{"name", "plip"}]}]}}]})),
  97. ?assertEqual([{"score", null}],
  98. to_bson({struct, [{"score", null}]})),
  99. ?assertEqual([{"score", true}],
  100. to_bson({struct, [{"score", true}]})),
  101. ?assertEqual([{"score", false}],
  102. to_bson({struct, [{"score", false}]})).
  103. -endif.