nsx_opt.erl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. %%----------------------------------------------------------------------
  2. %% @author Vladimir Baranov <baranoff.vladimir@gmail.com>
  3. %% @copyright Paynet Internet ve Bilisim Hizmetleri A.S. All Rights Reserved.
  4. %% @doc nsm_mq routines
  5. %% @end
  6. %%--------------------------------------------------------------------
  7. -module(nsx_opt).
  8. %%
  9. %% Include files
  10. %%
  11. %%
  12. %% Exported Functions
  13. %%
  14. -export([
  15. encode/1,
  16. decode/1
  17. ]).
  18. -export([opt/3,
  19. override_opt/3,
  20. get_env/2,
  21. get_env/3]).
  22. %%
  23. %% API Functions
  24. %%
  25. encode(Term) ->
  26. term_to_binary(Term).
  27. decode(Binary) when is_binary(Binary) ->
  28. binary_to_term(Binary).
  29. %% Get option from list. Option can be either single option, like 'name', or
  30. %% tuple {name, "Name"}. Single options are boolean, but return value can be
  31. %% overwritten with default value. If option not found in options List - returns
  32. %% default value.
  33. opt(Option, List, Default) ->
  34. case lists:member(Option, List) of
  35. true ->
  36. true;
  37. false ->
  38. proplists:get_value(Option, List, Default)
  39. end.
  40. -spec override_opt(atom(), any(), list()) -> list().
  41. override_opt(Option, Value, Options) ->
  42. CleanOptions = lists:filter(
  43. fun({O, _}) when O == Option -> false;
  44. (O) when O == Option -> false;
  45. (_) -> true
  46. end, Options),
  47. [{Option, Value} | CleanOptions].
  48. %% @doc Get env options from application config. If parameter not set - default
  49. %% value will be returned
  50. -spec get_env(Par::atom(), Default::term()) -> term().
  51. get_env(Par, DefaultValue) ->
  52. case application:get_env(Par) of
  53. undefined ->
  54. DefaultValue;
  55. {ok, Value} ->
  56. Value
  57. end.
  58. get_env(App, Par, DefaultValue) ->
  59. case application:get_env(App, Par) of
  60. undefined ->
  61. DefaultValue;
  62. {ok, Value} ->
  63. Value
  64. end.