-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathered_messages.erl
More file actions
314 lines (293 loc) · 8.89 KB
/
Copy pathered_messages.erl
File metadata and controls
314 lines (293 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
-module(ered_messages).
-export([
convert_to_num/1,
convert_units_to_milliseconds/2,
create_outgoing_msg/1,
decode_json/1,
delete_prop/2,
encode_json/1,
escape_specials/1,
get_prop/2,
is_same/2,
is_not_same/2,
map_keys_to_binary/1,
map_keys_to_lists/1,
retrieve_prop_value/2,
set_prop_value/3,
timestamp/0,
to_bool/1
]).
%%
%% Helper functions for dealing with messages and their electic needs.
%%
-import(ered_nodes, [
generate_id/0,
jstr/2
]).
%%
%% Used by switch and assert values nodes.
is_same(Same, Diff) when is_list(Same) and is_binary(Diff) ->
is_same(Same, binary_to_list(Diff));
is_same(Same, Diff) when is_binary(Same) and is_list(Diff) ->
is_same(binary_to_list(Same), Diff);
is_same(Same, Same) ->
true;
is_same(_, _) ->
false.
is_not_same(Same, Diff) when is_list(Same) and is_binary(Diff) ->
is_not_same(Same, binary_to_list(Diff));
is_not_same(Same, Diff) when is_binary(Same) and is_list(Diff) ->
is_not_same(binary_to_list(Same), Diff);
is_not_same(Same, Same) ->
false;
is_not_same(_, _) ->
true.
%%
%% convert to num - a num can be a integer or float, so to convert a string
%% to one of these inluding the negativity that is minus.
convert_to_num(V) when is_binary(V) ->
convert_to_num(binary_to_list(V));
convert_to_num(V) when is_number(V) ->
V;
convert_to_num(V) when is_integer(V) ->
V;
convert_to_num(V) when is_list(V) ->
case string:to_float(V) of
{error, _} ->
case string:to_integer(V) of
{error, _} ->
{error,
jstr("WARNING: Value ~p cannot be convereted to num", [
V
])};
{V2, _} ->
V2
end;
{V3, _} ->
V3
end.
%%
%% these are from the trigger node
convert_units_to_milliseconds({ok, <<"hr">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val)) * 1000 * 60 * 60};
convert_units_to_milliseconds({ok, <<"min">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val)) * 1000 * 60};
convert_units_to_milliseconds({ok, <<"s">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val)) * 1000};
convert_units_to_milliseconds({ok, <<"ms">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val))};
%%
%% these are from the delay node
convert_units_to_milliseconds({ok, <<"days">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val)) * 1000 * 60 * 60 * 24};
convert_units_to_milliseconds({ok, <<"hours">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val)) * 1000 * 60 * 60};
convert_units_to_milliseconds({ok, <<"minutes">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val)) * 1000 * 60};
convert_units_to_milliseconds({ok, <<"seconds">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val)) * 1000};
convert_units_to_milliseconds({ok, <<"milliseconds">>}, {ok, Val}) ->
{ok, element(1, string:to_integer(Val))};
convert_units_to_milliseconds(A, B) ->
{error, jstr("WARNING: convert to ms: nomatch for ~p & ~p~n", [A, B])}.
%%
%% When something is a date type, it gets this value.
timestamp() ->
erlang:system_time(millisecond).
%%
%% Decode a json string just like we like it, with atoms as keys
decode_json(Val) when is_list(Val) ->
decode_json(list_to_binary(Val));
decode_json(Val) ->
json:decode(Val).
%%
%%
%% This is json:encode except that Pids are converted to strings. Used for
%% the contents of debug messages - that may certainly contain a Pid or two.
%% Tuples are also a foe of JSON - convert them to lists.
%%
encoder({K, V}, Encode) ->
json:encode_value([K, V], Encode);
encoder([{_, _} | _] = Value, Encode) ->
json:encode_key_value_list(Value, Encode);
encoder(Other, Encode) when is_tuple(Other) ->
json:encode_value(tuple_to_list(Other), Encode);
encoder(Other, Encode) when is_reference(Other) ->
json:encode_value(list_to_binary(ref_to_list(Other)), Encode);
encoder(Other, Encode) when is_pid(Other) ->
json:encode_value(list_to_binary(pid_to_list(Other)), Encode);
encoder(Other, Encode) ->
json:encode_value(Other, Encode).
%%
encode_json(Value2) ->
json:encode(Value2, fun(Value, Encode) -> encoder(Value, Encode) end).
%%
%% get prop can used to retrieve a nestd value from the Msg map, i.e.,
%% msg.payload.key1.key2.key3
%% from this:
%% #{ paylad => #{ key1 => #{ key2 => key3 => <<"this is the value">> }}}
%% gives
%% <<"this is the value">>
%%
%% designed to be used with a maps:find, e.g.
%%
%% get_prop(maps:find(p,Rule), Msg)
%%
%% retuns
%%
%% {ok, Value, Prop}
%% or
%% {undefined, Prop}
%% or
%% {error, ErrorMsg}
%%
get_prop({ok, Prop}, Msg) ->
case erl_attributeparser:attrbutes_to_array(Prop) of
{ok, KeyNames} ->
case mapz:deep_find(KeyNames, Msg) of
{ok, V} ->
{ok, V, Prop};
error ->
{undefined, Prop}
end;
Error ->
io:format("ERROR get_prop {{{ ~p }}}~n", [Error]),
Error
end.
%%
%% Retrieve a nested parameters from the Msg map.
-spec retrieve_prop_value(PropName :: string(), Msg :: map()) -> any().
retrieve_prop_value(PropName, Msg) ->
case get_prop({ok, PropName}, Msg) of
{ok, V, _} ->
V;
_ ->
""
end.
%% Set a value in a nested map. This supports using nesting parameters to
%% set a value somewhere in a map.
-spec set_prop_value(PropName :: string(), Value :: any(), Msg :: map()) ->
map().
set_prop_value(PropName, Value, Msg) ->
case erl_attributeparser:attrbutes_to_array(PropName) of
{ok, KeyNames} ->
%% silently ignore any key that isn't available
try
mapz:deep_put(KeyNames, Value, Msg)
catch
error:Error ->
io:format(
"ERROR setting value: ~p =.=> ~p~n",
[PropName, Error]
),
Msg
end;
Error ->
io:format("ERROR setting value: ~p ==> ~p~n", [PropName, Error]),
Msg
end.
%%
%% remove an property that happens to match the nested path provided.
-spec delete_prop(
PropName :: string() | {ok, PropName :: string()}, Msg :: map()
) -> map().
delete_prop({ok, PropName}, Msg) ->
delete_prop(PropName, Msg);
delete_prop(PropName, Msg) ->
case erl_attributeparser:attrbutes_to_array(PropName) of
{ok, KeyNames} ->
%% silently ignore any key that isn't available
%% deep_remove seems to delete keeps if other keys don't exist,
%% so this pre-check ensures there is a value
%% see: https://github.com/eproxus/mapz/issues/1
%% TODO remove this if deep_remove is ever fixed
case mapz:deep_find(KeyNames, Msg) of
{ok, _} ->
try
mapz:deep_remove(KeyNames, Msg)
catch
error:_Error ->
Msg
end;
_ ->
Msg
end;
_Error ->
% silently ignore errors.
Msg
end.
%%
%% Generate an empty message map with just an _msgid
create_outgoing_msg(WsName) ->
{outgoing, #{'_msgid' => generate_id(), '_ws' => WsName}}.
%%
%%
key_to_binary(V) when is_binary(V) ->
V;
key_to_binary(V) when is_atom(V) ->
erlang:atom_to_binary(V);
key_to_binary(V) when is_list(V) ->
erlang:list_to_binary(V);
key_to_binary(V) ->
V.
%%
%%
map_keys_to_binary(Map) ->
maps:from_list(
lists:map(
fun({D, E}) -> {key_to_binary(D), E} end,
maps:to_list(Map)
)
).
%%
%%
key_to_list(V) when is_binary(V) ->
erlang:binary_to_list(V);
key_to_list(V) when is_atom(V) ->
erlang:atom_to_list(V);
key_to_list(V) when is_list(V) ->
V;
key_to_list(V) ->
V.
%%
%%
map_keys_to_lists(Map) ->
maps:from_list(
lists:map(
fun({D, E}) -> {key_to_list(D), E} end,
maps:to_list(Map)
)
).
%%
%%
%% erlfmt:ignore alignment
to_bool(<<"">>) -> false;
to_bool("") -> false;
to_bool(<<"false">>) -> false;
to_bool(false) -> false;
to_bool("false") -> false;
to_bool(_) -> true.
%%
%% From a string value, escape all occurance of \r \t \n to be replaced
%% by \\r \\t \\n - this allows for test definitions that include \r and
%% not a "return" or "tab" value.
%%
escape_specials(Str) when is_atom(Str) ->
escape_specials(atom_to_binary(Str));
escape_specials(Str) ->
re:replace(
re:replace(
re:replace(
Str,
"\r",
"\\\\r",
[{return, binary}, global]
),
"\n",
"\\\\n",
[{return, binary}, global]
),
"\t",
"\\\\t",
[{return, binary}, global]
).