couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vatam...@apache.org
Subject config commit: updated refs/heads/master to f62d553
Date Thu, 10 Nov 2016 16:19:39 GMT
Repository: couchdb-config
Updated Branches:
  refs/heads/master a83b75ef4 -> f62d553b3


Features API and implementation.

Implement ability to enable, disable and query feature flags.

Features are identified as atoms. Usage intent is for various components in the
system to enable features, then the HTTP API will expose those to the user.
For example, features could indicate the presence of an optional component, a
plugin or a new mode of operation.

The API has 3 functions:

 * `config:features/0` : Return a sorted list of feature flags

 * `config:enable_feature/1` : Enables a feature. Feature argument could be a
   list, a binary or an atom.

 * `config:disable_feature/1` : Disables a feature.

Implementation is a thin wrapper around setting and deleting keys from the
'[features]' config section. This means, users can also set their own features
there via the .ini config files. Features set via the API are not persistent,
so applications will have to set them every time they initialize.

COUCHDB-3180


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

Branch: refs/heads/master
Commit: f62d553b337ce975edb0fb68772d22bdd3bf6490
Parents: a83b75e
Author: Nick Vatamaniuc <vatamane@apache.org>
Authored: Tue Nov 8 21:46:13 2016 -0500
Committer: Nick Vatamaniuc <vatamane@apache.org>
Committed: Tue Nov 8 21:46:20 2016 -0500

----------------------------------------------------------------------
 src/config.erl        | 16 ++++++++++++++++
 test/config_tests.erl | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/f62d553b/src/config.erl
----------------------------------------------------------------------
diff --git a/src/config.erl b/src/config.erl
index 9449b1e..2c7819b 100644
--- a/src/config.erl
+++ b/src/config.erl
@@ -31,6 +31,8 @@
 -export([get_float/3, set_float/3]).
 -export([get_boolean/3, set_boolean/3]).
 
+-export([features/0, enable_feature/1, disable_feature/1]).
+
 -export([listen_for_changes/2]).
 -export([subscribe_for_changes/1]).
 -export([parse_ini_file/1]).
@@ -38,6 +40,8 @@
 -export([init/1, terminate/2, code_change/3]).
 -export([handle_call/3, handle_cast/2, handle_info/2]).
 
+-define(FEATURES, "features").
+
 -record(config, {
     notify_funs=[],
     ini_files=undefined,
@@ -183,6 +187,18 @@ delete(Section, Key, Persist, Reason) when is_list(Section), is_list(Key)
->
     gen_server:call(?MODULE, {delete, Section, Key, Persist, Reason}).
 
 
+features() ->
+    lists:usort([list_to_atom(Key) || {Key, "true"} <- ?MODULE:get(?FEATURES)]).
+
+
+enable_feature(Feature) when is_atom(Feature) ->
+    ?MODULE:set(?FEATURES, atom_to_list(Feature), "true", false).
+
+
+disable_feature(Feature) when is_atom(Feature) ->
+    ?MODULE:delete(?FEATURES, atom_to_list(Feature), false).
+
+
 listen_for_changes(CallbackModule, InitialState) ->
     config_listener_mon:subscribe(CallbackModule, InitialState).
 

http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/f62d553b/test/config_tests.erl
----------------------------------------------------------------------
diff --git a/test/config_tests.erl b/test/config_tests.erl
index 8293f2e..a0e1758 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -172,6 +172,22 @@ config_del_test_() ->
     }.
 
 
+config_features_test_() ->
+    {
+        "Config features tests",
+        {
+            foreach,
+            fun setup/0,
+            fun teardown/1,
+            [
+                fun should_enable_features/0,
+                fun should_disable_features/0
+            ]
+        }
+    }.
+
+
+
 config_override_test_() ->
     {
         "Configs overide tests",
@@ -601,6 +617,32 @@ should_not_add_duplicate(_, _) ->
     end).
 
 
+should_enable_features() ->
+    ?assertEqual([], config:features()),
+
+    ?assertEqual(ok, config:enable_feature(snek)),
+    ?assertEqual([snek], config:features()),
+
+    ?assertEqual(ok, config:enable_feature(snek)),
+    ?assertEqual([snek], config:features()),
+
+    ?assertEqual(ok, config:enable_feature(dogo)),
+    ?assertEqual([dogo, snek], config:features()).
+
+
+should_disable_features() ->
+    ?assertEqual([], config:features()),
+
+    config:enable_feature(snek),
+    ?assertEqual([snek], config:features()),
+
+    ?assertEqual(ok, config:disable_feature(snek)),
+    ?assertEqual([], config:features()),
+
+    ?assertEqual(ok, config:disable_feature(snek)),
+    ?assertEqual([], config:features()).
+
+
 spawn_config_listener() ->
     Self = self(),
     Pid = erlang:spawn(fun() ->


Mime
View raw message