From 48522b8dcf75dd5a869466b47ffb5be8d700c021 Mon Sep 17 00:00:00 2001 From: Timo Smit Date: Tue, 13 Feb 2018 11:45:11 +0100 Subject: [PATCH] Added util.typecheck function to ensure types in certain utility functions (fixes #78) --- luamods/wolfadmin/util/bits.lua | 6 +++++- luamods/wolfadmin/util/pagination.lua | 4 ++++ luamods/wolfadmin/util/tables.lua | 10 ++++++++++ luamods/wolfadmin/util/util.lua | 12 ++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/luamods/wolfadmin/util/bits.lua b/luamods/wolfadmin/util/bits.lua index 3735faf..6371169 100644 --- a/luamods/wolfadmin/util/bits.lua +++ b/luamods/wolfadmin/util/bits.lua @@ -15,9 +15,13 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +local util = require (wolfa_getLuaPath()..".util.util") + local bits = {} function bits.hasbit(x, b) + util.typecheck("bits.hasbit", {x, b}, {"number", "number"}) + if b == 0 then return x == b end @@ -25,4 +29,4 @@ function bits.hasbit(x, b) return x % (b + b) >= b end -return bits \ No newline at end of file +return bits diff --git a/luamods/wolfadmin/util/pagination.lua b/luamods/wolfadmin/util/pagination.lua index 432c7b0..5c04621 100644 --- a/luamods/wolfadmin/util/pagination.lua +++ b/luamods/wolfadmin/util/pagination.lua @@ -15,9 +15,13 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +local util = require (wolfa_getLuaPath()..".util.util") + local pagination = {} function pagination.calculate(count, limit, offset) + util.typecheck("pagination.calculate", {count, limit, offset}, {"number", "number", "number"}) + limit = limit or 30 offset = offset or 0 diff --git a/luamods/wolfadmin/util/tables.lua b/luamods/wolfadmin/util/tables.lua index 445e12f..e4fd915 100644 --- a/luamods/wolfadmin/util/tables.lua +++ b/luamods/wolfadmin/util/tables.lua @@ -15,9 +15,13 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +local util = require (wolfa_getLuaPath()..".util.util") + local tables = {} function tables.copy(tbl) + util.typecheck("tables.contains", {tbl}, {"table"}) + local copy = {} for key, value in pairs(tbl) do @@ -28,6 +32,8 @@ function tables.copy(tbl) end function tables.unpack(tbl) + util.typecheck("tables.contains", {tbl}, {"table"}) + if table.unpack ~= nil then return table.unpack(tbl) elseif unpack ~= nil then @@ -36,6 +42,8 @@ function tables.unpack(tbl) end function tables.contains(tbl, needle) + util.typecheck("tables.contains", {tbl}, {"table"}) + for _, value in pairs(tbl) do if value == needle then return true @@ -46,6 +54,8 @@ function tables.contains(tbl, needle) end function tables.find(tbl, needle) + util.typecheck("tables.contains", {tbl}, {"table"}) + for key, value in pairs(tbl) do if value == needle then return key diff --git a/luamods/wolfadmin/util/util.lua b/luamods/wolfadmin/util/util.lua index 6f6b0ea..a109c2b 100644 --- a/luamods/wolfadmin/util/util.lua +++ b/luamods/wolfadmin/util/util.lua @@ -19,6 +19,14 @@ local constants = require (wolfa_getLuaPath()..".util.constants") local util = {} +function util.typecheck(func, args, types) + for idx, arg in ipairs(args) do + if type(arg) ~= types[idx] then + error("bad argument #"..idx.." to '"..func.."' ("..types[idx].." expected, got "..type(arg)..")", 3) + end + end +end + function util.split(str, pat) local t = {} -- NOTE: use {n = 0} in Lua-5.0 local fpat = "(.-)" .. pat @@ -41,10 +49,14 @@ function util.split(str, pat) end function util.escape(str) + util.typecheck("util.escape", {str}, {"string"}) + return string.gsub(str, "([\"'])", "\\%1") end function util.removeColors(str) + util.typecheck("util.removeColors", {str}, {"string"}) + return string.gsub(str, "(^[%a%d%p])", "") end