You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by kx...@apache.org on 2015/12/21 22:39:27 UTC

[2/2] couch-mrview commit: updated refs/heads/master to 6c9833d

Accept rewrites as string function

COUCHDB-2874


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/6c9833d6
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/tree/6c9833d6
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/diff/6c9833d6

Branch: refs/heads/master
Commit: 6c9833d667e319b82a7a1fffb8ba92116534e63f
Parents: 946d942
Author: Alexander Shorin <kx...@apache.org>
Authored: Tue Nov 10 20:55:02 2015 +0300
Committer: Alexander Shorin <kx...@apache.org>
Committed: Wed Nov 11 01:03:43 2015 +0300

----------------------------------------------------------------------
 src/couch_mrview.erl                        | 14 ++++++++++++--
 test/couch_mrview_ddoc_validation_tests.erl | 14 +++++++++++---
 2 files changed, 23 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/6c9833d6/src/couch_mrview.erl
----------------------------------------------------------------------
diff --git a/src/couch_mrview.erl b/src/couch_mrview.erl
index 39537cf..3d06a1e 100644
--- a/src/couch_mrview.erl
+++ b/src/couch_mrview.erl
@@ -56,7 +56,7 @@ validate_ddoc_fields(DDoc) ->
         [{<<"language">>, string}],
         [{<<"lists">>, object}, {any, string}],
         [{<<"options">>, object}],
-        [{<<"rewrites">>, array}],
+        [{<<"rewrites">>, [string, array]}],
         [{<<"shows">>, object}, {any, string}],
         [{<<"updates">>, object}, {any, string}],
         [{<<"validate_doc_update">>, string}],
@@ -88,7 +88,7 @@ validate_ddoc_fields(DDoc, Path) ->
         ok -> ok;
         {error, {FailedPath0, Type0}} ->
             FailedPath = iolist_to_binary(join(FailedPath0, <<".">>)),
-            Type = ?l2b(atom_to_list(Type0)),
+            Type = format_type(Type0),
             throw({invalid_design_doc,
                   <<"`", FailedPath/binary, "` field must have ",
                      Type/binary, " type">>})
@@ -121,6 +121,11 @@ validate_ddoc_field(undefined, Type) when is_atom(Type) ->
     ok;
 validate_ddoc_field(_, any) ->
     ok;
+validate_ddoc_field(Value, Types) when is_list(Types) ->
+    lists:foldl(fun
+        (_, ok) -> ok;
+        (Type, _) -> validate_ddoc_field(Value, Type)
+    end, error, Types);
 validate_ddoc_field(Value, string) when is_binary(Value) ->
     ok;
 validate_ddoc_field(Value, array) when is_list(Value) ->
@@ -150,6 +155,11 @@ map_function_type({Props}) ->
         _ -> string
     end.
 
+format_type(Type) when is_atom(Type) ->
+    ?l2b(atom_to_list(Type));
+format_type(Types) when is_list(Types) ->
+    iolist_to_binary(join(lists:map(fun atom_to_list/1, Types), <<" or ">>)).
+
 join(L, Sep) ->
     join(L, Sep, []).
 join([H|[]], _, Acc) ->

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/6c9833d6/test/couch_mrview_ddoc_validation_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_mrview_ddoc_validation_tests.erl b/test/couch_mrview_ddoc_validation_tests.erl
index 459c959..028e0be 100644
--- a/test/couch_mrview_ddoc_validation_tests.erl
+++ b/test/couch_mrview_ddoc_validation_tests.erl
@@ -45,7 +45,8 @@ ddoc_validation_test_() ->
                     fun should_reject_non_object_views/1,
                     fun should_reject_non_string_language/1,
                     fun should_reject_non_string_validate_doc_update/1,
-                    fun should_reject_non_array_rewrites/1,
+                    fun should_accept_string_rewrites/1,
+                    fun should_reject_bad_rewrites/1,
                     fun should_accept_option/1,
                     fun should_accept_any_option/1,
                     fun should_accept_filter/1,
@@ -176,10 +177,17 @@ should_reject_non_string_validate_doc_update(Db) ->
     ?_assertThrow({bad_request, invalid_design_doc, _},
                   couch_db:update_doc(Db, Doc, [])).
 
-should_reject_non_array_rewrites(Db) ->
+should_accept_string_rewrites(Db) ->
     Doc = couch_doc:from_json_obj({[
         {<<"_id">>, <<"_design/should_reject_non_array_rewrites">>},
-        {<<"rewrites">>, <<"invalid">>}
+        {<<"rewrites">>, <<"function(req){}">>}
+    ]}),
+    ?_assertMatch({ok,_}, couch_db:update_doc(Db, Doc, [])).
+
+should_reject_bad_rewrites(Db) ->
+    Doc = couch_doc:from_json_obj({[
+        {<<"_id">>, <<"_design/should_reject_non_array_rewrites">>},
+        {<<"rewrites">>, 42}
     ]}),
     ?_assertThrow({bad_request, invalid_design_doc, _},
                   couch_db:update_doc(Db, Doc, [])).