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 local uvl = require "luci.uvl"
18 local ERR = require "luci.uvl.errors"
19 local util = require "luci.util"
20 local table = require "table"
22 local type, unpack = type, unpack
23 local ipairs, pairs = ipairs, pairs
25 module "luci.uvl.dependencies"
29 function _parse_reference( r, c, s, o )
37 for v in r:gmatch("[^.]+") do
38 ref[#ref+1] = (v:gsub( "%$(.+)", vars ))
42 table.insert(ref, 1, s or '$section')
45 table.insert(ref, 1, c or '$config')
51 function _serialize_dependency( dep, v )
54 for k, v in util.spairs( dep,
56 a = ( type(dep[a]) ~= "boolean" and "_" or "" ) .. a
57 b = ( type(dep[b]) ~= "boolean" and "_" or "" ) .. b
61 str = ( str and str .. " and " or "" ) .. k ..
62 ( type(v) ~= "boolean" and "=" .. v or "" )
68 function check( self, object, nodeps )
70 local derr = ERR.DEPENDENCY(object)
72 if not self.depseen[object:cid()] then
73 self.depseen[object:cid()] = true
75 return false, derr:child(ERR.DEP_RECURSIVE(object))
78 if object:scheme('depends') then
82 for _, dep in ipairs(object:scheme('depends')) do
83 local subcondition = true
84 for k, v in pairs(dep) do
86 local ref = _parse_reference( k, unpack(object.cref) )
89 return false, derr:child(ERR.SME_BADDEP(object,k))
92 local option = uvl.option( self, object.c, unpack(ref) )
94 valid, err = self:_validate_option( option, true )
97 ( type(v) == "boolean" and option:value() ) or
98 ( ref[3] and option:value() ) == v
102 local depstr = _serialize_dependency( dep, v )
105 and ERR.DEP_NOVALUE(option, depstr)
106 or ERR.DEP_NOTEQUAL(option, {depstr, v})
114 local depstr = _serialize_dependency( dep, v )
115 derr:child(ERR.DEP_NOTVALID(option, depstr):child(err))
136 if object:scheme("type") == "enum" and
137 object:scheme("enum_depends")[object:value()]
141 local enum = object:enum()
142 local eerr = ERR.DEP_BADENUM(enum)
144 for _, dep in ipairs(enum:scheme('enum_depends')[object:value()]) do
145 local subcondition = true
146 for k, v in pairs(dep) do
148 local ref = _parse_reference( k, unpack(object.cref) )
151 return false, derr:child(eerr:child(ERR.SME_BADDEP(enum,k)))
154 local option = luci.uvl.option( self, object.c, unpack(ref) )
156 valid, err = self:_validate_option( option, true )
159 ( type(v) == "boolean" and object.config[ref[2]][ref[3]] ) or
160 ( ref[3] and object:config() ) == v
164 local depstr = _serialize_dependency( dep, v )
167 and ERR.DEP_NOVALUE(option, depstr)
168 or ERR.DEP_NOTEQUAL(option, {depstr, v})
176 local depstr = _serialize_dependency( dep, v )
177 eerr:child(ERR.DEP_NOTVALID(option, depstr):child(err))
191 return false, derr:child(eerr)