From e124eec0e86584a52ca1559459f58a99c5a0847c Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sun, 24 Aug 2008 18:06:51 +0000 Subject: [PATCH] * luci/libs: uvl: implement "valueof" option --- libs/uvl/luasrc/uvl.lua | 56 ++++++++++++++++++++++++++++++++++++--- libs/uvl/luasrc/uvl/loghelper.lua | 2 +- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/libs/uvl/luasrc/uvl.lua b/libs/uvl/luasrc/uvl.lua index d384564c9..cc1d8b9d6 100644 --- a/libs/uvl/luasrc/uvl.lua +++ b/libs/uvl/luasrc/uvl.lua @@ -347,11 +347,11 @@ function UVL._validate_option( self, option, nodeps ) % option:cid() end - if item.type == "enum" and val then + if ( item.type == "reference" or item.type == "enum" ) and val then if not item.values or not item.values[val] then return false, - 'Value "%s" of given option "%s" is not defined in enum { %s }' - %{ val or '', option:cid(), + 'Value "%s" of given option "%s" is not defined in %s { %s }' + %{ val or '', option:cid(), item.type, table.concat( luci.util.keys(item.values), ", " ) } end elseif item.type == "list" and val then @@ -552,10 +552,20 @@ function UVL._read_scheme_parts( self, scheme, schemes ) 'validator specification in "%s"', v.name, scheme, k ) + elseif k == "valueof" then + local values, err = self:_read_reference( v2 ) + + _assert( values, + 'Variable "%s" in scheme "%s" has invalid ' .. + 'reference specification:\n%s', + v.name, scheme, err ) + + t.type = "reference" + t.values = values elseif k == "required" then t[k] = _bool(v2) else - t[k] = v2 + t[k] = t[k] or v2 end end end @@ -675,6 +685,44 @@ function UVL._read_validator( self, values, validators ) end end +-- Read a reference specification (XXX: We should validate external configs too...) +function UVL._read_reference( self, values ) + local val = { } + values = ( type(values) == "table" and values or { values } ) + + for _, value in ipairs(values) do + local ref = luci.util.split(value, ".") + + if #ref == 2 or #ref == 3 then + self.uci.load_config(ref[1]) + local co = self.uci.get_all(ref[1]) + + if not co then + return nil, 'Can not load config "%s" for reference "%s"' + %{ ref[1], value } + end + + for k, v in pairs(co) do + if v['.type'] == ref[2] then + if #ref == 2 then + if v['.anonymous'] == true then + return nil, 'Illegal reference "%s" to an anonymous section' + % value + end + table.insert( val, k ) + elseif v[ref[3]] then + table.insert( val, v[ref[3]] ) + end + end + end + else + return nil, 'Malformed reference "%s"' % value + end + end + + return val, nil +end + -- Resolve given path function UVL._resolve_function( self, value ) local path = luci.util.split(value, ".") diff --git a/libs/uvl/luasrc/uvl/loghelper.lua b/libs/uvl/luasrc/uvl/loghelper.lua index c36c97961..91da1ac35 100644 --- a/libs/uvl/luasrc/uvl/loghelper.lua +++ b/libs/uvl/luasrc/uvl/loghelper.lua @@ -76,6 +76,6 @@ function id( c, s, o ) if type(c) == "table" then c, s, o = unpack(c) end - + return c .. ( s and '.' .. s or '' ) .. ( o and '.' .. o or '' ) end -- 2.11.0