aboutsummaryrefslogtreecommitdiffstats
path: root/lib/diameter/src/base/diameter_gen.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_gen.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_gen.erl')
-rw-r--r--lib/diameter/src/base/diameter_gen.erl28
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/diameter/src/base/diameter_gen.erl b/lib/diameter/src/base/diameter_gen.erl
index 4879ad8f6c..be2e221b7e 100644
--- a/lib/diameter/src/base/diameter_gen.erl
+++ b/lib/diameter/src/base/diameter_gen.erl
@@ -50,7 +50,9 @@
%% # encode_avps/3
%% ---------------------------------------------------------------------------
--spec encode_avps(parent_name(), parent_record() | avp_values(), map())
+-spec encode_avps(parent_name(),
+ parent_record() | avp_values() | map(),
+ map())
-> iolist()
| no_return().
@@ -83,6 +85,11 @@ encode(Name, Vals, Opts, Mod)
when is_list(Vals) ->
encode(Name, Mod:'#set-'(Vals, newrec(Mod, Name)), Opts, Mod);
+encode(Name, Map, Opts, Mod)
+ when is_map(Map) ->
+ [enc(Name, F, A, V, Opts, Mod) || {F,A} <- Mod:avp_arity(Name),
+ V <- [maps:get(F, Map, def(A))]];
+
encode(Name, Rec, Opts, Mod) ->
[encode(Name, F, V, Opts, Mod) || {F,V} <- Mod:'#get-'(Rec)].
@@ -627,6 +634,14 @@ too_many(FieldName, M, Map) ->
set(_, _, _, _, undefined = No) ->
No;
+set(1, F, Value, _, Map)
+ when is_map(Map) ->
+ maps:put(F, Value, Map);
+
+set(_, F, V, _, Map)
+ when is_map(Map) ->
+ maps:update_with(F, fun(Vs) -> [V|Vs] end, [V], Map);
+
set(1, F, Value, Mod, Rec) ->
Mod:'#set-'({F, Value}, Rec);
@@ -731,6 +746,9 @@ empty(Name, #{module := Mod} = Opts) ->
newrec(_, _, #{record_decode := false}) ->
undefined;
+newrec(_, Name, #{record_decode := map}) ->
+ #{':name' => Name};
+
newrec(Mod, Name, _) ->
newrec(Mod, Name).
@@ -738,3 +756,11 @@ newrec(Mod, Name, _) ->
newrec(Mod, Name) ->
Mod:'#new-'(Mod:name2rec(Name)).
+
+%% def/1
+
+def(1) ->
+ undefined;
+
+def(_) ->
+ [].