Skip to content

Commit 976dade

Browse files
committed
♻️ tbl.extend use __index and __newindex instead
1 parent 50c3a54 commit 976dade

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

lua/sql/table.lua

+17-19
Original file line numberDiff line numberDiff line change
@@ -319,26 +319,24 @@ function tbl:extend(db, name, schema)
319319
end
320320

321321
local t = self:new(db, name, { schema = schema })
322-
local o = {}
323-
324-
for key, value in pairs(t) do
325-
o[key] = value
326-
end
327-
328-
for key, value in pairs(getmetatable(t)) do
329-
if type(value) == "function" then
330-
o[key] = function(...)
322+
local o = {
323+
set_db = function(db)
324+
t.db = db
325+
end,
326+
}
327+
328+
return setmetatable(o, {
329+
__index = function(self, key, ...)
330+
return type(t[key]) == "function" and function(...)
331331
return t[key](t, ...)
332-
end
333-
o["_" .. key] = o[key]
334-
end
335-
end
336-
337-
o.set_db = function(db)
338-
t.db = db
339-
end
340-
341-
return setmetatable(o, { __index = o.tbl })
332+
end or t[key]
333+
end,
334+
__newindex = function(_, key, val)
335+
local old = t[key]
336+
t["_" .. key] = t[key]
337+
t[key] = val
338+
end,
339+
})
342340
end
343341

344342
tbl = setmetatable(tbl, { __call = tbl.extend })

lua/sql/utils.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ M.is_userdata = function(t)
2525
end
2626

2727
M.is_nested = function(t)
28-
return type(t[1]) == "table"
28+
return t and type(t[1]) == "table" or false
2929
end
3030

3131
-- taken from: https://github.com/neovim/neovim/blob/master/runtime/lua/vim/shared.lua

0 commit comments

Comments
 (0)