feat: add table parsing

This commit is contained in:
Kazhnuz 2021-05-15 15:50:15 +02:00
parent 33d854ffd4
commit 792738c5cc

View file

@ -77,4 +77,27 @@ function Table.average(table)
return Table.sum(table) / #table
end
--Parse a basic list into a structured table. Return an error if the number of arguments is not the same
---@param table table the list to parse into a table
---@param structure table the structure to create the table : each name correspond to an attribute of the parsed table
---@param nullableNbr integer the number of nullable argument (at the end of the functions) (can be null)
---@return table parsedTable the parsed table
function Table.parse(table, structure, nullableNbr)
local parsedTable = {}
assert(table ~= nil, "The table to parse can't be null")
assert(structure ~= nil, "The table structure can't be null")
nullableNbr = nullableNbr or 0
if ((#table) > (#structure)) or ((#table) < (#structure - nullableNbr)) then
error("The table to parse doesn't have the right number of arguments: " .. #table .. " instead of " .. #structure .. " and " .. nullableNbr .. " nullables")
else
for i, key in ipairs(structure) do
--print(i, key, table[i])
parsedTable[key] = table[i]
end
end
return parsedTable
end
return Table