summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-08-25 14:12:27 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-08-25 14:18:07 +0100
commitfe363eed4fd96c4443c4ca9ffe13c2c47c9efe0e (patch)
tree593930c9d84a062e86217f70f299df6170633c13
parent05f5c01ba6b609f8b15574412fd874adda322590 (diff)
downloadclod-fe363eed4fd96c4443c4ca9ffe13c2c47c9efe0e.tar.gz
Support migrating lists, which Gitano needs at least short-term
-rw-r--r--examples/list-migration.lua32
-rw-r--r--lib/clod.lua16
-rw-r--r--test/test-clod.lua33
3 files changed, 79 insertions, 2 deletions
diff --git a/examples/list-migration.lua b/examples/list-migration.lua
new file mode 100644
index 0000000..907d148
--- /dev/null
+++ b/examples/list-migration.lua
@@ -0,0 +1,32 @@
+-- examples/list-migration.lua
+--
+-- Example of list migration capability of Clod
+--
+-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
+--
+
+clod = require "clod"
+
+local configuration = [[
+description = "Gitano Alpha Testers"
+members = {
+ "dsilvers",
+ "liw",
+}
+subgroups {
+}
+]]
+
+conf, err = clod.parse(configuration, "@config", true)
+if not conf then
+ error(err)
+end
+
+print("Group description is", conf.settings.description)
+local members = conf:get_list("members")
+print("There are", #members, "members")
+for i = 1, #members do
+ print(members[i])
+end
+local subgroups = conf:get_list("subgroups")
+print("There are", #subgroups, "sub-groups")
diff --git a/lib/clod.lua b/lib/clod.lua
index 4114fd6..8f9ec7f 100644
--- a/lib/clod.lua
+++ b/lib/clod.lua
@@ -293,7 +293,7 @@ function clod_mt:__index(key)
end
end
-local function parse_config(conf, confname)
+local function parse_config(conf, confname, migrate_lists)
local ret = {}
local settings = {}
local last_entry = {lineno = 0}
@@ -316,16 +316,28 @@ local function parse_config(conf, confname)
end
function parse_mt:__newindex(key, value)
-- This is the equivalent of 'foo = "bar"' instead of 'foo "bar"'
+ if migrate_lists and type(value) == "table" then
+ for i = 1, #value do
+ self[key .. ".*"](value[i],1)
+ end
+ return
+ end
if type(value) == "table" or type(value) == "function" then
error("Clod does not support " .. type(value) .. "s as values")
end
return self[key](value, 1)
end
function parse_mt:__call(value, offset)
+ local key = assert(keys[self])
+ if migrate_lists and type(value) == "table" then
+ for i = 1, #value do
+ self["*"](value[i],2)
+ end
+ return
+ end
if type(value) == "table" or type(value) == "function" then
error("Clod does not support " .. type(value) .. "s as values")
end
- local key = assert(keys[self])
local wild_prefix, last_key_element = key:match("^(.-)([^.]+)$")
if last_key_element == "*" then
-- Wild insert, so calculate a unique key to use
diff --git a/test/test-clod.lua b/test/test-clod.lua
index 7fc05cd..855be41 100644
--- a/test/test-clod.lua
+++ b/test/test-clod.lua
@@ -315,6 +315,39 @@ foo["*"] "Gawd"
assert(conf:serialise() == output_str)
end
+function suite.migrate_tables_assign()
+ local input_str = [[
+tab = { "hello", "world" }
+]]
+ local conf = assert(clod.parse(input_str, "@str", true))
+ local tab = conf:get_list('tab')
+ assert(#tab == 2)
+ assert(tab[1] == "hello")
+ assert(tab[2] == "world")
+ local output_str = [[
+tab["*"] "hello"
+tab["*"] "world"
+]]
+ assert(conf:serialise() == output_str)
+end
+
+function suite.migrate_tables_call()
+ local input_str = [[
+tab { "hello", "world" }
+]]
+ local conf = assert(clod.parse(input_str, "@str", true))
+ local tab = conf:get_list('tab')
+ assert(#tab == 2)
+ assert(tab[1] == "hello")
+ assert(tab[2] == "world")
+ local output_str = [[
+tab["*"] "hello"
+tab["*"] "world"
+]]
+print(conf:serialise())
+ assert(conf:serialise() == output_str)
+end
+
local count_ok = 0
for _, testname in ipairs(testnames) do
-- print("Run: " .. testname)