Commit d89c0c1f authored by Isabella Almeida da Silva's avatar Isabella Almeida da Silva
Browse files

[SOMA-3827][SOMA-4177] Corrigir: Validação de campos não previstos na...

[SOMA-3827][SOMA-4177] Corrigir: Validação de campos não previstos na configuração não está sendo feita na configuração padrão
- Fazendo um iterador especial que também percorre as chaves da
metatable para poder validar corretamente a configuração
- Limpando método de debug que pode ser substituido pelo novo iterador
- Pequeno ajuste de chave duplicada no schema da configuração
parent edbe1805
......@@ -3,11 +3,14 @@ local configuration = {}
local safer = require("safer")
local schema = require("schema")
local util = require("sga.util")
local function get(config_data, filename, config_default)
local config = {}
config_default.sgas = nil -- avoids multiple inside multiple
config_default.__index = config_default -- sets default values for the configuration
config_default.__pairs = util.metatable_pairs -- forces the iterator to go through default values
setmetatable(config, config_default)
local chunk, err = load(config_data, filename, "t", config)
if not chunk then
......@@ -131,7 +134,6 @@ function configuration.check(config, just_warn)
runtime_data_dir = schema.String,
project_root_dir = schema.String,
algorithm_root_dir = schema.String,
runtime_data_dir = schema.String,
sandbox_root_dir = schema.String,
}
local err = schema.CheckSchema(config, config_schema)
......
......@@ -39,32 +39,39 @@ function util.read_file(name, offset)
return data
end
function util.debug_metatable(tbl, lvl)
lvl = lvl or 0
local indent = (" "):rep(lvl)
local out = {"{\n"}
for k,v in pairs(tbl) do
table.insert(out, indent.." ")
table.insert(out, tostring(k).." = ")
table.insert(out, (type(v) == "table" and util.debug_metatable(v, lvl + 1) or format_str(v)))
table.insert(out, ",\n")
end
local mt = getmetatable(tbl)
if mt and mt.__index and type(mt.__index) == "table" then
for k,v in pairs(mt) do
if k ~= "__index" then
v = tbl[k]
if v and not rawget(tbl, k) then
table.insert(out, indent.." ")
table.insert(out, tostring(k).." = ")
table.insert(out, (type(v) == "table" and util.debug_metatable(v, lvl + 1) or format_str(v)))
table.insert(out, ",\n")
function util.metatable_pairs(tbl)
-- Iterator function that goes through both the table and its metatable keys
local function stateless_iter(tbl, k)
local v
if k == nil or rawget(tbl,k) then
k, v = next(tbl, k)
end
if nil~=v then
return k,v
else
local mt = getmetatable(tbl)
if mt and mt.__index and type(mt.__index) == "table" then
k, v = next(mt, k)
-- Excludes meta-methods from iteration
if k == nil or k:sub(1, 2) == "__" then
k, v = next(mt, k)
end
if v ~= nil then
if rawget(tbl, k) then
k, v = next(mt, k)
end
if v ~= nil then
v = tbl[k]
return k, v
end
end
end
end
end
table.insert(out, indent.."}")
return table.concat(out)
end
end
-- Return an iterator function, the table, starting point
return stateless_iter, tbl, nil
end
function util.debug_table(tbl, lvl)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment