summaryrefslogtreecommitdiffhomepage
path: root/libs/web/luasrc/cbi
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2012-01-08 23:33:47 +0000
committerJo-Philipp Wich <jow@openwrt.org>2012-01-08 23:33:47 +0000
commit9fcdf0fe81f3c5142b8abd3f701dc3964549742a (patch)
tree2d189d22e4686a8aeb55c452221042582d68b95d /libs/web/luasrc/cbi
parent3812f2908731c6ff5e59aec4e0c899dbff35c469 (diff)
libs/web: introduce recursive expression support for datatypes, introduce "or" and "and" datatypes
The commit adds a recursive parser for datatype expressions which allows nesting of validators, this allows for complex expressions like "list(or(range(0,65535),'infinite'))" to allow a list of values which are either integers between 0 and 65535 or the literal string "inifinite". That change also deprecates combined datatypes like "ipaddr" ["or(ip4addr,ip6addr)"] or "host" ["or(hostname,ip4addr,ip6addr)"]
Diffstat (limited to 'libs/web/luasrc/cbi')
-rw-r--r--libs/web/luasrc/cbi/datatypes.lua76
1 files changed, 53 insertions, 23 deletions
diff --git a/libs/web/luasrc/cbi/datatypes.lua b/libs/web/luasrc/cbi/datatypes.lua
index 9f5a3eb1b..6d8715734 100644
--- a/libs/web/luasrc/cbi/datatypes.lua
+++ b/libs/web/luasrc/cbi/datatypes.lua
@@ -17,12 +17,64 @@ local fs = require "nixio.fs"
local ip = require "luci.ip"
local math = require "math"
local util = require "luci.util"
-local tonumber, type = tonumber, type
+local tonumber, type, unpack, select = tonumber, type, unpack, select
module "luci.cbi.datatypes"
+_M['or'] = function(v, ...)
+ local i
+ for i = 1, select('#', ...), 2 do
+ local f = select(i, ...)
+ local a = select(i+1, ...)
+ if type(f) ~= "function" then
+ print("COMP", f, v)
+ if f == v then
+ return true
+ end
+ i = i - 1
+ elseif f(v, unpack(a)) then
+ return true
+ end
+ end
+ return false
+end
+
+_M['and'] = function(v, ...)
+ local i
+ for i = 1, select('#', ...), 2 do
+ local f = select(i, ...)
+ local a = select(i+1, ...)
+ if type(f) ~= "function" then
+ if f ~= v then
+ return false
+ end
+ i = i - 1
+ elseif not f(v, unpack(a)) then
+ return false
+ end
+ end
+ return true
+end
+
+function neg(v, ...)
+ return _M['or'](v:gsub("^%s*!%s*", ""), ...)
+end
+
+function list(v, subvalidator, subargs)
+ if type(subvalidator) ~= "function" then
+ return false
+ end
+ local token
+ for token in v:gmatch("%S+") do
+ if not subvalidator(token, unpack(subargs)) then
+ return false
+ end
+ end
+ return true
+end
+
function bool(val)
if val == "1" or val == "yes" or val == "on" or val == "true" then
return true
@@ -254,25 +306,3 @@ function max(val, max)
return false
end
-
-function neg(val, what)
- if what and type(_M[what]) == "function" then
- return _M[what](val:gsub("^%s*!%s*", ""))
- end
-
- return false
-end
-
-function list(val, what, ...)
- if type(val) == "string" and what and type(_M[what]) == "function" then
- for val in val:gmatch("%S+") do
- if not _M[what](val, ...) then
- return false
- end
- end
-
- return true
- end
-
- return false
-end