You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ko...@apache.org on 2017/03/01 16:37:45 UTC

[11/43] couch-mrview commit: updated refs/heads/2971-count-distinct to f7c3c24

Allow json decode in parse_params to be optional

This allows explicitly indicate if ifunction parse_params
should decode passed parameter's value instead of trying
to guess it by value's term type.

It makes it possible to use this function both with GET
and POST queries.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/commit/26f5af7c
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/tree/26f5af7c
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/diff/26f5af7c

Branch: refs/heads/2971-count-distinct
Commit: 26f5af7c6d2c7642e9463cd26f3c4070c9add2e8
Parents: d07771a
Author: Eric Avdey <ei...@eiri.ca>
Authored: Mon Jun 13 13:28:17 2016 -0300
Committer: Eric Avdey <ei...@eiri.ca>
Committed: Mon Jun 13 15:48:00 2016 -0300

----------------------------------------------------------------------
 src/couch_mrview_http.erl | 45 ++++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/26f5af7c/src/couch_mrview_http.erl
----------------------------------------------------------------------
diff --git a/src/couch_mrview_http.erl b/src/couch_mrview_http.erl
index 1f08961..191198c 100644
--- a/src/couch_mrview_http.erl
+++ b/src/couch_mrview_http.erl
@@ -32,6 +32,7 @@
     prepend_val/1,
     parse_params/2,
     parse_params/3,
+    parse_params/4,
     view_cb/2,
     row_to_json/1,
     row_to_json/2,
@@ -439,39 +440,55 @@ parse_params(Props, Keys) ->
     parse_params(Props, Keys, Args).
 
 
-parse_params(Props, Keys, #mrargs{}=Args0) ->
+parse_params(Props, Keys, Args) ->
+    parse_params(Props, Keys, Args, []).
+
+parse_params(Props, Keys, #mrargs{}=Args0, Options) ->
+    IsDecoded = lists:member(decoded, Options),
     % group_level set to undefined to detect if explicitly set by user
     Args1 = Args0#mrargs{keys=Keys, group=undefined, group_level=undefined},
     lists:foldl(fun({K, V}, Acc) ->
-        parse_param(K, V, Acc)
+        parse_param(K, V, Acc, IsDecoded)
     end, Args1, Props).
 
 
-parse_param(Key, Val, Args) when is_binary(Key) ->
-    parse_param(binary_to_list(Key), Val, Args);
-parse_param(Key, Val, Args) ->
+parse_param(Key, Val, Args, IsDecoded) when is_binary(Key) ->
+    parse_param(binary_to_list(Key), Val, Args, IsDecoded);
+parse_param(Key, Val, Args, IsDecoded) ->
     case Key of
         "" ->
             Args;
         "reduce" ->
             Args#mrargs{reduce=parse_boolean(Val)};
+        "key" when IsDecoded ->
+            Args#mrargs{start_key=Val, end_key=Val};
         "key" ->
-            JsonKey = parse_json(Val),
+            JsonKey = ?JSON_DECODE(Val),
             Args#mrargs{start_key=JsonKey, end_key=JsonKey};
+        "keys" when IsDecoded ->
+            Args#mrargs{keys=Val};
         "keys" ->
-            Args#mrargs{keys=parse_json(Val)};
+            Args#mrargs{keys=?JSON_DECODE(Val)};
+        "startkey" when IsDecoded ->
+            Args#mrargs{start_key=Val};
+        "start_key" when IsDecoded ->
+            Args#mrargs{start_key=Val};
         "startkey" ->
-            Args#mrargs{start_key=parse_json(Val)};
+            Args#mrargs{start_key=?JSON_DECODE(Val)};
         "start_key" ->
-            Args#mrargs{start_key=parse_json(Val)};
+            Args#mrargs{start_key=?JSON_DECODE(Val)};
         "startkey_docid" ->
             Args#mrargs{start_key_docid=couch_util:to_binary(Val)};
         "start_key_doc_id" ->
             Args#mrargs{start_key_docid=couch_util:to_binary(Val)};
+        "endkey" when IsDecoded ->
+            Args#mrargs{end_key=Val};
+        "end_key" when IsDecoded ->
+            Args#mrargs{end_key=Val};
         "endkey" ->
-            Args#mrargs{end_key=parse_json(Val)};
+            Args#mrargs{end_key=?JSON_DECODE(Val)};
         "end_key" ->
-            Args#mrargs{end_key=parse_json(Val)};
+            Args#mrargs{end_key=?JSON_DECODE(Val)};
         "endkey_docid" ->
             Args#mrargs{end_key_docid=couch_util:to_binary(Val)};
         "end_key_doc_id" ->
@@ -573,9 +590,3 @@ check_view_etag(Sig, Acc0, Req) ->
         true -> throw({etag_match, ETag});
         false -> {ok, Acc0#vacc{etag=ETag}}
     end.
-
-
-parse_json(V) when is_list(V) ->
-    ?JSON_DECODE(V);
-parse_json(V) ->
-    V.