3 UCI Validation Layer - Main Library
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
18 --- UVL - UCI Validation Layer
22 local fs = require "luci.fs"
23 local uci = require "luci.model.uci"
24 local util = require "luci.util"
25 local table = require "table"
26 local string = require "string"
28 local require, pcall, ipairs, pairs = require, pcall, ipairs, pairs
29 local type, error, tonumber, tostring = type, error, tonumber, tostring
30 local unpack, loadfile = unpack, loadfile
34 local ERR = require "luci.uvl.errors"
35 local datatypes = require "luci.uvl.datatypes"
36 local validation = require "luci.uvl.validation"
37 local dependencies = require "luci.uvl.dependencies"
39 local TYPE_SCHEME = 0x00
40 local TYPE_CONFIG = 0x01
41 local TYPE_SECTION = 0x02
42 local TYPE_OPTION = 0x03
43 local TYPE_ENUM = 0x04
45 --- Boolean; default true;
46 -- treat sections found in config but not in scheme as error
47 STRICT_UNKNOWN_SECTIONS = true
49 --- Boolean; default true;
50 -- treat options found in config but not in scheme as error
51 STRICT_UNKNOWN_OPTIONS = true
53 --- Boolean; default true;
54 -- treat failed external validators as error
55 STRICT_EXTERNAL_VALIDATORS = true
57 --- Boolean; default true;
58 -- treat list values stored as options like errors
59 STRICT_LIST_TYPE = true
62 local default_schemedir = "/lib/uci/schema"
63 local default_savedir = "/tmp/.uvl"
66 --- Object constructor
69 -- @param schemedir Path to the scheme directory (optional)
70 -- @return Instance object
73 function UVL.__init__( self, schemedir )
74 self.schemedir = schemedir or default_schemedir
80 self.dep = dependencies
81 self.datatypes = datatypes
85 --- Parse given scheme and return the scheme tree.
86 -- @param scheme Name of the scheme to parse
87 -- @return Table containing the parsed scheme or nil on error
88 -- @return String containing the reason for errors (if any)
89 function UVL.get_scheme( self, scheme )
90 if not self.packages[scheme] then
91 local ok, err = self:read_scheme( scheme )
96 return self.packages[scheme], nil
99 --- Validate given configuration, section or option.
100 -- @param config Name of the configuration to validate
101 -- @param section Name of the section to validate (optional)
102 -- @param option Name of the option to validate (optional)
103 -- @return Boolean indicating whether the given config validates
104 -- @return String containing the reason for errors (if any)
105 function UVL.validate( self, config, section, option )
106 if config and section and option then
107 return self:validate_option( config, section, option )
108 elseif config and section then
109 return self:validate_section( config, section )
111 return self:validate_config( config )
115 --- Validate given configuration.
116 -- @param cfg Name of the configuration to validate
117 -- @return Boolean indicating whether the given config validates
118 -- @return String containing the reason for errors (if any)
119 function UVL.validate_config( self, cfg, uci )
121 if not self.packages[cfg] then
122 local ok, err = self:read_scheme(cfg)
128 local co = config( self, uci or cfg, uci and cfg )
134 if not co:config() then
135 return false, co:errors()
138 local function _uci_foreach( type, func )
139 for k, v in pairs(co:config()) do
140 if v['.type'] == type then
141 sc[type] = sc[type] + 1
142 local ok, err = func( k, v )
143 if not ok then co:error(err) end
148 for k, v in pairs( self.packages[cfg].sections ) do
152 return self:_validate_section( co:section(s) )
157 if STRICT_UNKNOWN_SECTIONS then
158 for k, v in pairs(co:config()) do
159 local so = co:section(k)
160 if not self.beenthere[so:cid()] then
161 co:error(ERR.SECT_UNKNOWN(so))
166 for _, k in ipairs(util.keys(sc)) do
167 local so = co:section(k)
168 if so:scheme('required') and sc[k] == 0 then
169 co:error(ERR.SECT_REQUIRED(so))
170 elseif so:scheme('unique') and sc[k] > 1 then
171 co:error(ERR.SECT_UNIQUE(so))
175 return co:ok(), co:errors()
178 --- Validate given config section.
179 -- @param config Name of the configuration to validate
180 -- @param section Name of the section to validate
181 -- @return Boolean indicating whether the given config validates
182 -- @return String containing the reason for errors (if any)
183 function UVL.validate_section( self, cfg, section, uci )
185 if not self.packages[cfg] then
186 local ok, err = self:read_scheme( cfg )
192 local co = config( self, uci or cfg, uci and cfg )
193 local so = co:section( section )
198 if not co:config() then
199 return false, co:errors()
203 return self:_validate_section( so )
205 return false, ERR.SECT_NOTFOUND(so)
209 --- Validate given config option.
210 -- @param config Name of the configuration to validate
211 -- @param section Name of the section to validate
212 -- @param option Name of the option to validate
213 -- @return Boolean indicating whether the given config validates
214 -- @return String containing the reason for errors (if any)
215 function UVL.validate_option( self, cfg, section, option, uci )
217 if not self.packages[cfg] then
218 local ok, err = self:read_scheme( cfg )
224 local co = config( self, uci or cfg, uci and cfg )
225 local so = co:section( section )
226 local oo = so:option( option )
228 if not co:config() then
229 return false, co:errors()
232 if so:config() and oo:config() then
233 return self:_validate_option( oo )
235 return false, ERR.OPT_NOTFOUND(oo)
240 function UVL._validate_section( self, section )
242 self.beenthere[section:cid()] = true
244 if section:config() then
245 if section:scheme('named') == true and
246 section:config('.anonymous') == true
248 return false, ERR.SECT_NAMED(section)
251 for _, v in ipairs(section:variables()) do
252 local ok, err = self:_validate_option( v )
254 v:scheme('required') or v:scheme('type') == "enum" or (
255 not err:is(ERR.ERR_DEP_NOTEQUAL) and
256 not err:is(ERR.ERR_DEP_NOVALUE)
263 local ok, err = dependencies.check( self, section )
268 return false, ERR.SECT_NOTFOUND(section)
271 if STRICT_UNKNOWN_OPTIONS and not section:scheme('dynamic') then
272 for k, v in pairs(section:config()) do
273 local oo = section:option(k)
274 if k:sub(1,1) ~= "." and not self.beenthere[oo:cid()] then
275 section:error(ERR.OPT_UNKNOWN(oo))
280 return section:ok(), section:errors()
283 function UVL._validate_option( self, option, nodeps )
285 self.beenthere[option:cid()] = true
287 if not option:scheme() and not option:parent():scheme('dynamic') then
288 if STRICT_UNKNOWN_OPTIONS then
289 return false, option:error(ERR.OPT_UNKNOWN(option))
294 elseif option:scheme() then
296 local ok, err = dependencies.check( self, option )
298 -- XXX: maybe this needs to be more specific
299 if not err:is(ERR.ERR_DEP_NOTEQUAL) and
300 not err:is(ERR.ERR_DEP_NOVALUE)
303 return false, option:errors()
310 if option:scheme('required') and not option:value() then
311 return false, option:error(ERR.OPT_REQUIRED(option))
313 elseif option:value() then
314 local val = option:value()
316 if option:scheme('type') == "reference" or
317 option:scheme('type') == "enum"
319 local scheme_values = option:scheme('values') or { }
320 local config_values = ( type(val) == "table" and val or { val } )
321 for _, v in ipairs(config_values) do
322 if not scheme_values[v] then
323 return false, option:error( ERR.OPT_BADVALUE(
324 option, { v, util.serialize_data(
325 util.keys(scheme_values)
330 elseif option:scheme('type') == "list" then
331 if type(val) ~= "table" and STRICT_LIST_TYPE then
332 return false, option:error(ERR.OPT_NOTLIST(option))
336 if option:scheme('datatype') then
337 local dt = option:scheme('datatype')
339 if self.datatypes[dt] then
340 val = ( type(val) == "table" and val or { val } )
341 for i, v in ipairs(val) do
342 if not self.datatypes[dt]( v ) then
343 return false, option:error(
344 ERR.OPT_INVVALUE(option, { v, dt })
349 return false, option:error(ERR.OPT_DATATYPE(option, dt))
353 val = ( type(val) == "table" and val or { val } )
354 for _, v in ipairs(val) do
355 if option:scheme('minlength') then
356 if #v < option:scheme('minlength') then
357 return false, option:error(ERR.OPT_RANGE(option))
361 if option:scheme('maxlength') then
362 if #v > option:scheme('maxlength') then
363 return false, option:error(ERR.OPT_RANGE(option))
369 if option:scheme('minimum') then
370 if not v or v < option:scheme('minimum') then
371 return false, option:error(ERR.OPT_RANGE(option))
375 if option:scheme('maximum') then
376 if not v or v > option:scheme('maximum') then
377 return false, option:error(ERR.OPT_RANGE(option))
383 local ok, err = validation.check( self, option )
384 if not ok and STRICT_EXTERNAL_VALIDATORS then
385 return false, option:error(err)
389 return option:ok(), option:errors()
392 --- Find all parts of given scheme and construct validation tree.
393 -- This is normally done on demand, so you don't have to call this function
395 -- @param shm Name of the scheme to parse
396 -- @param alias Create an alias for the loaded scheme
397 function UVL.read_scheme( self, shm, alias )
399 local so = scheme( self, shm )
400 local bc = "%s/bytecode/%s.lua" %{ self.schemedir, shm }
402 if not fs.access(bc) then
403 local files = fs.glob(self.schemedir .. '/*/' .. shm)
407 for i, file in ipairs( files ) do
408 if not fs.access(file) then
409 return false, so:error(ERR.SME_READ(so,file))
412 local uci = uci.cursor( fs.dirname(file), default_savedir )
414 local sname = fs.basename(file)
415 local sd, err = uci:load( sname )
418 return false, ERR.UCILOAD(so, err)
421 ok, err = pcall(function()
422 uci:foreach(sname, "package", function(s)
423 self:_parse_package(so, s[".name"], s)
425 uci:foreach(sname, "section", function(s)
426 self:_parse_section(so, s[".name"], s)
428 uci:foreach(sname, "variable", function(s)
429 self:_parse_var(so, s[".name"], s)
431 uci:foreach(sname, "enum", function(s)
432 self:_parse_enum(so, s[".name"], s)
438 if ok and alias then self.packages[alias] = self.packages[shm] end
439 return ok and self, err
441 return false, so:error(ERR.SME_FIND(so, self.schemedir))
444 local sc = loadfile(bc)
446 self.packages[shm] = sc()
449 return false, so:error(ERR.SME_READ(so,bc))
454 -- helper function to check for required fields
455 local function _req( t, n, c, r )
456 for i, v in ipairs(r) do
458 local p, o = scheme:sid(), nil
460 if t == TYPE_SECTION then
461 o = section( scheme, nil, p, n )
462 elseif t == TYPE_OPTION then
463 o = option( scheme, nil, p, '(nil)', n )
464 elseif t == TYPE_ENUM then
465 o = enum( scheme, nil, p, '(nil)', '(nil)', n )
468 return false, ERR.SME_REQFLD(o,v)
474 -- helper function to validate references
475 local function _ref( c, t )
477 if c == TYPE_SECTION then
480 elseif c == TYPE_OPTION then
483 elseif c == TYPE_ENUM then
488 for o in t[k]:gmatch("[^.]+") do
491 r[1] = ( #r[1] > 0 and r[1] or scheme:sid() )
494 return false, ERR.SME_BADREF(scheme, k)
500 -- helper function to read bools
501 local function _bool( v )
502 return ( v == "true" or v == "yes" or v == "on" or v == "1" )
505 -- Step 0: get package meta information
506 function UVL._parse_package(self, scheme, k, v)
507 local sid = scheme:sid()
508 local pkg = self.packages[sid] or {
515 pkg.description = v.description
517 self.packages[sid] = pkg
520 -- Step 1: get all sections
521 function UVL._parse_section(self, scheme, k, v)
522 local ok, err = _req( TYPE_SECTION, k, v, { "name", "package" } )
523 if err then error(scheme:error(err)) end
525 local r, err = _ref( TYPE_SECTION, v )
526 if err then error(scheme:error(err)) end
528 local p = self.packages[r[1]] or {
533 p.sections[v.name] = p.sections[v.name] or { }
534 p.variables[v.name] = p.variables[v.name] or { }
535 self.packages[r[1]] = p
537 local s = p.sections[v.name]
538 local so = scheme:section(v.name)
540 for k, v2 in pairs(v) do
541 if k ~= "name" and k ~= "package" and k:sub(1,1) ~= "." then
542 if k == "depends" then
543 s.depends = self:_read_dependency( v2, s.depends )
544 if not s.depends then
545 return false, scheme:error(
546 ERR.SME_BADDEP(so, util.serialize_data(s.depends))
549 elseif k == "dynamic" or k == "unique" or
550 k == "required" or k == "named"
559 s.dynamic = s.dynamic or false
560 s.unique = s.unique or false
561 s.required = s.required or false
562 s.named = s.named or false
565 -- Step 2: get all variables
566 function UVL._parse_var(self, scheme, k, v)
567 local ok, err = _req( TYPE_OPTION, k, v, { "name", "section" } )
568 if err then error(scheme:error(err)) end
570 local r, err = _ref( TYPE_OPTION, v )
571 if err then error(scheme:error(err)) end
573 local p = self.packages[r[1]]
576 ERR.SME_VBADPACK({scheme:sid(), '', v.name}, r[1])
580 local s = p.variables[r[2]]
583 ERR.SME_VBADSECT({scheme:sid(), '', v.name}, r[2])
587 s[v.name] = s[v.name] or { }
590 local so = scheme:section(r[2])
591 local to = so:option(v.name)
593 for k, v2 in pairs(v) do
594 if k ~= "name" and k ~= "section" and k:sub(1,1) ~= "." then
595 if k == "depends" then
596 t.depends = self:_read_dependency( v2, t.depends )
597 if not t.depends then
598 error(scheme:error(so:error(
599 ERR.SME_BADDEP(to, util.serialize_data(v2))
602 elseif k == "validator" then
603 t.validators = self:_read_validator( v2, t.validators )
604 if not t.validators then
605 error(scheme:error(so:error(
606 ERR.SME_BADVAL(to, util.serialize_data(v2))
609 elseif k == "valueof" then
610 local values, err = self:_read_reference( v2 )
612 error(scheme:error(so:error(
613 ERR.REFERENCE(to, util.serialize_data(v2)):child(err)
618 t.valueof = type(v2) == "table" and v2 or {v2}
619 elseif k == "required" then
621 elseif k == "minlength" or k == "maxlength" or
622 k == "minimum" or k == "maximum"
631 t.type = t.type or "variable"
632 t.datatype = t.datatype or "string"
633 t.required = t.required or false
636 -- Step 3: get all enums
637 function UVL._parse_enum(self, scheme, k, v)
638 local ok, err = _req( TYPE_ENUM, k, v, { "value", "variable" } )
639 if err then error(scheme:error(err)) end
641 local r, err = _ref( TYPE_ENUM, v )
642 if err then error(scheme:error(err)) end
644 local p = self.packages[r[1]]
647 ERR.SME_EBADPACK({scheme:sid(), '', '', v.value}, r[1])
651 local s = p.variables[r[2]]
654 ERR.SME_EBADSECT({scheme:sid(), '', '', v.value}, r[2])
661 ERR.SME_EBADOPT({scheme:sid(), '', '', v.value}, r[3])
666 local so = scheme:section(r[2])
667 local oo = so:option(r[3])
668 local eo = oo:enum(v.value)
670 if t.type ~= "enum" and t.type ~= "reference" then
671 error(scheme:error(ERR.SME_EBADTYPE(eo)))
675 t.values = { [v.value] = v.title or v.value }
676 t.valuelist = { {value = v.value, title = v.title} }
678 t.values[v.value] = v.title or v.value
679 t.valuelist[#t.valuelist + 1] = {value = v.value, title = v.title}
682 if not t.enum_depends then
688 error(scheme:error(ERR.SME_EBADDEF(eo)))
694 t.enum_depends[v.value] = self:_read_dependency(
695 v.depends, t.enum_depends[v.value]
698 if not t.enum_depends[v.value] then
699 error(scheme:error(so:error(oo:error(
700 ERR.SME_BADDEP(eo, util.serialize_data(v.depends))
706 -- Read a dependency specification
707 function UVL._read_dependency( self, values, deps )
708 local expr = "%$?[%w_]+"
710 values = ( type(values) == "table" and values or { values } )
711 for _, value in ipairs(values) do
712 local condition = { }
713 for val in value:gmatch("[^,]+") do
714 local k, e, v = val:match("%s*([%w$_.]+)%s*(=?)%s*(.*)")
717 k:match("^"..expr.."%."..expr.."%."..expr.."$") or
718 k:match("^"..expr.."%."..expr.."$") or
719 k:match("^"..expr.."$")
721 condition[k] = (e == '=') and v or true
730 deps[#deps+1] = condition
738 -- Read a validator specification
739 function UVL._read_validator( self, values, validators )
741 values = ( type(values) == "table" and values or { values } )
742 for _, value in ipairs(values) do
745 if value:match("^exec:") then
746 validator = value:gsub("^exec:","")
747 elseif value:match("^lua:") then
748 validator = self:_resolve_function( (value:gsub("^lua:","") ) )
749 elseif value:match("^regexp:") then
750 local pattern = value:gsub("^regexp:","")
751 validator = function( type, dtype, pack, sect, optn, ... )
752 local values = { ... }
753 for _, v in ipairs(values) do
755 pcall( string.match, v, pattern )
759 elseif not match then
761 'Value "%s" does not match pattern "%s"' % {
771 if not validators then
772 validators = { validator }
774 validators[#validators+1] = validator
785 -- Read a reference specification (XXX: We should validate external configs too...)
786 function UVL._read_reference( self, values )
788 values = ( type(values) == "table" and values or { values } )
790 for _, value in ipairs(values) do
791 local ref = util.split(value, ".")
793 if #ref == 2 or #ref == 3 then
794 local co = config( self, ref[1] )
795 if not co:config() then return false, co:errors() end
797 for k, v in pairs(co:config()) do
798 if v['.type'] == ref[2] then
800 if v['.anonymous'] == true then
801 return false, ERR.SME_INVREF('', value)
803 val[k] = k -- XXX: title/description would be nice
804 elseif v[ref[3]] then
805 val[v[ref[3]]] = v[ref[3]] -- XXX: dito
810 return false, ERR.SME_BADREF('', value)
817 -- Resolve given path
818 function UVL._resolve_function( self, value )
819 local path = util.split(value, ".")
822 local stat, mod = pcall(
823 require, table.concat(path, ".", 1, i)
827 for j=i+1, #path-1 do
828 if not type(mod) == "table" then
836 mod = type(mod) == "table" and mod[path[#path]] or nil
837 if type(mod) == "function" then
845 --- Object representation of an uvl item - base class.
846 uvlitem = util.class()
848 function uvlitem.cid(self)
849 if #self.cref == 1 then
852 local r = { unpack(self.cref) }
854 if c and c[r[2]] and c[r[2]]['.anonymous'] and c[r[2]]['.index'] then
855 r[2] = '@' .. c[r[2]]['.type'] ..
856 '[' .. tostring(c[r[2]]['.index']) .. ']'
858 return table.concat( r, '.' )
862 function uvlitem.sid(self)
863 return table.concat( self.sref, '.' )
866 function uvlitem.scheme(self, opt)
867 local s = self.s and self.s.packages
868 s = s and s[self.sref[1]]
869 if #self.sref == 4 or #self.sref == 3 then
870 s = s and s.variables
871 s = s and s[self.sref[2]]
872 s = s and s[self.sref[3]]
873 elseif #self.sref == 2 then
875 s = s and s[self.sref[2]]
885 function uvlitem.config(self, opt)
888 if #self.cref >= 2 and #self.cref <= 4 then
889 c = c and self.c[self.cref[2]] or nil
890 if #self.cref >= 3 then
891 c = c and c[self.cref[3]] or nil
902 function uvlitem.title(self)
903 return self:scheme() and self:scheme('title') or
904 self.cref[3] or self.cref[2] or self.cref[1]
907 function uvlitem.type(self)
908 if self.t == TYPE_CONFIG then
910 elseif self.t == TYPE_SECTION then
912 elseif self.t == TYPE_OPTION then
914 elseif self.t == TYPE_ENUM then
919 function uvlitem.error(self, ...)
921 local errconst = { ERR.CONFIG, ERR.SECTION, ERR.OPTION, ERR.OPTION }
922 self.e = errconst[#self.cref]( self )
925 return self.e:child( ... )
928 function uvlitem.errors(self)
932 function uvlitem.ok(self)
933 return not self:errors()
936 function uvlitem.parent(self)
939 elseif #self.cref == 3 or #self.cref == 4 then
940 return section( self.s, self.c, self.cref[1], self.cref[2] )
941 elseif #self.cref == 2 then
942 return config( self.s, self.c, self.cref[1] )
948 function uvlitem._loadconf(self, co, c)
949 co = co or self._configcache
952 co, err = uci.cursor():get_all(c)
955 self:error(ERR.UCILOAD(self, err))
958 self._configcache = co
964 --- Object representation of a scheme.
967 -- @name luci.uvl.scheme
969 --- Scheme instance constructor.
972 -- @param scheme Scheme instance
973 -- @param co Configuration data
974 -- @param c Configuration name
975 -- @return Config instance
976 scheme = util.class(uvlitem)
978 function scheme.__init__(self, scheme, co, c)
985 self.c = self:_loadconf(co, c)
990 --- Add an error to scheme.
991 -- @return Scheme error context
992 function scheme.error(self, ...)
993 if not self.e then self.e = ERR.SCHEME( self ) end
994 return self.e:child( ... )
997 --- Get an associated config object.
998 -- @return Config instance
999 function scheme.config(self)
1000 local co = config( self.s, self.cref[1] )
1006 --- Get all section objects associated with this scheme.
1007 -- @return Table containing all associated luci.uvl.section instances
1008 function scheme.sections(self)
1010 if self.s.packages[self.sref[1]].sections then
1011 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1013 self.s, self.c, self.cref[1], self.cref[2], o
1020 --- Get an associated section object.
1021 -- @param s Section to select
1022 -- @return Section instance
1023 function scheme.section(self, s)
1024 local so = section( self.s, self.c, self.cref[1], s )
1031 --- Object representation of a config.
1034 -- @name luci.uvl.config
1036 --- Config instance constructor.
1039 -- @param scheme Scheme instance
1040 -- @param co Configuration data
1041 -- @param c Configuration name
1042 -- @return Config instance
1043 config = util.class(uvlitem)
1045 function config.__init__(self, scheme, co, c)
1052 self.c = self:_loadconf(co, c)
1054 self.t = TYPE_CONFIG
1057 --- Get all section objects associated with this config.
1058 -- @return Table containing all associated luci.uvl.section instances
1059 function config.sections(self)
1061 if self.s.packages[self.sref[1]].sections then
1062 for o, _ in pairs( self.s.packages[self.sref[1]].sections ) do
1064 self.s, self.c, self.cref[1], self.cref[2], o
1071 --- Get an associated section object.
1072 -- @param s Section to select
1073 -- @return Section instance
1074 function config.section(self, s)
1075 local so = section( self.s, self.c, self.cref[1], s )
1082 --- Object representation of a scheme/config section.
1085 -- @name luci.uvl.section
1087 --- Section instance constructor.
1090 -- @param scheme Scheme instance
1091 -- @param co Configuration data
1092 -- @param c Configuration name
1093 -- @param s Section name
1094 -- @return Section instance
1095 section = util.class(uvlitem)
1097 function section.__init__(self, scheme, co, c, s)
1098 self.cref = { c, s }
1099 self.sref = { c, co and co[s] and co[s]['.type'] or s }
1100 self.c = self:_loadconf(co, c)
1102 self.t = TYPE_SECTION
1105 --- Get all option objects associated with this section.
1106 -- @return Table containing all associated luci.uvl.option instances
1107 function section.variables(self)
1109 if self.s.packages[self.sref[1]].variables[self.sref[2]] then
1111 self.s.packages[self.sref[1]].variables[self.sref[2]]
1114 self.s, self.c, self.cref[1], self.cref[2], o
1121 --- Get an associated option object.
1122 -- @param o Option to select
1123 -- @return Option instance
1124 function section.option(self, o)
1125 local oo = option( self.s, self.c, self.cref[1], self.cref[2], o )
1132 --- Object representation of a scheme/config option.
1135 -- @name luci.uvl.option
1137 --- Section instance constructor.
1140 -- @param scheme Scheme instance
1141 -- @param co Configuration data
1142 -- @param c Configuration name
1143 -- @param s Section name
1144 -- @param o Option name
1145 -- @return Option instance
1146 option = util.class(uvlitem)
1148 function option.__init__(self, scheme, co, c, s, o)
1149 self.cref = { c, s, o }
1150 self.sref = { c, co and co[s] and co[s]['.type'] or s, o }
1151 self.c = self:_loadconf(co, c)
1153 self.t = TYPE_OPTION
1156 --- Get the value of this option.
1157 -- @return The associated configuration value
1158 function option.value(self)
1159 local v = self:config() or self:scheme('default')
1160 if v and self:scheme('multival') then
1161 v = util.split( v, "%s+", nil, true )
1166 --- Get the associated section information in scheme.
1167 -- @return Table containing the scheme properties
1168 function option.section(self)
1169 return self.s.packages[self.sref[1]].sections[self.sref[2]]
1172 --- Construct an enum object instance from given or default value.
1173 -- @param v Value to select
1174 -- @return Enum instance for selected value
1175 function option.enum(self, val)
1178 self.cref[1], self.cref[2], self.cref[3],
1184 --- Object representation of a enum value.
1187 -- @name luci.uvl.enum
1189 --- Section instance constructor.
1192 -- @param scheme Scheme instance
1193 -- @param co Configuration data
1194 -- @param c Configuration name
1195 -- @param s Section name
1196 -- @param o Enum name
1197 -- @param v Enum value
1198 -- @return Enum value instance
1199 enum = util.class(option)
1201 function enum.__init__(self, scheme, co, c, s, o, v)
1202 self.cref = { c, s, o, v }
1203 self.sref = { c, co and co[s] and co[s]['.type'] or s, o, v }
1204 self.c = self:_loadconf(co, c)