emqx_utils_conv.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2017-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_utils_conv).
  17. -export([bin/1]).
  18. -export([str/1]).
  19. -export([bool/1]).
  20. -export([int/1]).
  21. -export([float/1]).
  22. -compile({no_auto_import, [float/1]}).
  23. -type scalar() :: binary() | number() | atom() | string().
  24. -spec bin(Term) -> binary() when
  25. Term :: scalar() | #{scalar() => Term} | [Term].
  26. bin(Bin) when is_binary(Bin) -> Bin;
  27. bin(Num) when is_number(Num) -> number_to_binary(Num);
  28. bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
  29. bin(Map) when is_map(Map) -> emqx_utils_json:encode(Map);
  30. bin(List) when is_list(List) ->
  31. case io_lib:printable_list(List) of
  32. true -> list_to_binary(List);
  33. false -> emqx_utils_json:encode(List)
  34. end;
  35. bin(Data) ->
  36. error({invalid_bin, Data}).
  37. -spec str(Term) -> string() when
  38. Term :: scalar() | #{scalar() => Term} | [Term].
  39. str(Bin) when is_binary(Bin) -> binary_to_list(Bin);
  40. str(Num) when is_number(Num) -> number_to_list(Num);
  41. str(Atom) when is_atom(Atom) -> atom_to_list(Atom);
  42. str(Map) when is_map(Map) -> binary_to_list(emqx_utils_json:encode(Map));
  43. str(List) when is_list(List) ->
  44. case io_lib:printable_list(List) of
  45. true -> List;
  46. false -> binary_to_list(emqx_utils_json:encode(List))
  47. end;
  48. str(Data) ->
  49. error({invalid_str, Data}).
  50. -spec number_to_binary(number()) -> binary().
  51. number_to_binary(Int) when is_integer(Int) ->
  52. integer_to_binary(Int);
  53. number_to_binary(Float) when is_float(Float) ->
  54. float_to_binary(Float, [{decimals, 10}, compact]).
  55. -spec number_to_list(number()) -> string().
  56. number_to_list(Int) when is_integer(Int) ->
  57. integer_to_list(Int);
  58. number_to_list(Float) when is_float(Float) ->
  59. float_to_list(Float, [{decimals, 10}, compact]).
  60. -spec bool(Term) -> boolean() when
  61. Term :: boolean() | binary() | 0..1.
  62. bool(true) -> true;
  63. bool(<<"true">>) -> true;
  64. bool(N) when N == 1 -> true;
  65. bool(false) -> false;
  66. bool(<<"false">>) -> false;
  67. bool(N) when N == 0 -> false;
  68. bool(Data) -> error(badarg, [Data]).
  69. -spec int(Term) -> integer() when
  70. Term :: binary() | string() | number() | boolean().
  71. int(List) when is_list(List) ->
  72. try
  73. list_to_integer(List)
  74. catch
  75. error:badarg ->
  76. int(list_to_float(List))
  77. end;
  78. int(Bin) when is_binary(Bin) ->
  79. try
  80. binary_to_integer(Bin)
  81. catch
  82. error:badarg ->
  83. int(binary_to_float(Bin))
  84. end;
  85. int(Int) when is_integer(Int) ->
  86. Int;
  87. int(Float) when is_float(Float) ->
  88. erlang:floor(Float);
  89. int(true) ->
  90. 1;
  91. int(false) ->
  92. 0;
  93. int(Data) ->
  94. error(badarg, [Data]).
  95. -spec float(Term) -> float() when
  96. Term :: binary() | string() | number().
  97. float(List) when is_list(List) ->
  98. try
  99. list_to_float(List)
  100. catch
  101. error:badarg ->
  102. float(list_to_integer(List))
  103. end;
  104. float(Bin) when is_binary(Bin) ->
  105. try
  106. binary_to_float(Bin)
  107. catch
  108. error:badarg ->
  109. float(binary_to_integer(Bin))
  110. end;
  111. float(Num) when is_number(Num) ->
  112. erlang:float(Num);
  113. float(Data) ->
  114. error(badarg, [Data]).