Add a function converting message to binary-key map (#2360)
This commit is contained in:
parent
8d92d58b6c
commit
f0fa9a9252
|
@ -44,6 +44,8 @@
|
|||
|
||||
-export([ to_map/1
|
||||
, to_list/1
|
||||
, to_bin_key_map/1
|
||||
, to_bin_key_list/1
|
||||
]).
|
||||
|
||||
-export([format/1]).
|
||||
|
@ -157,11 +159,24 @@ update_expiry(Msg) -> Msg.
|
|||
to_map(Msg) ->
|
||||
maps:from_list(to_list(Msg)).
|
||||
|
||||
%% @doc Message to map
|
||||
-spec(to_bin_key_map(emqx_types:message()) -> #{binary() => any()}).
|
||||
to_bin_key_map(Msg) ->
|
||||
maps:from_list(to_bin_key_list(Msg)).
|
||||
|
||||
%% @doc Message to tuple list
|
||||
-spec(to_list(emqx_types:message()) -> map()).
|
||||
to_list(Msg) ->
|
||||
lists:zip(record_info(fields, message), tl(tuple_to_list(Msg))).
|
||||
|
||||
%% @doc Message to tuple list
|
||||
-spec(to_bin_key_list(emqx_types:message()) -> map()).
|
||||
to_bin_key_list(Msg) ->
|
||||
lists:zipwith(
|
||||
fun(Key, Val) ->
|
||||
{bin(Key), Val}
|
||||
end, record_info(fields, message), tl(tuple_to_list(Msg))).
|
||||
|
||||
%% MilliSeconds
|
||||
elapsed(Since) ->
|
||||
max(0, timer:now_diff(os:timestamp(), Since) div 1000).
|
||||
|
@ -177,3 +192,6 @@ format(flags, Flags) ->
|
|||
format(headers, Headers) ->
|
||||
io_lib:format("~p", [Headers]).
|
||||
|
||||
bin(Bin) when is_binary(Bin) -> Bin;
|
||||
bin(Atom) when is_atom(Atom) -> list_to_binary(atom_to_list(Atom));
|
||||
bin(Str) when is_list(Str) -> list_to_binary(Str).
|
Loading…
Reference in New Issue