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 function _assert( condition, fmt, ... )
21 return assert( nil, string.format( fmt, ... ) )
28 function _parse_reference( r, c, s, o )
36 for i, v in ipairs(luci.util.split(r,".")) do
37 table.insert( ref, (v:gsub( "%$(.+)", function(n) return vars[n] end )) )
40 if #ref == 1 and c and s then
41 ref = { c, s, ref[1] }
42 elseif #ref == 2 and c then
43 ref = { c, unpack(ref) }
51 function check( self, object, nodeps )
53 if not self.beenthere[object:cid()] then
54 self.beenthere[object:cid()] = true
56 return false, "Recursive dependency for '" .. object:sid() .. "' found"
59 local item = object.type == luci.uvl.TYPE_SECTION
60 and object:section() or object:option()
66 for _, dep in ipairs(item.depends) do
67 local subcondition = true
68 for k, v in pairs(dep) do
70 local ref = _parse_reference( k, unpack(object.cref) )
73 return false, "Ambiguous dependency reference '" .. k ..
74 "' for object '" .. object:sid() .. "' given"
77 local option = luci.uvl.option(
80 and object.config[ref[2]]['.type']
82 ref[1], ref[2], ref[3]
85 valid, err = self:_validate_option( option, true )
88 ( type(v) == "boolean" and object.config[ref[2]][ref[3]] ) or
89 ( ref[3] and object.config[ref[2]][ref[3]] ) == v
92 err = type(v) ~= "boolean"
93 and "Option '" .. table.concat( ref, "." ) ..
94 "' doesn't match requested value '" .. v .. '"'
95 or "Option '" .. table.concat( ref, "." ) ..