couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject [couchdb] branch 1.x.x updated: feat: disallow dupe keys
Date Wed, 01 Nov 2017 07:29:11 GMT
This is an automated email from the ASF dual-hosted git repository.

jan pushed a commit to branch 1.x.x
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/1.x.x by this push:
     new 3706a77  feat: disallow dupe keys
3706a77 is described below

commit 3706a77c13a78672e5a3fbde06e7bffd3665f73b
Author: Jan Lehnardt <jan@apache.org>
AuthorDate: Tue Oct 31 19:00:16 2017 +0100

    feat: disallow dupe keys
---
 src/ejson/ejson.erl | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/ejson/ejson.erl b/src/ejson/ejson.erl
index 72bb6c1..aff5f3d 100644
--- a/src/ejson/ejson.erl
+++ b/src/ejson/ejson.erl
@@ -36,6 +36,9 @@ init() ->
 decode(undefined) ->
     throw({invalid_json, undefined});
 decode(IoList) ->
+    dedupe_objs(decode_int(IoList)).
+
+decode_int(IoList) ->
     try
         nif_decode(IoList)
     catch exit:ejson_nif_not_loaded ->
@@ -161,6 +164,23 @@ make_ejson([Value | RevEvs], [Vals | RestStack] = _Stack) ->
     make_ejson(RevEvs, [[Value | Vals] | RestStack]).
 
 
+dedupe_objs({Props}) when is_list(Props) ->
+    RevProps = lists:reverse(Props),
+    {_, NewProps} = lists:foldl(fun({Key, Val}, {Seen, PropAcc}) ->
+        case sets:is_element(Key, Seen) of
+            true ->
+                {Seen, PropAcc};
+            false ->
+                {sets:add_element(Key, Seen), [{Key, Val} | PropAcc]}
+        end
+    end, {sets:new(), []}, RevProps),
+    {NewProps};
+dedupe_objs(Vals) when is_list(Vals) ->
+    lists:map(fun dedupe_objs/1, Vals);
+dedupe_objs(Val) ->
+    Val.
+
+
 reverse_tokens(_) ->
     exit(ejson_nif_not_loaded).
 

-- 
To stop receiving notification emails like this one, please contact
['"commits@couchdb.apache.org" <commits@couchdb.apache.org>'].

Mime
View raw message