3 UCI Validation Layer - Dependency helper
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 module( "luci.uvl.dependencies", package.seeall )
19 local ERR = luci.uvl.errors
21 function _parse_reference( r, c, s, o )
29 for v in r:gmatch("[^.]+") do
30 table.insert(ref, (v:gsub( "%$(.+)", vars )))
34 table.insert(ref, 1, s or '$section')
37 table.insert(ref, 1, c or '$config')
43 function _serialize_dependency( dep, v )
46 for k, v in luci.util.spairs( dep,
48 a = ( type(dep[a]) ~= "boolean" and "_" or "" ) .. a
49 b = ( type(dep[b]) ~= "boolean" and "_" or "" ) .. b
53 str = ( str and str .. " and " or "" ) .. k ..
54 ( type(v) ~= "boolean" and "=" .. v or "" )
60 function check( self, object, nodeps )
62 local derr = ERR.DEPENDENCY(object)
64 if not self.depseen[object:cid()] then
65 self.depseen[object:cid()] = true
67 return false, derr:child(ERR.DEP_RECURSIVE(object))
70 if object:scheme('depends') then
74 for _, dep in ipairs(object:scheme('depends')) do
75 local subcondition = true
76 for k, v in pairs(dep) do
78 local ref = _parse_reference( k, unpack(object.cref) )
81 return false, derr:child(ERR.SME_BADDEP(object,k))
84 local option = luci.uvl.option( self, object.c, unpack(ref) )
86 valid, err = self:_validate_option( option, true )
89 ( type(v) == "boolean" and option:value() ) or
90 ( ref[3] and option:value() ) == v
94 local depstr = _serialize_dependency( dep, v )
97 and ERR.DEP_NOVALUE(option, depstr)
98 or ERR.DEP_NOTEQUAL(option, {depstr, v})
106 local depstr = _serialize_dependency( dep, v )
107 derr:child(ERR.DEP_NOTVALID(option, depstr):child(err))
128 if object:scheme("type") == "enum" and
129 object:scheme("enum_depends")[object:value()]
133 local enum = object:enum()
134 local eerr = ERR.DEP_BADENUM(enum)
136 for _, dep in ipairs(enum:scheme('enum_depends')[object:value()]) do
137 local subcondition = true
138 for k, v in pairs(dep) do
140 local ref = _parse_reference( k, unpack(object.cref) )
143 return false, derr:child(eerr:child(ERR.SME_BADDEP(enum,k)))
146 local option = luci.uvl.option( self, object.c, unpack(ref) )
148 valid, err = self:_validate_option( option, true )
151 ( type(v) == "boolean" and object.config[ref[2]][ref[3]] ) or
152 ( ref[3] and object:config() ) == v
156 local depstr = _serialize_dependency( dep, v )
159 and ERR.DEP_NOVALUE(option, depstr)
160 or ERR.DEP_NOTEQUAL(option, {depstr, v})
168 local depstr = _serialize_dependency( dep, v )
169 eerr:child(ERR.DEP_NOTVALID(option, depstr):child(err))
183 return false, derr:child(eerr)