Skip to content

Instantly share code, notes, and snippets.

@dmitrii-eremin
Forked from qizhihere/merge-table.lua
Created July 23, 2020 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitrii-eremin/949999ca85985065e8116750c876493b to your computer and use it in GitHub Desktop.
Save dmitrii-eremin/949999ca85985065e8116750c876493b to your computer and use it in GitHub Desktop.
merge two tables in lua
local function table_clone_internal(t, copies)
if type(t) ~= "table" then return t end
copies = copies or {}
if copies[t] then return copies[t] end
local copy = {}
copies[t] = copy
for k, v in pairs(t) do
copy[table_clone_internal(k, copies)] = table_clone_internal(v, copies)
end
setmetatable(copy, table_clone_internal(getmetatable(t), copies))
return copy
end
local function table_clone(t)
-- We need to implement this with a helper function to make sure that
-- user won't call this function with a second parameter as it can cause
-- unexpected troubles
return table_clone_internal(t)
end
local function table_merge(...)
local tables_to_merge = {...}
assert(#tables_to_merge > 1, "There should be at least two tables to merge them")
for k, t in ipairs(tables_to_merge) do
assert(type(t) == "table", string.format("Expected a table as function parameter %d", k))
end
local result = table_clone(tables_to_merge[1])
for i = 2, #tables_to_merge do
local from = tables_to_merge[i]
for k, v in pairs(from) do
if type(v) == "table" then
result[k] = result[k] or {}
assert(type(result[k]) == "table", string.format("Expected a table: '%s'", k))
result[k] = table_merge(result[k], v)
else
result[k] = v
end
end
end
return result
end
return {
table_clone = table_clone,
table_merge = table_merge,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment