summaryrefslogtreecommitdiff
path: root/src/util.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.lua')
-rw-r--r--src/util.lua12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/util.lua b/src/util.lua
index 52597b2..f0192dc 100644
--- a/src/util.lua
+++ b/src/util.lua
@@ -90,4 +90,16 @@ function util.reverse(tbl, val)
return ret
end
+function util.typecheck(tbl, ...)
+ local args = {...}
+ assert(#args % 2 == 0,"Typecheck should have an odd number of arguments, found " .. tostring(#args + 1) .. ".")
+ for i = 1, #args, 2 do
+ assert(args[i] and type(args[i]) == "string", "Cannot check a field of type " .. type(args[i]) .. " at position " .. tostring(i + 1) .. ".")
+ assert(tbl[args[i]], "Failed to find a field: " .. args[i])
+ assert(args[i+1] and type(args[i + 1]) == "string", "Cannot check for a type " .. type(args[i + 1]) .. " at position " .. tostring(i + 2) .. ".")
+ assert(type(tbl[args[i]]) == args[i+1], "Expected a " .. args[i+1] .. " at position " .. tostring(i+2) .. " but found a " .. type(tbl[args[i]]))
+ end
+ return true
+end
+
return util