Merged with 0.6.
authorWaqas Hussain <waqas20@gmail.com>
Mon, 23 Nov 2009 16:49:20 +0000 (21:49 +0500)
committerWaqas Hussain <waqas20@gmail.com>
Mon, 23 Nov 2009 16:49:20 +0000 (21:49 +0500)
core/modulemanager.lua
tests/modulemanager_option_conversion.lua [new file with mode: 0644]

index 9cd5618722e5baba6ff3ddeaaf2864773773828b..d1f7d413cad3866201419766080e710aaef535ba 100644 (file)
@@ -28,7 +28,9 @@ local type = type;
 local next = next;
 local rawget = rawget;
 local error = error;
-local tostring = tostring;
+local tostring, tonumber = tostring, tonumber;
+
+local array, set = require "util.array", require "util.set";
 
 local autoload_modules = {"presence", "message", "iq"};
 
@@ -400,6 +402,85 @@ function api:get_option(name, default_value)
        return value;
 end
 
+function api:get_option_string(...)
+       local value = self:get_option(...);
+       if type(value) == "table" then
+               if #value > 1 then
+                       self:log("error", "Config option '%s' does not take a list, using just the first item", name);
+               end
+               value = value[1];
+       end
+       if value == nil then
+               return nil;
+       end
+       return tostring(value);
+end
+
+function api:get_option_number(name, ...)
+       local value = self:get_option(name, ...);
+       if type(value) == "table" then
+               if #value > 1 then
+                       self:log("error", "Config option '%s' does not take a list, using just the first item", name);
+               end
+               value = value[1];
+       end
+       local ret = tonumber(value);
+       if value ~= nil and ret == nil then
+               self:log("error", "Config option '%s' not understood, expecting a number", name);
+       end
+       return ret;
+end
+
+function api:get_option_boolean(name, ...)
+       local value = self:get_option(name, ...);
+       if type(value) == "table" then
+               if #value > 1 then
+                       self:log("error", "Config option '%s' does not take a list, using just the first item", name);
+               end
+               value = value[1];
+       end
+       if value == nil then
+               return nil;
+       end
+       local ret = value == true or value == "true" or value == 1 or nil;
+       if ret == nil then
+               ret = (value == false or value == "false" or value == 0);
+               if ret then
+                       ret = false;
+               else
+                       ret = nil;
+               end
+       end
+       if ret == nil then
+               self:log("error", "Config option '%s' not understood, expecting true/false", name);
+       end
+       return ret;
+end
+
+function api:get_option_array(name, ...)
+       local value = self:get_option(name, ...);
+
+       if value == nil then
+               return nil;
+       end
+       
+       if type(value) ~= "table" then
+               return array{ value }; -- Assume any non-list is a single-item list
+       end
+       
+       return array():append(value); -- Clone
+end
+
+function api:get_option_set(name, ...)
+       local value = self:get_option_array(name, ...);
+       
+       if value == nil then
+               return nil;
+       end
+       
+       return set.new(value);
+end
+
 local t_remove = _G.table.remove;
 local module_items = multitable_new();
 function api:add_item(key, value)
diff --git a/tests/modulemanager_option_conversion.lua b/tests/modulemanager_option_conversion.lua
new file mode 100644 (file)
index 0000000..7dceeae
--- /dev/null
@@ -0,0 +1,55 @@
+package.path = "../?.lua;"..package.path;
+
+local api = require "core.modulemanager".api;
+
+local module = setmetatable({}, {__index = api});
+local opt = nil;
+function module:log() end
+function module:get_option(name)
+       if name == "opt" then
+               return opt;
+       else
+               return nil;
+       end
+end
+
+function test_value(value, returns)
+       opt = value;
+       assert(module:get_option_number("opt") == returns.number, "number doesn't match");
+       assert(module:get_option_string("opt") == returns.string, "string doesn't match");
+       assert(module:get_option_boolean("opt") == returns.boolean, "boolean doesn't match");
+       
+       if type(returns.array) == "table" then
+               local target_array, returned_array = returns.array, module:get_option_array("opt");
+               assert(#target_array == #returned_array, "array length doesn't match");
+               for i=1,#target_array do
+                       assert(target_array[i] == returned_array[i], "array item doesn't match");
+               end
+       else
+               assert(module:get_option_array("opt") == returns.array, "array is returned (not nil)");
+       end
+       
+       if type(returns.set) == "table" then
+               local target_items, returned_items = set.new(returns.set), module:get_option_set("opt");
+               assert(target_items == returned_items, "set doesn't match");
+       else
+               assert(module:get_option_set("opt") == returns.set, "set is returned (not nil)");
+       end
+end
+
+test_value(nil, {});
+
+test_value(true, { boolean = true, string = "true", array = {true}, set = {true} });
+test_value(false, { boolean = false, string = "false", array = {false}, set = {false} });
+test_value("true", { boolean = true, string = "true", array = {"true"}, set = {"true"} });
+test_value("false", { boolean = false, string = "false", array = {"false"}, set = {"false"} });
+test_value(1, { boolean = true, string = "1", array = {1}, set = {1}, number = 1 });
+test_value(0, { boolean = false, string = "0", array = {0}, set = {0}, number = 0 });
+
+test_value("hello world", { string = "hello world", array = {"hello world"}, set = {"hello world"} });
+test_value(1234, { string = "1234", number = 1234, array = {1234}, set = {1234} });
+
+test_value({1, 2, 3}, { boolean = true, string = "1", number = 1, array = {1, 2, 3}, set = {1, 2, 3} });
+test_value({1, 2, 3, 3, 4}, {boolean = true, string = "1", number = 1, array = {1, 2, 3, 3, 4}, set = {1, 2, 3, 4} });
+test_value({0, 1, 2, 3}, { boolean = false, string = "0", number = 0, array = {0, 1, 2, 3}, set = {0, 1, 2, 3} });
+