Add mergetable replacement and deprecate the former

This commit is contained in:
numzero 2020-09-11 01:39:22 +03:00
parent 9fda51b650
commit b19fbc321e

View File

@ -204,7 +204,7 @@ function mesecon.cmpAny(t1, t2)
return true
end
-- does not overwrite values; number keys (ipairs) are appended, not overwritten
-- Deprecated. Use `merge_replace` or `merge_rule_sets` as appropriate.
function mesecon.mergetable(source, dest)
local rval = mesecon.tablecopy(dest)
@ -218,6 +218,32 @@ function mesecon.mergetable(source, dest)
return rval
end
-- Merges several rule sets in one. Order may not be preserved. Nil arguments
-- are ignored.
-- The rule sets must be of the same kind (either all single-level or all two-level).
-- The function may be changed to normalize the resulting set in some way.
function mesecon.merge_rule_sets(...)
local rval = {}
for _, t in pairs({...}) do -- ignores nils automatically
table.insert_all(rval, mesecon.tablecopy(t))
end
return rval
end
-- Merges two tables, with entries from `replacements` taking precedence over
-- those from `base`. Returns the new table.
-- Numerical indices arent handled specially.
-- Values are copied, keys are referenced.
function mesecon.merge_replace(base, replacements)
local ret = mesecon.tablecopy(replacements) -- these are never overriden so have to be copied in any case
for k, v in pairs(base) do
if ret[k] == nil then -- it could be `false`
ret[k] = mesecon.tablecopy(v)
end
end
return ret
end
function mesecon.register_node(name, spec_common, spec_off, spec_on)
spec_common.drop = spec_common.drop or name .. "_off"
spec_common.on_blast = spec_common.on_blast or mesecon.on_blastnode