Repository: couchdb-couch
Updated Branches:
  refs/heads/master 4476595e0 -> ac3dae37d


Use couch_epi:decide for validate_dbname

COUCHDB-3066


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

Branch: refs/heads/master
Commit: 11c25659c5057e45f0fbff44e4baf64ec7f92fe4
Parents: 4476595
Author: ILYA Khlopotov <iil...@ca.ibm.com>
Authored: Mon Jul 18 11:49:51 2016 -0700
Committer: ILYA Khlopotov <iil...@ca.ibm.com>
Committed: Mon Jul 18 12:10:01 2016 -0700

----------------------------------------------------------------------
 src/couch_db.erl               |  8 ++------
 src/couch_db_plugin.erl        | 19 ++++++++++++++-----
 test/couch_db_plugin_tests.erl | 21 +++++++++++++++------
 3 files changed, 31 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/11c25659/src/couch_db.erl
----------------------------------------------------------------------
diff --git a/src/couch_db.erl b/src/couch_db.erl
index 8260a5c..3eff99f 100644
--- a/src/couch_db.erl
+++ b/src/couch_db.erl
@@ -1536,12 +1536,8 @@ validate_dbname(DbName) when is_list(DbName) ->
     validate_dbname(?l2b(DbName));
 validate_dbname(DbName) when is_binary(DbName) ->
     Normalized = normalize_dbname(DbName),
-    case couch_db_plugin:validate_dbname(DbName, Normalized) of
-        true ->
-            ok;
-        false ->
-            validate_dbname_int(DbName, Normalized)
-    end.
+    couch_db_plugin:validate_dbname(
+        DbName, Normalized, fun validate_dbname_int/2).
 
 validate_dbname_int(DbName, Normalized) when is_binary(DbName) ->
     case re:run(DbName, ?DBNAME_REGEX, [{capture,none}, dollar_endonly]) of

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/11c25659/src/couch_db_plugin.erl
----------------------------------------------------------------------
diff --git a/src/couch_db_plugin.erl b/src/couch_db_plugin.erl
index 1a6d2ea..774e9e0 100644
--- a/src/couch_db_plugin.erl
+++ b/src/couch_db_plugin.erl
@@ -13,7 +13,7 @@
 -module(couch_db_plugin).
 
 -export([
-    validate_dbname/2,
+    validate_dbname/3,
     before_doc_update/2,
     after_doc_read/2,
     validate_docid/1,
@@ -29,10 +29,8 @@
 %% API Function Definitions
 %% ------------------------------------------------------------------
 
-validate_dbname(DbName, Normalized) ->
-    Handle = couch_epi:get_handle(?SERVICE_ID),
-    %% callbacks return true only if it specifically allow the given Id
-    couch_epi:any(Handle, ?SERVICE_ID, validate_dbname, [DbName, Normalized], 
[]).
+validate_dbname(DbName, Normalized, Default) ->
+    maybe_handle(validate_dbname, [DbName, Normalized], Default).
 
 before_doc_update(#db{before_doc_update = Fun} = Db, Doc0) ->
     case with_pipe(before_doc_update, [Doc0, Db]) of
@@ -70,3 +68,14 @@ with_pipe(Func, Args) ->
 do_apply(Func, Args, Opts) ->
     Handle = couch_epi:get_handle(?SERVICE_ID),
     couch_epi:apply(Handle, ?SERVICE_ID, Func, Args, Opts).
+
+maybe_handle(Func, Args, Default) ->
+    Handle = couch_epi:get_handle(?SERVICE_ID),
+    case couch_epi:decide(Handle, ?SERVICE_ID, Func, Args, []) of
+       no_decision when is_function(Default) ->
+           apply(Default, Args);
+       no_decision ->
+           Default;
+       {decided, Result} ->
+           Result
+    end.

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/11c25659/test/couch_db_plugin_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_db_plugin_tests.erl b/test/couch_db_plugin_tests.erl
index 337207e..bbc01c7 100644
--- a/test/couch_db_plugin_tests.erl
+++ b/test/couch_db_plugin_tests.erl
@@ -52,9 +52,10 @@ setup() ->
 teardown(Ctx) ->
     couch_tests:teardown(Ctx).
 
-validate_dbname({true, _Db}, _) -> true;
-validate_dbname({false, _Db}, _) -> false;
-validate_dbname({fail, _Db}, _) -> throw(validate_dbname).
+validate_dbname({true, _Db}, _) -> {decided, true};
+validate_dbname({false, _Db}, _) -> {decided, false};
+validate_dbname({fail, _Db}, _) -> throw(validate_dbname);
+validate_dbname({pass, _Db}, _) -> no_decision.
 
 before_doc_update({fail, _Doc}, _Db) -> throw(before_doc_update);
 before_doc_update({true, Doc}, Db) -> [{true, [before_doc_update|Doc]}, Db];
@@ -85,6 +86,7 @@ callback_test_() ->
                 fun validate_dbname_match/0,
                 fun validate_dbname_no_match/0,
                 fun validate_dbname_throw/0,
+                fun validate_dbname_pass/0,
 
                 fun before_doc_update_match/0,
                 fun before_doc_update_no_match/0,
@@ -111,15 +113,22 @@ callback_test_() ->
 
 
 validate_dbname_match() ->
-    ?assert(couch_db_plugin:validate_dbname({true, [db]}, db)).
+    ?assert(couch_db_plugin:validate_dbname(
+        {true, [db]}, db, fun(_, _) -> pass end)).
 
 validate_dbname_no_match() ->
-    ?assertNot(couch_db_plugin:validate_dbname({false, [db]}, db)).
+    ?assertNot(couch_db_plugin:validate_dbname(
+        {false, [db]}, db, fun(_, _) -> pass end)).
 
 validate_dbname_throw() ->
     ?assertThrow(
         validate_dbname,
-        couch_db_plugin:validate_dbname({fail, [db]}, db)).
+        couch_db_plugin:validate_dbname(
+            {fail, [db]}, db, fun(_, _) -> pass end)).
+
+validate_dbname_pass() ->
+    ?assertEqual(pass, couch_db_plugin:validate_dbname(
+        {pass, [db]}, db, fun(_, _) -> pass end)).
 
 before_doc_update_match() ->
     ?assertMatch(

Reply via email to