-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathered_node_template.erl
More file actions
114 lines (102 loc) · 3.43 KB
/
Copy pathered_node_template.erl
File metadata and controls
114 lines (102 loc) · 3.43 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
-module(ered_node_template).
-behaviour(ered_node).
-export([start/2]).
-export([handle_msg/2]).
-export([handle_event/2]).
%%
%%
%% Attributes of importance:
%%
%% "field": "payload",
%% "fieldType": "msg",
%% "format": "handlebars", <<---- ignored since this is for the frontend
%% "syntax": "plain", <<---- (can be "mustache template")
%% "output": "str", <<---- (can be "parse the content as json")
%% "template": "This is the payload: {{payload}} !",
%%
-import(ered_nodered_comm, [
debug/3,
debug_string/2,
post_exception_or_debug/3,
ws_from/1,
unsupported/3
]).
-import(ered_nodes, [
jstr/2,
send_msg_to_connected_nodes/2,
this_should_not_happen/2
]).
-import(ered_messages, [
decode_json/1,
set_prop_value/3,
map_keys_to_list/1
]).
doit(Prop, <<"msg">>, Template, <<"plain">>, <<"base64">>, Msg) ->
{ok, set_prop_value(Prop, base64:decode(binary_to_list(Template)), Msg)};
doit(Prop, <<"msg">>, Template, <<"mustache">>, <<"base64">>, Msg) ->
MustachedRendered = bbmustache:render(Template, Msg, [{key_type, binary}]),
{ok,
set_prop_value(
Prop,
base64:decode(binary_to_list(MustachedRendered)),
Msg
)};
doit(Prop, <<"msg">>, Template, <<"plain">>, <<"str">>, Msg) ->
{ok, set_prop_value(Prop, Template, Msg)};
doit(Prop, <<"msg">>, Template, <<"mustache">>, <<"str">>, Msg) ->
MustachedRendered = bbmustache:render(Template, Msg, [{key_type, binary}]),
{ok, set_prop_value(Prop, MustachedRendered, Msg)};
doit(Prop, <<"msg">>, Template, <<"plain">>, <<"json">>, Msg) ->
{ok, set_prop_value(Prop, decode_json(Template), Msg)};
doit(Prop, <<"msg">>, Template, <<"mustache">>, <<"json">>, Msg) ->
MustachedRendered = bbmustache:render(Template, Msg, [{key_type, binary}]),
{ok, set_prop_value(Prop, decode_json(MustachedRendered), Msg)};
doit(_, _, _, _, _, _) ->
unsupported.
%%
%%
start(NodeDef, _WsName) ->
ered_node:start(NodeDef, ?MODULE).
%%
%%
handle_event(_, NodeDef) ->
NodeDef.
%%
%%
% erlfmt:ignore - alginment
handle_msg(
{incoming, Msg},
#{
<<"field">> := Prop,
<<"fieldType">> := PropType,
<<"syntax">> := Syntax,
<<"template">> := Template,
<<"output">> := Output
} = NodeDef
) ->
try
{NewNodeDef, NewMsg} =
case doit(Prop, PropType, Template, Syntax, Output, Msg) of
{ok, Msg2} ->
send_msg_to_connected_nodes(NodeDef, Msg2),
{NodeDef, Msg2};
unsupported ->
ErrMsg = jstr("Unsupported configuration: ~p", [NodeDef]),
unsupported(NodeDef, Msg, ErrMsg),
%% Ok, we push the content into the msg object but warn as
%% well. Not modifying the message and sending it on would
%% be an error but not supporting formatting is kind of ...
%% an error really but since I don't raise an error here, set
%% the content in the msg.
Msg2 = set_prop_value(Prop, Template, Msg),
send_msg_to_connected_nodes(NodeDef, Msg2),
{NodeDef, Msg2}
end,
{handled, NewNodeDef, NewMsg}
catch
E:F:S ->
post_exception_or_debug(NodeDef, Msg, {E, F, S}),
{handled, NodeDef, dont_send_complete_msg}
end;
handle_msg(_, NodeDef) ->
{unhandled, NodeDef}.