diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-10-14 19:19:50 +0300 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-10-14 19:49:57 +0300 |
commit | e55dc995617a19fbc5772d7bd9fb3082a2d2b1f4 (patch) | |
tree | 33ddd87a1e3f0976da2d0817bd09ebc2169c2d56 | |
parent | d15bdd8d9177e0b74a5f7a675ad09ea15a04bc86 (diff) | |
download | acf2-e55dc995617a19fbc5772d7bd9fb3082a2d2b1f4.tar.bz2 acf2-e55dc995617a19fbc5772d7bd9fb3082a2d2b1f4.tar.xz |
persistence: boolean/string conversion
-rw-r--r-- | acf2/model/field.lua | 9 | ||||
-rw-r--r-- | acf2/persistence/backends/augeas.lua | 3 | ||||
-rw-r--r-- | acf2/persistence/backends/files.lua | 2 | ||||
-rw-r--r-- | acf2/persistence/init.lua | 13 | ||||
-rw-r--r-- | acf2/persistence/util.lua | 6 | ||||
-rw-r--r-- | acf2/util.lua | 9 |
6 files changed, 33 insertions, 9 deletions
diff --git a/acf2/model/field.lua b/acf2/model/field.lua index 6cbfa96..5829201 100644 --- a/acf2/model/field.lua +++ b/acf2/model/field.lua @@ -17,11 +17,6 @@ local super = object.super local util = require('acf2.util') -local function contains(list, value) - for k, v in ipairs(list) do if v == value then return true end end - return false -end - M.Member = class() function M.Member:init(params) @@ -92,7 +87,9 @@ function M.Field:_validate(context, value) if self.required and value == nil then raise(context.path, 'Required value not set') end - if self.choice and value ~= nil and not contains(self.choice, value) then + if self.choice and value ~= nil and not util.contains( + self.choice, value + ) then raise(context.path, 'Invalid value') end if value ~= nil then self:validate(context, value) end diff --git a/acf2/persistence/backends/augeas.lua b/acf2/persistence/backends/augeas.lua index 7078fe9..c24ec6f 100644 --- a/acf2/persistence/backends/augeas.lua +++ b/acf2/persistence/backends/augeas.lua @@ -5,6 +5,7 @@ See LICENSE file for license details local topology = require('acf2.model.root').topology local pth = require('acf2.path') +local tostr = require('acf2.persistence.util').tostring local util = require('acf2.util') local copy = util.copy @@ -130,7 +131,7 @@ function backend:set(mods) end if type(value) == 'table' then value = nil end - if not delete or mvpath then self.aug:set(apath, value) end + if not delete or mvpath then self.aug:set(apath, tostr(value)) end if delete or value == '' then gcpaths[mpath] = true end end diff --git a/acf2/persistence/backends/files.lua b/acf2/persistence/backends/files.lua index 70ff33e..02dcf82 100644 --- a/acf2/persistence/backends/files.lua +++ b/acf2/persistence/backends/files.lua @@ -94,7 +94,7 @@ function backend:set(mods) else local file = util.open_file(name, 'w') - file:write(tostring(value)) + file:write(util.tostring(value)) file:close() self.cache[name] = value diff --git a/acf2/persistence/init.lua b/acf2/persistence/init.lua index 1dca61d..cd043f4 100644 --- a/acf2/persistence/init.lua +++ b/acf2/persistence/init.lua @@ -9,7 +9,9 @@ local loadmods = require('acf2.loader') local topology = require('acf2.model.root').topology local object = require('acf2.object') local pth = require('acf2.path') + local util = require('acf2.util') +local contains = util.contains local stringy = require('stringy') @@ -52,8 +54,17 @@ function DataStore:get(path) if t == 'string' then res = tostring(res) elseif t == 'number' then res = tonumber(res) + elseif t == 'boolean' then - res = (res and res ~= 'false') and true or false + if atype == 'string' then res = res:lower() end + if res == 1 or contains({'1', 't', 'true', 'y', 'yes'}, res) then + res = true + elseif res == 0 or contains( + {'0', 'f', 'false', 'n', 'no'}, res + ) then + res = false + else res = res and true or false end + elseif t == 'reference' then assert(atype == 'string') else assert(false) end end diff --git a/acf2/persistence/util.lua b/acf2/persistence/util.lua index a657411..2491381 100644 --- a/acf2/persistence/util.lua +++ b/acf2/persistence/util.lua @@ -19,4 +19,10 @@ function M.read_file(path) return data end +function M.tostring(value) + -- TODO make values configurable per address + if type(value) == 'boolean' then return value and 'yes' or 'no' end + return tostring(value) +end + return M diff --git a/acf2/util.lua b/acf2/util.lua index 25e885b..a40f7fd 100644 --- a/acf2/util.lua +++ b/acf2/util.lua @@ -71,6 +71,15 @@ function M.keys(tbl) return res end +--- determine whether a value is present in an array. +-- @param list an array +-- @param value a value +-- @return a boolean +function M.contains(list, value) + for k, v in ipairs(list) do if v == value then return true end end + return false +end + --- map a function over a table. -- @param func a function with one argument -- @param tbl the table |