diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-12-21 11:28:17 +0200 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-12-21 12:40:21 +0200 |
commit | 87e651fc120f62603079b66eaa5f65ec0a238018 (patch) | |
tree | b73ea9fd4b2ffbdfefc3382307520af6bb598347 | |
parent | 7aa2c8e1200a2a57a3ba064f117d66a47224efff (diff) | |
download | acf2-87e651fc120f62603079b66eaa5f65ec0a238018.tar.bz2 acf2-87e651fc120f62603079b66eaa5f65ec0a238018.tar.xz |
model: make TreeNode.fetch work properly with parent references
-rw-r--r-- | acf2/model/node.lua | 14 | ||||
-rw-r--r-- | acf2/path.lua | 6 |
2 files changed, 14 insertions, 6 deletions
diff --git a/acf2/model/node.lua b/acf2/model/node.lua index 2d9aa98..bebad93 100644 --- a/acf2/model/node.lua +++ b/acf2/model/node.lua @@ -128,6 +128,15 @@ function M.TreeNode:fetch(path, create) if #path == 0 then return self end local name = path[1] + table.remove(path, 1) + + if name == pth.up then + if not mt.parent then + raise(mt.path, 'Root object does not have parent') + end + return M.TreeNode.fetch(mt.parent, path, create) + end + if not mt.member(name) then raise(mt.path, 'Member does not exist: '..name) end @@ -135,14 +144,13 @@ function M.TreeNode:fetch(path, create) local options = {} if create then options.create = true - if #path == 1 then options.dereference = false end + if #path == 0 then options.dereference = false end end local next = mt.get(name, options) - if next == nil and (not create or #path > 1) then + if next == nil and (not create or #path > 0) then raise(mt.path, 'Subordinate does not exist: '..name) end - table.remove(path, 1) if #path > 0 and type(next) ~= 'table' then raise(pth.join(mt.path, name), 'Is a primitive value') end diff --git a/acf2/path.lua b/acf2/path.lua index d080c42..431f0df 100644 --- a/acf2/path.lua +++ b/acf2/path.lua @@ -8,9 +8,9 @@ local M = {} local map = require('acf2.util').map -local up = {} +M.up = {} M.wildcard = {} -local special = {['..']=up, ['*']=M.wildcard} +local special = {['..']=M.up, ['*']=M.wildcard} function M.is_absolute(path) return path:sub(1, 1) == '/' end @@ -93,7 +93,7 @@ function M.to_absolute(path, base) local comps = M.split(path) local i = 1 while i <= #comps do - if comps[i] == up then + if comps[i] == M.up then if i == 1 then error('Invalid path: '..path) end table.remove(comps, i - 1) table.remove(comps, i - 1) |