Merge remote branch 'cloudant:70457-use-couch_epi-decide'
This closes #187
Signed-off-by: ILYA Khlopotov <iilyak@ca.ibm.com>
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(?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
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 @@
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.
diff --git a/test/couch_db_plugin_tests.erl b/test/couch_db_plugin_tests.erl
index 337207e..ea9b230 100644
--- a/test/couch_db_plugin_tests.erl
+++ b/test/couch_db_plugin_tests.erl
@@ -52,9 +52,10 @@
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];
@@ -82,44 +83,52 @@
{
setup, fun setup/0, fun teardown/1,
[
- fun validate_dbname_match/0,
- fun validate_dbname_no_match/0,
- fun validate_dbname_throw/0,
+ {"validate_dbname_match", fun validate_dbname_match/0},
+ {"validate_dbname_no_match", fun validate_dbname_no_match/0},
+ {"validate_dbname_throw", fun validate_dbname_throw/0},
+ {"validate_dbname_pass", fun validate_dbname_pass/0},
- fun before_doc_update_match/0,
- fun before_doc_update_no_match/0,
- fun before_doc_update_throw/0,
+ {"before_doc_update_match", fun before_doc_update_match/0},
+ {"before_doc_update_no_match", fun before_doc_update_no_match/0},
+ {"before_doc_update_throw", fun before_doc_update_throw/0},
- fun after_doc_read_match/0,
- fun after_doc_read_no_match/0,
- fun after_doc_read_throw/0,
+ {"after_doc_read_match", fun after_doc_read_match/0},
+ {"after_doc_read_no_match", fun after_doc_read_no_match/0},
+ {"after_doc_read_throw", fun after_doc_read_throw/0},
- fun validate_docid_match/0,
- fun validate_docid_no_match/0,
- fun validate_docid_throw/0,
+ {"validate_docid_match", fun validate_docid_match/0},
+ {"validate_docid_no_match", fun validate_docid_no_match/0},
+ {"validate_docid_throw", fun validate_docid_throw/0},
- fun check_is_admin_match/0,
- fun check_is_admin_no_match/0,
- fun check_is_admin_throw/0,
+ {"check_is_admin_match", fun check_is_admin_match/0},
+ {"check_is_admin_no_match", fun check_is_admin_no_match/0},
+ {"check_is_admin_throw", fun check_is_admin_throw/0},
- fun on_delete_match/0,
- fun on_delete_no_match/0,
- fun on_delete_throw/0
+ {"on_delete_match", fun on_delete_match/0},
+ {"on_delete_no_match", fun on_delete_no_match/0},
+ {"on_delete_throw", fun on_delete_throw/0}
]
}
}.
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(