* luci/libs: uvl - cleanup round #1
[project/luci.git] / libs / uvl / luasrc / uvl / dependencies.lua
1 --[[
2
3 UCI Validation Layer - Dependency helper
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
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
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17 module( "luci.uvl.dependencies", package.seeall )
18
19 local function _assert( condition, fmt, ... )
20         if not condition then
21                 return assert( nil, string.format( fmt, ... ) )
22         else
23                 return condition
24         end
25 end
26
27
28 function _parse_reference( r, c, s, o )
29         local ref  = { }
30         local vars = {
31                 config  = c,
32                 section = s,
33                 option  = o
34         }
35
36         for i, v in ipairs(luci.util.split(r,".")) do
37                 table.insert( ref, (v:gsub( "%$(.+)", function(n) return vars[n] end )) )
38         end
39
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) }
44         elseif #ref ~= 3 then
45                 ref = nil
46         end
47
48         return ref
49 end
50
51 function check( self, object, nodeps )
52
53         if not self.beenthere[object:cid()] then
54                 self.beenthere[object:cid()] = true
55         else
56                 return false, "Recursive dependency for '" .. object:sid() .. "' found"
57         end
58
59         local item = object.type == luci.uvl.TYPE_SECTION
60                 and object:section() or object:option()
61
62         if item.depends then
63                 local ok = false
64                 local valid, err
65
66                 for _, dep in ipairs(item.depends) do
67                         local subcondition = true
68                         for k, v in pairs(dep) do
69                                 -- XXX: better error
70                                 local ref = _parse_reference( k, unpack(object.cref) )
71
72                                 if not ref then
73                                         return false, "Ambiguous dependency reference '" .. k ..
74                                                 "' for object '" .. object:sid() .. "' given"
75                                 end
76
77                                 local option = luci.uvl.option(
78                                         self, object.config,
79                                         object.config[ref[2]]
80                                                 and object.config[ref[2]]['.type']
81                                                 or  object.sref[2],
82                                         ref[1], ref[2], ref[3]
83                                 )
84
85                                 valid, err = self:_validate_option( option, true )
86                                 if valid then
87                                         if not (
88                                                 ( type(v) == "boolean" and object.config[ref[2]][ref[3]] ) or
89                                                 ( ref[3] and object.config[ref[2]][ref[3]] ) == v
90                                         ) then
91                                                 subcondition = false
92                                                 err = type(v) ~= "boolean"
93                                                         and "Option '" .. table.concat( ref, "." ) ..
94                                                                 "' doesn't match requested value '" .. v .. '"'
95                                                         or  "Option '" .. table.concat( ref, "." ) ..
96                                                                 "' has no value"
97
98                                                 break
99                                         end
100                                 else
101                                         subcondition = false
102                                         break
103                                 end
104                         end
105
106                         if subcondition then
107                                 return true
108                         end
109                 end
110
111                 return false, err
112         end
113
114         return true
115 end