summaryrefslogtreecommitdiffstats
path: root/aconf/model/node.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-03-17 18:53:46 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-03-25 09:02:57 +0200
commitacbb01f353d7a156df33d7657f63a7c92fd9ace7 (patch)
tree7f63dbc6fca8a5987cc5498d1f5c9bcab2d38bab /aconf/model/node.lua
parent96b35ceb0b8e35b39acd7e8a3c30861739eaf01c (diff)
downloadaconf-acbb01f353d7a156df33d7657f63a7c92fd9ace7.tar.bz2
aconf-acbb01f353d7a156df33d7657f63a7c92fd9ace7.tar.xz
model: move fetch to TreeNode metatable
Diffstat (limited to 'aconf/model/node.lua')
-rw-r--r--aconf/model/node.lua89
1 files changed, 44 insertions, 45 deletions
diff --git a/aconf/model/node.lua b/aconf/model/node.lua
index 0f9c552..4ef45d8 100644
--- a/aconf/model/node.lua
+++ b/aconf/model/node.lua
@@ -81,6 +81,50 @@ function M.TreeNode:init(context, params)
end
function mt.get(k, options) return mt.load(k, options) end
+
+ function mt.fetch(path, create)
+ if type(path) == 'string' then
+ if pth.is_absolute(path) and mt.path > '/' then
+ assert(not create)
+ return mt.txn:fetch(path)
+ end
+ path = pth.split(path)
+ end
+
+ 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 getmetatable(mt.parent).fetch(path, create)
+ end
+
+ if not mt.member(name) then
+ raise(mt.path, 'Member does not exist: '..name)
+ end
+
+ local options = {}
+ if create then
+ options.create = true
+ if #path == 0 then options.dereference = false end
+ end
+ local next = mt.get(name, options)
+ if next == nil and (not create or #path > 0) then
+ raise(mt.path, 'Subordinate does not exist: '..name)
+ end
+ if #path == 0 then return next end
+
+ if type(next) ~= 'table' then
+ raise(pth.join(mt.path, name), 'Is a primitive value')
+ end
+
+ return getmetatable(next).fetch(path, create)
+ end
+
function mt.removable() return true end
function mt.member_removable(k)
@@ -117,50 +161,6 @@ function M.TreeNode:init(context, params)
mt.txn.validable[mt.path] = mt.addr
end
-function M.TreeNode:fetch(path, create)
- local mt = getmetatable(self)
-
- if type(path) == 'string' then
- if pth.is_absolute(path) and mt.path > '/' then
- assert(not create)
- return mt.txn:fetch(path)
- end
- path = pth.split(path)
- end
-
- 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
-
- local options = {}
- if create then
- options.create = true
- if #path == 0 then options.dereference = false end
- end
- local next = mt.get(name, options)
- if next == nil and (not create or #path > 0) then
- raise(mt.path, 'Subordinate does not exist: '..name)
- end
-
- if #path > 0 and type(next) ~= 'table' then
- raise(pth.join(mt.path, name), 'Is a primitive value')
- end
-
- return M.TreeNode.fetch(next, path, create)
-end
-
function M.TreeNode:search_refs(path)
if type(path) == 'string' then path = pth.split(path) end
@@ -202,7 +202,6 @@ function M.Collection:init(context, params)
)
self.init = nil
- self.fetch = nil
self.search_refs = nil
local mt = getmetatable(self)