From 9843bb8363978b1556d3ccc79bd4c08027af7bfe Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Wed, 11 Mar 2015 13:27:01 +0200 Subject: persistence: string-based augeas back-end modes --- aconf/persistence/backends/augeas.lua | 58 +++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 26 deletions(-) (limited to 'aconf/persistence/backends/augeas.lua') diff --git a/aconf/persistence/backends/augeas.lua b/aconf/persistence/backends/augeas.lua index 2210346..46d3ea9 100644 --- a/aconf/persistence/backends/augeas.lua +++ b/aconf/persistence/backends/augeas.lua @@ -4,13 +4,8 @@ See LICENSE file for license details --]] local topology = require('aconf.model.root').topology - -local object = require('aconf.object') -local class = object.class -local isinstance = object.isinstance - +local class = require('aconf.object').class local address = require('aconf.path.address') -local special = require('aconf.path.address.special') local tostr = require('aconf.persistence.util').tostring local util = require('aconf.util') @@ -18,6 +13,9 @@ local contains = util.contains local copy = util.copy +local stringy = require('stringy') + + local function array_join(tbl, value) local res = copy(tbl) table.insert(res, value) @@ -45,7 +43,14 @@ local function append_key_pred(path, key, value) return append_pred(path, key.." = '"..value.."'") end -local function key(mode) return mode.key or '.' end +local function key(mode) + if mode == 'value' then return '.' end + if mode and stringy.startswith(mode, 'child-value:') then + return mode:sub(13, -1) + end +end + +local function is_selector(mode) return mode and mode ~= 'parent-value' end @@ -69,15 +74,15 @@ local function conv_path(path) local comp = path[1] if mode then - if isinstance(mode, special.EnumKeys) then + if mode == 'enumerate' then assert(type(comp) == 'number') res = append_pred(res, comp) - elseif isinstance(mode, special.ValueEquals) then - assert(type(comp) == 'string' and comp:match('^[%w %_%-%.%#]+$')) + else local k = key(mode) + assert(k and type(comp) == 'string' and comp:match('^[%w %_%-%.%#]+$')) res = append_key_pred(res, k, comp) table.insert(keys, k) - else assert(false) end + end parent = nil @@ -93,7 +98,7 @@ local function conv_path(path) table.remove(path, 1) until #path == 0 - if isinstance(mode, special.Value) then + if mode == 'parent-value' then assert(parent) res = parent end @@ -116,17 +121,16 @@ function backend:get(path, top) if tbl and top.subtype == 'model' then mode = nil end local leaf = not tbl and not mode - if isinstance(mode, special.Value) then + if mode == 'parent-value' then assert(not tbl) leaf = true end local matches = self.aug:match(apath..(not leaf and not mode and '/*' or '')) - local is_selector = isinstance(mode, special.Selector) - if #matches == 0 and not is_selector then return end + if #matches == 0 and not is_selector(mode) then return end - if is_selector and #path > 1 and not self:get( + if is_selector(mode) and #path > 1 and not self:get( array_without_last(path), true ) then return @@ -135,7 +139,7 @@ function backend:get(path, top) if not tpe and not mode then if #matches > 1 then leaf = false - mode = address.special.enum_keys + mode = 'enumerate' else local children = self.aug:match(apath..'/*') if #children > 0 then @@ -160,10 +164,12 @@ function backend:get(path, top) name = basename(child) name = tonumber(name) or name if contains(keys, name) then name = nil end - elseif isinstance(mode, special.EnumKeys) then name = i - elseif isinstance(mode, special.ValueEquals) then - name = self.aug:get(child..(mode.key and '/'..mode.key or '')) - else assert(false) end + elseif mode == 'enumerate' then name = i + else + local k = key(mode) + assert(k) + name = self.aug:get(child..(k == '.' and '' or '/'..k)) + end if name and self:get(array_join(path, name), true) then names[name] = true @@ -187,10 +193,10 @@ function backend:set(mods) local parent = array_without_last(path) local ppath, pmode = conv_path(parent) - if isinstance(pmode, special.Selector) then + if is_selector(pmode) then gc[address.join('/', table.unpack(parent))] = false - if isinstance(pmode, special.EnumKeys) then + if pmode == 'enumerate' then local count = #self.aug:match(ppath) while count < name do insert(parent, true) @@ -205,9 +211,9 @@ function backend:set(mods) if count > 0 and not new then return apath, keys end - if isinstance(pmode, special.ValueEquals) then - apath = pmode.key and append_pred( - ppath, 'count('..pmode.key..') = 0' + if key(pmode) then + apath = key(pmode) and append_pred( + ppath, 'count('..key(pmode)..') = 0' ) or append_key_pred(ppath, '.', '') matches = self.aug:match(apath) -- cgit v1.2.3