diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-05-01 11:44:11 +0300 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-05-01 14:11:00 +0300 |
commit | b5cf596d6c17fc9c8b7c679fe445a9c70af2a16e (patch) | |
tree | aa649c1ff98961f1b6bbc1fef983a8ee9adc1c07 /acf | |
parent | 8a1ecf4ef917a47119c269f3adcf78cf082e0e55 (diff) | |
download | aconf-b5cf596d6c17fc9c8b7c679fe445a9c70af2a16e.tar.bz2 aconf-b5cf596d6c17fc9c8b7c679fe445a9c70af2a16e.tar.xz |
allow numerical components in path names
use escape syntax to encode number-looking strings
Diffstat (limited to 'acf')
-rw-r--r-- | acf/loader.lua | 2 | ||||
-rw-r--r-- | acf/model/init.lua | 2 | ||||
-rw-r--r-- | acf/path.lua | 20 | ||||
-rw-r--r-- | acf/persistence/backends/augeas.lua | 2 | ||||
-rw-r--r-- | acf/persistence/backends/files.lua | 6 | ||||
-rw-r--r-- | acf/persistence/backends/json.lua | 4 | ||||
-rw-r--r-- | acf/persistence/backends/volatile.lua | 4 |
7 files changed, 23 insertions, 17 deletions
diff --git a/acf/loader.lua b/acf/loader.lua index ba28245..95545b7 100644 --- a/acf/loader.lua +++ b/acf/loader.lua @@ -12,7 +12,7 @@ require 'stringy' function loadmods(subdir) local comps = pth.split('acf/'..subdir) local res = {} - for _, modfile in ipairs(posix.dir(pth.mjoin(unpack(comps)))) do + for _, modfile in ipairs(posix.dir(pth.join(unpack(comps)))) do if stringy.endswith(modfile, '.lua') then local name = string.sub(modfile, 1, -5) res[name] = require(table.concat(comps, '.')..'.'..name) diff --git a/acf/model/init.lua b/acf/model/init.lua index 351c1a2..ed06548 100644 --- a/acf/model/init.lua +++ b/acf/model/init.lua @@ -146,7 +146,7 @@ function Reference:btype(context) end function Reference:follow(context, value) - return context.txn:search(pth.mjoin(self:abs_scope(context), value)) + return context.txn:search(pth.rawjoin(self:abs_scope(context), value)) end function Reference:load(context) diff --git a/acf/path.lua b/acf/path.lua index f18d701..b915012 100644 --- a/acf/path.lua +++ b/acf/path.lua @@ -31,25 +31,29 @@ end local function escape(comp) + if type(comp) == 'number' then return tostring(comp) end local res = string.gsub(comp, '([\\/])', '\\%1') - return res + return tonumber(res) and '\\'..res or res end -function join(parent, name) return mjoin(parent, escape(name)) end - -function mjoin(p1, p2, ...) +function rawjoin(p1, p2, ...) if not p2 then return p1 end if not is_absolute(p2) then p2 = '/'..p2 end - return mjoin((p1 == '/' and '' or p1)..p2, unpack(arg)) + return rawjoin((p1 == '/' and '' or p1)..p2, unpack(arg)) end +function join(parent, ...) return rawjoin(parent, unpack(map(escape, arg))) end + function split(path) local res = {} local comp = '' + local escaped - local function merge(s) if s > '' then table.insert(res, s) end end + local function merge(s) + if s > '' then table.insert(res, not escaped and tonumber(s) or s) end + end while true do local prefix, sep, suffix = string.match(path, '([^\\/]*)([\\/])(.*)') @@ -61,10 +65,12 @@ function split(path) comp = comp..prefix if sep == '\\' then comp = comp..string.sub(suffix, 1, 1) + escaped = true path = string.sub(suffix, 2, -1) else merge(comp) comp = '' + escaped = false path = suffix end end @@ -74,7 +80,7 @@ end function parent(path) local comps = split(path) table.remove(comps) - return mjoin('/', unpack(map(escape, comps))) + return join('/', unpack(comps)) end function name(path) diff --git a/acf/persistence/backends/augeas.lua b/acf/persistence/backends/augeas.lua index c33e09a..d3fff0f 100644 --- a/acf/persistence/backends/augeas.lua +++ b/acf/persistence/backends/augeas.lua @@ -13,7 +13,7 @@ backend = require('acf.object').class() function backend:init() self.aug = require('augeas').init() end function backend:get(path, tpe) - path = '/'..pth.mjoin(unpack(path)) + path = pth.join('/', 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 03eadff..e123e21 100644 --- a/acf/persistence/backends/files.lua +++ b/acf/persistence/backends/files.lua @@ -17,7 +17,7 @@ backend = require('acf.object').class() function backend:init() self.cache = {} end function backend:get(path, tpe) - local name = pth.mjoin('/', unpack(path)) + local name = pth.join('/', unpack(path)) if not self.cache[name] then local t = posix.stat(name, 'type') @@ -30,7 +30,7 @@ function backend:get(path, tpe) local res = {} for _, fname in ipairs(posix.dir(name)) do if not ({['.']=true, ['..']=true})[fname] then - table.insert(res, fname) + table.insert(res, pth.name(fname)) end end return res @@ -46,7 +46,7 @@ end function backend:set(mods) for _, mod in pairs(mods) do local path, t, value = unpack(mod) - local name = pth.mjoin('/', unpack(path)) + local name = pth.join('/', unpack(path)) -- TODO save references (t == 'reference') as symlinks diff --git a/acf/persistence/backends/json.lua b/acf/persistence/backends/json.lua index 57352d8..65fc987 100644 --- a/acf/persistence/backends/json.lua +++ b/acf/persistence/backends/json.lua @@ -28,7 +28,7 @@ function backend:split_path(path) local res while #fpath > 0 do - local fp = pth.mjoin('/', unpack(fpath)) + local fp = pth.join('/', unpack(fpath)) if self.cache[fp] then return fp, jpath end table.insert(jpath, 1, fpath[#fpath]) table.remove(fpath) @@ -37,7 +37,7 @@ function backend:split_path(path) fpath = '/' while true do - fpath = pth.mjoin(fpath, jpath[1]) + fpath = pth.join(fpath, jpath[1]) table.remove(jpath, 1) local t = posix.stat(fpath, 'type') diff --git a/acf/persistence/backends/volatile.lua b/acf/persistence/backends/volatile.lua index 165e7fc..339f937 100644 --- a/acf/persistence/backends/volatile.lua +++ b/acf/persistence/backends/volatile.lua @@ -24,7 +24,7 @@ function backend:_get(path) for _, comp in ipairs(path) do if res == nil then return end assert(type(res) == 'table') - res = res[tonumber(comp) or comp] + res = res[comp] end return res end @@ -43,7 +43,7 @@ function backend:_set(path, tpe, value) local comps = copy(path) local name = comps[#comps] table.remove(comps) - self:_get(comps)[tonumber(name) or name] = value + self:_get(comps)[name] = value end end |