couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [6/9] config commit: updated refs/heads/1843-feature-bigcouch to 31d14bc
Date Tue, 22 Apr 2014 10:08:32 GMT
Type-safe config:get and :set methods

BugzID: 25731


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

Branch: refs/heads/1843-feature-bigcouch
Commit: 6859d11114af0911b91d6e97ac2d87d00c2a3bc3
Parents: 96d56e3
Author: Robert Newson <robert.newson@cloudant.com>
Authored: Tue Dec 3 14:36:27 2013 +0000
Committer: Robert Newson <robert.newson@cloudant.com>
Committed: Tue Dec 3 14:36:27 2013 +0000

----------------------------------------------------------------------
 src/config.erl | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/6859d111/src/config.erl
----------------------------------------------------------------------
diff --git a/src/config.erl b/src/config.erl
index c7c660b..98b5221 100644
--- a/src/config.erl
+++ b/src/config.erl
@@ -27,6 +27,10 @@
 -export([set/3, set/4, set/5]).
 -export([delete/2, delete/3, delete/4]).
 
+-export([get_integer/3, set_integer/3]).
+-export([get_float/3, set_float/3]).
+-export([get_boolean/3, set_boolean/3]).
+
 -export([listen_for_changes/2]).
 -export([parse_ini_file/1]).
 
@@ -49,6 +53,74 @@ stop() ->
 all() ->
     lists:sort(gen_server:call(?MODULE, all, infinity)).
 
+get_integer(Section, Key, Default) when is_integer(Default) ->
+    try
+        to_integer(get(Section, Key, Default))
+    catch
+        error:badarg ->
+            Default
+    end.
+
+set_integer(Section, Key, Value) when is_integer(Value) ->
+    set(Section, Key, integer_to_list(Value));
+set_integer(_, _, _) ->
+    error(badarg).
+
+to_integer(List) when is_list(List) ->
+    list_to_integer(List);
+to_integer(Int) when is_integer(Int) ->
+    Int;
+to_integer(Bin) when is_binary(Bin) ->
+    binary_to_list(list_to_integer(Bin)).
+
+get_float(Section, Key, Default) when is_float(Default) ->
+    try
+        to_float(get(Section, Key, Default))
+    catch
+        error:badarg ->
+            Default
+    end.
+
+set_float(Section, Key, Value) when is_float(Value) ->
+    set(Section, Key, float_to_list(Value));
+set_float(_, _, _) ->
+    error(badarg).
+
+to_float(List) when is_list(List) ->
+    list_to_float(List);
+to_float(Float) when is_float(Float) ->
+    Float;
+to_float(Int) when is_integer(Int) ->
+    list_to_float(integer_to_list(Int));
+to_float(Bin) when is_binary(Bin) ->
+    binary_to_list(list_to_float(Bin)).
+
+get_boolean(Section, Key, Default) when is_boolean(Default) ->
+    try
+        to_boolean(get(Section, Key, Default))
+    catch
+        error:badarg ->
+            Default
+    end.
+
+set_boolean(Section, Key, true) ->
+    set(Section, Key, "true");
+set_boolean(Section, Key, false) ->
+    set(Section, Key, "false");
+set_boolean(_, _, _) ->
+    error(badarg).
+
+to_boolean(List) when is_list(List) ->
+    case list_to_existing_atom(List) of
+        true  ->
+            true;
+        false ->
+            false;
+        _ ->
+            error(badarg)
+    end;
+to_boolean(Bool) when is_boolean(Bool) ->
+    Bool.
 
 get(Section) when is_binary(Section) ->
     ?MODULE:get(binary_to_list(Section));


Mime
View raw message