aboutsummaryrefslogtreecommitdiffstats
path: root/lib/diameter/src/base/diameter_traffic.erl
diff options
context:
space:
mode:
authorAnders Svensson <[email protected]>2017-07-06 11:02:31 +0200
committerAnders Svensson <[email protected]>2017-08-03 17:14:27 +0200
commit1b3b64af3d9a5441b6da37cf4e97b59cb043f33b (patch)
tree495cd9292b841814f4e509b78d68fe807ca6cd6f /lib/diameter/src/base/diameter_traffic.erl
parent722fa41564381dff0b7aa2b465193db30bb2f02f (diff)
downloadotp-1b3b64af3d9a5441b6da37cf4e97b59cb043f33b.tar.gz
otp-1b3b64af3d9a5441b6da37cf4e97b59cb043f33b.tar.bz2
otp-1b3b64af3d9a5441b6da37cf4e97b59cb043f33b.zip
Let messages and grouped AVPs be encoded/decoded from/to maps
With {record_decode, map}. The option name is arguably a bit misleading now, but not too objectionable given that the encode/decode in question has historically only been of records. One advantage of the map decode is that the map only contains values for those AVPs existing in the message or grouped AVP in question. The name of the message or grouped AVP is stored in with key ':name', the leading colon ensuring that the key isn't a diameter-name. Decoding to maps makes the hrl files generated from dictionary files largely irrelevant. There are value defines generated into these, but they're typically so long as to be unusable.
Diffstat (limited to 'lib/diameter/src/base/diameter_traffic.erl')
-rw-r--r--lib/diameter/src/base/diameter_traffic.erl87
1 files changed, 64 insertions, 23 deletions
diff --git a/lib/diameter/src/base/diameter_traffic.erl b/lib/diameter/src/base/diameter_traffic.erl
index 228d9802ad..f7dec2441f 100644
--- a/lib/diameter/src/base/diameter_traffic.erl
+++ b/lib/diameter/src/base/diameter_traffic.erl
@@ -76,7 +76,7 @@
service_name :: diameter:service_name(),
apps :: [#diameter_app{}],
sequence :: diameter:sequence(),
- codec :: #{record_decode := boolean(),
+ codec :: #{record_decode := boolean() | map,
string_decode := boolean(),
strict_mbit := boolean(),
incoming_maxlen := diameter:message_length()}}).
@@ -625,6 +625,12 @@ is_answer_message([#diameter_header{is_request = R, is_error = E} | _], _) ->
is_answer_message([Name | _], _) ->
Name == 'answer-message';
+%% Message sent as a map.
+is_answer_message(Map, _)
+ when is_map(Map) ->
+ #{':name' := Name} = Map,
+ Name == 'answer-message';
+
%% Message sent as a record.
is_answer_message(Rec, Dict) ->
try
@@ -873,6 +879,11 @@ reset(Msg, [RC | Avps], Dict) ->
set([_|_] = Ans, Avps, _) ->
Ans ++ Avps; %% Values nearer tail take precedence.
+%% ... a map ...
+set(Ans, Avps, _)
+ when is_map(Ans) ->
+ maps:merge(Ans, maps:from_list(Avps));
+
%% ... or record.
set(Rec, Avps, Dict) ->
Dict:'#set-'(Avps, Rec).
@@ -891,6 +902,9 @@ rc([MsgName | _], RC, Dict) ->
_ -> []
end;
+rc(#{':name' := Name}, RC, Dict) ->
+ rc([Name], RC, Dict);
+
rc(Rec, RC, Dict) ->
rc([Dict:rec2msg(element(1, Rec))], RC, Dict).
@@ -902,34 +916,50 @@ failed_avp(_, [] = No, _) ->
failed_avp(Msg, [_|_] = Avps, Dict) ->
[failed(Msg, [{'AVP', Avps}], Dict)].
-%% Reply as name and tuple list ...
-failed([MsgName | Values], FailedAvp, Dict) ->
- RecName = Dict:msg2rec(MsgName),
- try
- Dict:'#info-'(RecName, {index, 'Failed-AVP'}),
- {'Failed-AVP', [FailedAvp]}
- catch
- error: _ ->
- Avps = proplists:get_value('AVP', Values, []),
- A = #diameter_avp{name = 'Failed-AVP',
- value = FailedAvp},
- {'AVP', [A|Avps]}
- end;
+%% failed/3
-%% ... or record.
-failed(Rec, FailedAvp, Dict) ->
+failed(Msg, FailedAvp, Dict) ->
+ RecName = msg2rec(Msg, Dict),
try
- RecName = element(1, Rec),
- Dict:'#info-'(RecName, {index, 'Failed-AVP'}),
+ Dict:'#info-'(RecName, {index, 'Failed-AVP'}), %% assert existence
{'Failed-AVP', [FailedAvp]}
catch
error: _ ->
- Avps = Dict:'#get-'('AVP', Rec),
+ Avps = values(Msg, 'AVP', Dict),
A = #diameter_avp{name = 'Failed-AVP',
value = FailedAvp},
{'AVP', [A|Avps]}
end.
+%% msg2rec/2
+
+%% Message as name/values list ...
+msg2rec([MsgName | _], Dict) ->
+ Dict:msg2rec(MsgName);
+
+%% ... map ...
+msg2rec(#{':name' := MsgName}, Dict) ->
+ Dict:msg2rec(MsgName);
+
+%% ... or record.
+msg2rec(Rec, _) ->
+ element(1, Rec).
+
+%% values/2
+
+%% Message as name/values list ...
+values([_ | Avps], F, _) ->
+ proplists:get_value(F, Avps, []);
+
+%% ... map ...
+values(Msg, F, _)
+ when is_map(Msg) ->
+ maps:get(F, Msg, []);
+
+%% ... or record.
+values(Rec, F, Dict) ->
+ Dict:'#get-'(F, Rec).
+
%% 3. Diameter Header
%%
%% E(rror) - If set, the message contains a protocol error,
@@ -1861,7 +1891,7 @@ str(T) ->
get_avp(?RELAY, Name, Msg) ->
get_avp(?BASE, Name, Msg);
-%% Message as a header/avps list.
+%% Message is a header/avps list.
get_avp(Dict, Name, [#diameter_header{} | Avps]) ->
try
{Code, _, VId} = Dict:avp_header(Name),
@@ -1874,7 +1904,7 @@ get_avp(Dict, Name, [#diameter_header{} | Avps]) ->
undefined
end;
-%% Outgoing message as a name/values list.
+%% Message as name/values list ...
get_avp(_, Name, [_MsgName | Avps]) ->
case lists:keyfind(Name, 1, Avps) of
{_, V} ->
@@ -1883,7 +1913,17 @@ get_avp(_, Name, [_MsgName | Avps]) ->
undefined
end;
-%% Message is typically a record but not necessarily.
+%% ... map ...
+get_avp(_, Name, Map)
+ when is_map(Map) ->
+ case maps:find(Name, Map) of
+ {ok, V} ->
+ #diameter_avp{name = Name, value = V};
+ error ->
+ undefined
+ end;
+
+%% ... or record (but not necessarily).
get_avp(Dict, Name, Rec) ->
try
#diameter_avp{name = Name, value = Dict:'#get-'(Name, Rec)}
@@ -1913,7 +1953,8 @@ ungroup(Avp) ->
avp_decode(Dict, Name, #diameter_avp{value = undefined,
data = Bin}
- = Avp) ->
+ = Avp)
+ when is_binary(Bin) ->
try Dict:avp(decode, Bin, Name, decode_opts(Dict)) of
V ->
Avp#diameter_avp{value = V}