* luci/libs: uvl - cleanup round #1
[project/luci.git] / libs / uvl / luasrc / uvl / dependencies.lua
index b8d728657749cad2865203162af9cd03fcc3eff9..c5d0734e4c1b4c60b765e4bb158335108804ecd8 100644 (file)
@@ -1,6 +1,6 @@
 --[[
 
-UCI Validation Layer - Main Library
+UCI Validation Layer - Dependency helper
 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
 (c) 2008 Steven Barth <steven@midlink.org>
 
@@ -42,70 +42,58 @@ function _parse_reference( r, c, s, o )
        elseif #ref == 2 and c then
                ref = { c, unpack(ref) }
        elseif #ref ~= 3 then
-               print("INVALID REFERENCE: "..#ref, c, s, o)
                ref = nil
        end
 
        return ref
 end
 
-function check_dependency( self, uci, conf, sect, optn, nodeps, section2 )
+function check( self, object, nodeps )
 
---     print( "Depency check:    ", conf .. '.' .. sect .. ( optn and '.' .. optn or '' ) )
-
-       local key = conf .. '.' .. sect .. ( optn and '.' .. optn or '' )
-       if not self.beenthere[key] then
-               self.beenthere[key] = true
+       if not self.beenthere[object:cid()] then
+               self.beenthere[object:cid()] = true
        else
-               print("CIRCULAR DEPENDENCY!")
-               return false, "Recursive depency detected"
+               return false, "Recursive dependency for '" .. object:sid() .. "' found"
        end
 
-       -- check for config
-       if not self.packages[conf] then self:read_scheme(conf) end
-       local item = self.packages[conf]
-
-       -- check for section
-       if sect then
-               item = _assert( self:_scheme_section( uci, conf, sect ) or self:_scheme_section( uci, conf, section2 ),
-                       "Unknown section '%s' in scheme '%s' requested",
-                       sect or '<nil>', conf or '<nil>' )
-
-               -- check for option
-               if optn then
-                       item = _assert( self:_scheme_option( uci, conf, sect, optn ) or
-                                                       self:_scheme_option( uci, conf, section2, optn ),
-                               "Unknown variable '%s' in scheme '%s', section '%s' requested",
-                               optn or '<nil>', conf or '<nil>', sect or '<nil>' )
-               end
-       end
+       local item = object.type == luci.uvl.TYPE_SECTION
+               and object:section() or object:option()
 
        if item.depends then
                local ok = false
                local valid, err
 
                for _, dep in ipairs(item.depends) do
-                       --print("DEP:",luci.util.serialize_data(dep))
-
                        local subcondition = true
-
                        for k, v in pairs(dep) do
                                -- XXX: better error
-                               local ref = _assert( _parse_reference(k,conf,sect,optn),
-                                       "Ambiguous dependency reference '" .. k .. "' given" )
+                               local ref = _parse_reference( k, unpack(object.cref) )
+
+                               if not ref then
+                                       return false, "Ambiguous dependency reference '" .. k ..
+                                               "' for object '" .. object:sid() .. "' given"
+                               end
+
+                               local option = luci.uvl.option(
+                                       self, object.config,
+                                       object.config[ref[2]]
+                                               and object.config[ref[2]]['.type']
+                                               or  object.sref[2],
+                                       ref[1], ref[2], ref[3]
+                               )
 
-                               -- XXX: true -> nodeps
-                               valid, err = self:validate_option(ref[1], ref[2], ref[3], uci, true, section2)
+                               valid, err = self:_validate_option( option, true )
                                if valid then
-                                       --print("CHK:",uci[ref[2]][ref[3]],v,unpack(ref))
                                        if not (
-                                               ( type(v) == "boolean" and uci[ref[2]][ref[3]] ) or
-                                               ( ref[3] and uci[ref[2]][ref[3]] ) == v
+                                               ( type(v) == "boolean" and object.config[ref[2]][ref[3]] ) or
+                                               ( ref[3] and object.config[ref[2]][ref[3]] ) == v
                                        ) then
                                                subcondition = false
                                                err = type(v) ~= "boolean"
-                                                       and "Option '" .. table.concat( ref, "." ) .. "' doesn't match requested type '" .. v .. '"'
-                                                       or  "Option '" .. table.concat( ref, "." ) .. "' has no value"
+                                                       and "Option '" .. table.concat( ref, "." ) ..
+                                                               "' doesn't match requested value '" .. v .. '"'
+                                                       or  "Option '" .. table.concat( ref, "." ) ..
+                                                               "' has no value"
 
                                                break
                                        end
@@ -116,15 +104,12 @@ function check_dependency( self, uci, conf, sect, optn, nodeps, section2 )
                        end
 
                        if subcondition then
---                             print( " -> Success (condition matched)\n" )
                                return true
                        end
                end
 
---             print( " -> Failed\n" )
                return false, err
        end
 
---     print( " -> Success (no depends)\n" )
        return true
 end