emqx_jsonish.erl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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_jsonish).
  17. -behaviour(emqx_template).
  18. -export([lookup/2]).
  19. -export_type([t/0]).
  20. %% @doc Either a map or a JSON serial.
  21. %% Think of it as a kind of lazily parsed and/or constructed JSON.
  22. -type t() :: propmap() | serial().
  23. %% @doc JSON in serialized form.
  24. -type serial() :: binary().
  25. -type propmap() :: #{prop() => value()}.
  26. -type prop() :: atom() | binary().
  27. -type value() :: scalar() | [scalar() | propmap()] | t().
  28. -type scalar() :: atom() | unicode:chardata() | number().
  29. %%
  30. %% @doc Lookup a value in the JSON-ish map accessible through the given accessor.
  31. %% If accessor implies drilling down into a binary, it will be treated as JSON serial.
  32. %% Failure to parse the binary as JSON will result in an _invalid type_ error.
  33. %% Nested JSON is NOT parsed recursively.
  34. -spec lookup(emqx_template:accessor(), t()) ->
  35. {ok, value()}
  36. | {error, undefined | {_Location :: non_neg_integer(), _InvalidType :: atom()}}.
  37. lookup(Var, Jsonish) ->
  38. lookup(0, _Decoded = false, Var, Jsonish).
  39. lookup(_, _, [], Value) ->
  40. {ok, Value};
  41. lookup(Loc, Decoded, [Prop | Rest], Jsonish) when is_map(Jsonish) ->
  42. case emqx_template:lookup(Prop, Jsonish) of
  43. {ok, Value} ->
  44. lookup(Loc + 1, Decoded, Rest, Value);
  45. {error, Reason} ->
  46. {error, Reason}
  47. end;
  48. lookup(Loc, _Decoded = false, Props, Json) when is_binary(Json) ->
  49. try emqx_utils_json:decode(Json) of
  50. Value ->
  51. % NOTE: This is intentional, we don't want to parse nested JSON.
  52. lookup(Loc, true, Props, Value)
  53. catch
  54. error:_ ->
  55. {error, {Loc, binary}}
  56. end;
  57. lookup(Loc, _, _, Invalid) ->
  58. {error, {Loc, type_name(Invalid)}}.
  59. type_name(Term) when is_atom(Term) -> atom;
  60. type_name(Term) when is_number(Term) -> number;
  61. type_name(Term) when is_binary(Term) -> binary;
  62. type_name(Term) when is_list(Term) -> list.