rolling-13

This commit is contained in:
Lars Mueller 2020-04-05 10:42:44 +02:00
parent 2e0eb885c4
commit 4ee679f187
2 changed files with 22 additions and 7 deletions

@ -7,7 +7,7 @@ function load (filename, constraints)
if constraints then if constraints then
local error_message = check_constraints(config, constraints) local error_message = check_constraints(config, constraints)
if error_message then if error_message then
error("Configuration - "..filename.." doesn't satisfy constraints : "..error_message) error("Configuration - "..filename.." doesn't satisfy constraints: "..error_message)
end end
end end
return config return config
@ -28,22 +28,22 @@ function check_constraints(value, constraints)
end end
end end
if constraints.type and constraints.type~=t then if constraints.type and constraints.type~=t then
return "Wrong type : Expected "..constraints.type..", found "..t return "Wrong type: Expected "..constraints.type..", found "..t
end end
if (t == "number" or t == "string") and constraints.range then if (t == "number" or t == "string") and constraints.range then
if value < constraints.range[1] or (constraints.range[2] and value > constraints.range[2]) then if value < constraints.range[1] or (constraints.range[2] and value > constraints.range[2]) then
return "Not inside range : Expected value >= "..constraints.range[1].." and <= "..constraints.range[1]..", found "..minetest.write_json(value) return "Not inside range: Expected value >= "..constraints.range[1].." and <= "..constraints.range[1]..", found "..minetest.write_json(value)
end end
end end
if constraints.possible_values and not constraints.possible_values[value] then if constraints.possible_values and not constraints.possible_values[value] then
return "None of the possible values : Expected one of "..minetest.write_json(modlib.table.keys(constraints.possible_values))..", found "..minetest.write_json(value) return "None of the possible values: Expected one of "..minetest.write_json(modlib.table.keys(constraints.possible_values))..", found "..minetest.write_json(value)
end end
if t == "table" then if t == "table" then
if constraints.children then if constraints.children then
for k, v in pairs(value) do for k, v in pairs(value) do
local child=constraints.children[k] local child=constraints.children[k]
if not child then if not child then
return "Unexpected table entry : Expected one of "..minetest.write_json(modlib.table.keys(constraints.children))..", found "..minetest.write_json(k) return "Unexpected table entry: Expected one of "..minetest.write_json(modlib.table.keys(constraints.children))..", found "..minetest.write_json(k)
else else
local possible_errors=check_constraints(v, child) local possible_errors=check_constraints(v, child)
if possible_errors then if possible_errors then
@ -53,7 +53,7 @@ function check_constraints(value, constraints)
end end
for k, _ in pairs(constraints.children) do for k, _ in pairs(constraints.children) do
if value[k] == nil then if value[k] == nil then
return "Table entry missing : Expected key "..minetest.write_json(k).." to be present in table "..minetest.write_json(value) return "Table entry missing: Expected key "..minetest.write_json(k).." to be present in table "..minetest.write_json(value)
end end
end end
end end
@ -66,7 +66,7 @@ function check_constraints(value, constraints)
return possible_errors return possible_errors
end end
else else
return "Table entry missing : Expected key "..minetest.write_json(k).." to be present in table "..minetest.write_json(value) return "Table entry missing: Expected key "..minetest.write_json(k).." to be present in table "..minetest.write_json(value)
end end
end end
end end

@ -1,5 +1,20 @@
-- Table helpers -- Table helpers
function map_index(table, func)
return setmetatable(roles, {
__index = function(table, key)
return rawget(table, func(key))
end,
__newindex = function(table, key, value)
rawset(table, func(key), value)
end
})
end
function set_case_insensitive_index(table)
return map_index(table, string.lower)
end
-- Fisher-Yates -- Fisher-Yates
function shuffle(t) function shuffle(t)
for i = 1, #t-1 do for i = 1, #t-1 do