summaryrefslogtreecommitdiffstats
path: root/acf2/persistence
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-10-14 19:19:50 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-10-14 19:49:57 +0300
commite55dc995617a19fbc5772d7bd9fb3082a2d2b1f4 (patch)
tree33ddd87a1e3f0976da2d0817bd09ebc2169c2d56 /acf2/persistence
parentd15bdd8d9177e0b74a5f7a675ad09ea15a04bc86 (diff)
downloadacf2-e55dc995617a19fbc5772d7bd9fb3082a2d2b1f4.tar.bz2
acf2-e55dc995617a19fbc5772d7bd9fb3082a2d2b1f4.tar.xz
persistence: boolean/string conversion
Diffstat (limited to 'acf2/persistence')
-rw-r--r--acf2/persistence/backends/augeas.lua3
-rw-r--r--acf2/persistence/backends/files.lua2
-rw-r--r--acf2/persistence/init.lua13
-rw-r--r--acf2/persistence/util.lua6
4 files changed, 21 insertions, 3 deletions
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