summaryrefslogtreecommitdiffstats
path: root/acf/persistence
diff options
context:
space:
mode:
Diffstat (limited to 'acf/persistence')
-rw-r--r--acf/persistence/backends/augeas.lua2
-rw-r--r--acf/persistence/backends/files.lua4
-rw-r--r--acf/persistence/backends/json.lua4
-rw-r--r--acf/persistence/backends/null.lua2
-rw-r--r--acf/persistence/backends/volatile.lua2
-rw-r--r--acf/persistence/init.lua22
6 files changed, 27 insertions, 9 deletions
diff --git a/acf/persistence/backends/augeas.lua b/acf/persistence/backends/augeas.lua
index cf4614f..c33e09a 100644
--- a/acf/persistence/backends/augeas.lua
+++ b/acf/persistence/backends/augeas.lua
@@ -12,7 +12,7 @@ backend = require('acf.object').class()
function backend:init() self.aug = require('augeas').init() end
-function backend:get(path)
+function backend:get(path, tpe)
path = '/'..pth.mjoin(unpack(path))
local _, count = self.aug:match(path)
if count == 0 then return end
diff --git a/acf/persistence/backends/files.lua b/acf/persistence/backends/files.lua
index 31058a7..03eadff 100644
--- a/acf/persistence/backends/files.lua
+++ b/acf/persistence/backends/files.lua
@@ -16,7 +16,7 @@ backend = require('acf.object').class()
-- TODO cache expiration
function backend:init() self.cache = {} end
-function backend:get(path)
+function backend:get(path, tpe)
local name = pth.mjoin('/', unpack(path))
if not self.cache[name] then
@@ -59,7 +59,7 @@ function backend:set(mods)
else
local file = util.open_file(name, 'w')
- file:write(value)
+ file:write(tostring(value))
file:close()
self.cache[name] = value
diff --git a/acf/persistence/backends/json.lua b/acf/persistence/backends/json.lua
index 1ac977e..57352d8 100644
--- a/acf/persistence/backends/json.lua
+++ b/acf/persistence/backends/json.lua
@@ -51,12 +51,12 @@ function backend:split_path(path)
end
end
-function backend:get(path)
+function backend:get(path, tpe)
local fpath, jpath = self:split_path(path)
if not self.cache[fpath] then
self.cache[fpath] = Cache(json.decode(util.read_file(fpath)))
end
- return self.cache[fpath]:get(jpath)
+ return self.cache[fpath]:get(jpath, tpe)
end
function backend:set(mods)
diff --git a/acf/persistence/backends/null.lua b/acf/persistence/backends/null.lua
index 62a793f..45b6f22 100644
--- a/acf/persistence/backends/null.lua
+++ b/acf/persistence/backends/null.lua
@@ -6,5 +6,5 @@ See LICENSE file for license details
module(..., package.seeall)
backend = require('acf.object').class()
-function backend:get(path) if #path == 0 then return {} end end
+function backend:get(path, tpe) if #path == 0 then return {} end end
function backend:set(mods) end
diff --git a/acf/persistence/backends/volatile.lua b/acf/persistence/backends/volatile.lua
index 63d7265..165e7fc 100644
--- a/acf/persistence/backends/volatile.lua
+++ b/acf/persistence/backends/volatile.lua
@@ -29,7 +29,7 @@ function backend:_get(path)
return res
end
-function backend:get(path)
+function backend:get(path, tpe)
local res = self:_get(path)
return type(res) == 'table' and keys(res) or res
end
diff --git a/acf/persistence/init.lua b/acf/persistence/init.lua
index de23a68..7b10002 100644
--- a/acf/persistence/init.lua
+++ b/acf/persistence/init.lua
@@ -29,9 +29,27 @@ function DataStore:split_path(path)
return backend, comps
end
-function DataStore:get(path)
+function DataStore:get(path, t)
local backend, comps = self:split_path(path)
- return util.copy(backend:get(comps)), self.mod_time[path] or 0
+ local res = backend:get(comps, t)
+
+ if t ~= nil and res ~= nil then
+ local atype = type(res)
+
+ if t == 'table' then assert(atype == 'table')
+
+ else
+ assert(atype ~= 'table')
+
+ if t == 'string' then res = tostring(res)
+ elseif t == 'number' then res = tonumber(res)
+ elseif t == 'boolean' then res = res and true or false
+ elseif t == 'reference' then assert(atype == 'string')
+ else assert(false) end
+ end
+ end
+
+ return util.copy(res), self.mod_time[path] or 0
end
function DataStore:_set_multiple(mods)