PageRenderTime 72ms CodeModel.GetById 16ms RepoModel.GetById 3ms app.codeStats 0ms

/src/mochilists.erl

http://github.com/basho/mochiweb
Erlang | 122 lines | 67 code | 15 blank | 40 comment | 0 complexity | 1cb14eab14a7253272827c7d36f449ff MD5 | raw file
Possible License(s): MIT
  1. %% @copyright Copyright (c) 2010 Mochi Media, Inc.
  2. %% @author David Reid <dreid@mochimedia.com>
  3. %%
  4. %% Permission is hereby granted, free of charge, to any person obtaining a
  5. %% copy of this software and associated documentation files (the "Software"),
  6. %% to deal in the Software without restriction, including without limitation
  7. %% the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. %% and/or sell copies of the Software, and to permit persons to whom the
  9. %% Software is furnished to do so, subject to the following conditions:
  10. %%
  11. %% The above copyright notice and this permission notice shall be included in
  12. %% all copies or substantial portions of the Software.
  13. %%
  14. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. %% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. %% DEALINGS IN THE SOFTWARE.
  21. %% @doc Utility functions for dealing with proplists.
  22. -module(mochilists).
  23. -author("David Reid <dreid@mochimedia.com>").
  24. -export([get_value/2, get_value/3, is_defined/2, set_default/2, set_defaults/2]).
  25. %% @spec set_default({Key::term(), Value::term()}, Proplist::list()) -> list()
  26. %%
  27. %% @doc Return new Proplist with {Key, Value} set if not is_defined(Key, Proplist).
  28. set_default({Key, Value}, Proplist) ->
  29. case is_defined(Key, Proplist) of
  30. true ->
  31. Proplist;
  32. false ->
  33. [{Key, Value} | Proplist]
  34. end.
  35. %% @spec set_defaults([{Key::term(), Value::term()}], Proplist::list()) -> list()
  36. %%
  37. %% @doc Return new Proplist with {Key, Value} set if not is_defined(Key, Proplist).
  38. set_defaults(DefaultProps, Proplist) ->
  39. lists:foldl(fun set_default/2, Proplist, DefaultProps).
  40. %% @spec is_defined(Key::term(), Proplist::list()) -> bool()
  41. %%
  42. %% @doc Returns true if Propist contains at least one entry associated
  43. %% with Key, otherwise false is returned.
  44. is_defined(Key, Proplist) ->
  45. lists:keyfind(Key, 1, Proplist) =/= false.
  46. %% @spec get_value(Key::term(), Proplist::list()) -> term() | undefined
  47. %%
  48. %% @doc Return the value of <code>Key</code> or undefined
  49. get_value(Key, Proplist) ->
  50. get_value(Key, Proplist, undefined).
  51. %% @spec get_value(Key::term(), Proplist::list(), Default::term()) -> term()
  52. %%
  53. %% @doc Return the value of <code>Key</code> or <code>Default</code>
  54. get_value(_Key, [], Default) ->
  55. Default;
  56. get_value(Key, Proplist, Default) ->
  57. case lists:keyfind(Key, 1, Proplist) of
  58. false ->
  59. Default;
  60. {Key, Value} ->
  61. Value
  62. end.
  63. %%
  64. %% Tests
  65. %%
  66. -ifdef(TEST).
  67. -include_lib("eunit/include/eunit.hrl").
  68. set_defaults_test() ->
  69. ?assertEqual(
  70. [{k, v}],
  71. set_defaults([{k, v}], [])),
  72. ?assertEqual(
  73. [{k, v}],
  74. set_defaults([{k, vee}], [{k, v}])),
  75. ?assertEqual(
  76. lists:sort([{kay, vee}, {k, v}]),
  77. lists:sort(set_defaults([{k, vee}, {kay, vee}], [{k, v}]))),
  78. ok.
  79. set_default_test() ->
  80. ?assertEqual(
  81. [{k, v}],
  82. set_default({k, v}, [])),
  83. ?assertEqual(
  84. [{k, v}],
  85. set_default({k, vee}, [{k, v}])),
  86. ok.
  87. get_value_test() ->
  88. ?assertEqual(
  89. undefined,
  90. get_value(foo, [])),
  91. ?assertEqual(
  92. undefined,
  93. get_value(foo, [{bar, baz}])),
  94. ?assertEqual(
  95. bar,
  96. get_value(foo, [{foo, bar}])),
  97. ?assertEqual(
  98. default,
  99. get_value(foo, [], default)),
  100. ?assertEqual(
  101. default,
  102. get_value(foo, [{bar, baz}], default)),
  103. ?assertEqual(
  104. bar,
  105. get_value(foo, [{foo, bar}], default)),
  106. ok.
  107. -endif.