X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi%2Fdatatypes.lua;h=6d871573413a0cf698b9fdad7830c89a98fcb3ef;hp=9f5a3eb1bfde8971185ad516f23942586cb97db6;hb=9fcdf0fe81f3c5142b8abd3f701dc3964549742a;hpb=3812f2908731c6ff5e59aec4e0c899dbff35c469 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