feat: add defaults func for table merge

master
Riceball LEE 2021-06-18 22:21:02 +08:00
parent 1780e62c84
commit f6940834cd
No known key found for this signature in database
GPG Key ID: 10F15E84852CB868
1 changed files with 20 additions and 0 deletions

20
defaults_table.lua Normal file
View File

@ -0,0 +1,20 @@
-- merge defaults value to target table
local function defaults(target, default)
if not target then target = {} end
if not default then return target end
for k,v in pairs(default) do
local value = target[k]
if type(v) == "table" then
if type(value) ~= "table" then
value = {}
target[k] = value
end
defaults(value, v)
else
if (value == nil) then target[k] = v end
end
end
return target
end
return defaults