summaryrefslogtreecommitdiffhomepage
path: root/libs/web/luasrc
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
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')
-rw-r--r--libs/web/luasrc/cbi.lua104
-rw-r--r--libs/web/luasrc/cbi/datatypes.lua76
2 files changed, 135 insertions, 45 deletions
diff --git a/libs/web/luasrc/cbi.lua b/libs/web/luasrc/cbi.lua
index ff030a268..88fb8a9b1 100644
--- a/libs/web/luasrc/cbi.lua
+++ b/libs/web/luasrc/cbi.lua
@@ -152,6 +152,78 @@ function load(cbimap, ...)
return maps
end
+--
+-- Compile a datatype specification into a parse tree for evaluation later on
+--
+local cdt_cache = { }
+
+function compile_datatype(code)
+ local i
+ local pos = 0
+ local esc = false
+ local depth = 0
+ local stack = { }
+
+ for i = 1, #code+1 do
+ local byte = code:byte(i) or 44
+ if esc then
+ esc = false
+ elseif byte == 92 then
+ esc = true
+ elseif byte == 40 or byte == 44 then
+ if depth <= 0 then
+ if pos < i then
+ local label = code:sub(pos, i-1)
+ :gsub("\\(.)", "%1")
+ :gsub("^%s+", "")
+ :gsub("%s+$", "")
+
+ if #label > 0 and tonumber(label) then
+ stack[#stack+1] = tonumber(label)
+ elseif label:match("^'.+'$") or label:match('^".+"$') then
+ stack[#stack+1] = label:gsub("[\"'](.+)[\"']", "%1")
+ elseif type(datatypes[label]) == "function" then
+ stack[#stack+1] = datatypes[label]
+ stack[#stack+1] = { }
+ else
+ error("Datatype error, bad token %q" % label)
+ end
+ end
+ pos = i + 1
+ end
+ depth = depth + (byte == 40 and 1 or 0)
+ elseif byte == 41 then
+ depth = depth - 1
+ if depth <= 0 then
+ if type(stack[#stack-1]) ~= "function" then
+ error("Datatype error, argument list follows non-function")
+ end
+ stack[#stack] = compile_datatype(code:sub(pos, i-1))
+ pos = i + 1
+ end
+ end
+ end
+
+ return stack
+end
+
+function verify_datatype(dt, value)
+ if dt and #dt > 0 then
+ if not cdt_cache[dt] then
+ local c = compile_datatype(dt)
+ if c and type(c[1]) == "function" then
+ cdt_cache[dt] = c
+ else
+ error("Datatype error, not a function expression")
+ end
+ end
+ if cdt_cache[dt] then
+ return cdt_cache[dt][1](value, unpack(cdt_cache[dt][2]))
+ end
+ end
+ return true
+end
+
-- Node pseudo abstract class
Node = class()
@@ -1356,31 +1428,19 @@ end
-- Validate the form value
function AbstractValue.validate(self, value)
if self.datatype and value then
- local args = { }
- local dt, ar = self.datatype:match("^(%w+)%(([^%(%)]+)%)")
-
- if dt and ar then
- local a
- for a in ar:gmatch("[^%s,]+") do
- args[#args+1] = a
- end
- else
- dt = self.datatype
- end
-
- if dt and datatypes[dt] then
- if type(value) == "table" then
- local v
- for _, v in ipairs(value) do
- if v and #v > 0 and not datatypes[dt](v, unpack(args)) then
- return nil
- end
- end
- else
- if not datatypes[dt](value, unpack(args)) then
+ if type(value) == "table" then
+ local v
+ for _, v in ipairs(value) do
+ if v and #v > 0 and not verify_datatype(self.datatype, v) then
+ error('F')
return nil
end
end
+ else
+ if not verify_datatype(self.datatype, value) then
+ error('F')
+ return nil
+ end
end
end
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