diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-06-26 13:56:18 +0300 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-06-28 13:24:35 +0300 |
commit | d60c9796b083a3617ce1d46fdc0e04aa2550f1bf (patch) | |
tree | ee6f25f1db9e3845dfe54d27388f34058157da37 /acf | |
parent | a9abe900412fb692c25d359a5f1fe79a1a79c97c (diff) | |
download | aconf-d60c9796b083a3617ce1d46fdc0e04aa2550f1bf.tar.bz2 aconf-d60c9796b083a3617ce1d46fdc0e04aa2550f1bf.tar.xz |
acf.path: support for wildcards in path names
Diffstat (limited to 'acf')
-rw-r--r-- | acf/model/node.lua | 6 | ||||
-rw-r--r-- | acf/path.lua | 51 |
2 files changed, 35 insertions, 22 deletions
diff --git a/acf/model/node.lua b/acf/model/node.lua index a58124d..380f809 100644 --- a/acf/model/node.lua +++ b/acf/model/node.lua @@ -36,7 +36,9 @@ function BoundMember:init(parent, field) txn=pmt.txn, parent=parent, path=pth.join(pmt.path, name), - addr=pth.to_absolute(field.addr or name, pmt.addr) + addr=pth.to_absolute( + field.addr or pth.escape(name), pmt.addr + ) }, unpack(arg) ) @@ -118,7 +120,7 @@ function Collection:init(context, params) local mt = getmetatable(self) mt.meta.type = 'collection' - mt.meta.members = field:meta('$') + mt.meta.members = field:meta(pth.wildcard) mt.meta['ui-member'] = params.ui_member or string.gsub( mt.meta['ui-name'], 's$', '' ) diff --git a/acf/path.lua b/acf/path.lua index b915012..3a5647d 100644 --- a/acf/path.lua +++ b/acf/path.lua @@ -8,32 +8,23 @@ module(..., package.seeall) local map = require('acf.util').map +local up = {} +wildcard = {} +local special = {['..']=up, ['*']=wildcard} + + function is_absolute(path) return string.sub(path, 1, 1) == '/' end -function to_absolute(path, base) - if not is_absolute(path) then - path = base..(base ~= '/' and '/' or '')..path - end - local comps = split(path) - local i = 1 - while i <= #comps do - if comps[i] == '..' then - if i == 1 then error('Invalid path: '..path) end - table.remove(comps, i - 1) - table.remove(comps, i - 1) - i = i - 1 - else i = i + 1 end - end - return '/'..table.concat(comps, '/') -end - -local function escape(comp) +function escape(comp) + for symbol, item in pairs(special) do + if comp == item then return symbol end + end if type(comp) == 'number' then return tostring(comp) end local res = string.gsub(comp, '([\\/])', '\\%1') - return tonumber(res) and '\\'..res or res + return (special[res] or tonumber(res)) and '\\'..res or res end @@ -52,7 +43,9 @@ function split(path) local escaped local function merge(s) - if s > '' then table.insert(res, not escaped and tonumber(s) or s) end + if s > '' then + table.insert(res, not escaped and (special[s] or tonumber(s)) or s) + end end while true do @@ -77,6 +70,24 @@ function split(path) end +function to_absolute(path, base) + if not is_absolute(path) then + path = base..(base ~= '/' and '/' or '')..path + end + local comps = split(path) + local i = 1 + while i <= #comps do + if comps[i] == up then + if i == 1 then error('Invalid path: '..path) end + table.remove(comps, i - 1) + table.remove(comps, i - 1) + i = i - 1 + else i = i + 1 end + end + return join('/', unpack(comps)) +end + + function parent(path) local comps = split(path) table.remove(comps) |