lua-simple-uci: do not use module(), do not rely on UCI internals

simple-uci was modifying the metatable of UCI cursor objects, thus changing
the behaviour of *all* UCI cursors, and not only UCI cursors that are
created through simple-uci.
This commit is contained in:
Matthias Schiffer 2019-06-16 14:11:33 +02:00
parent 9d26ec1cf9
commit cf0619a0f9
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
1 changed files with 29 additions and 16 deletions

View File

@ -5,23 +5,33 @@
-- This is basically everything useful from luci.model.uci -- This is basically everything useful from luci.model.uci
-- without any luci dependency. -- without any luci dependency.
local uci = require "uci"
local table = require "table"
local getmetatable = getmetatable local uci = require 'uci'
local next, pairs, ipairs = next, pairs, ipairs
local type, tonumber = type, tonumber
module "simple-uci"
cursor = uci.cursor
APIVERSION = uci.APIVERSION
local Cursor = getmetatable(cursor())
local uciset = Cursor.set local M = {}
M.APIVERSION = uci.APIVERSION
local Cursor = setmetatable({}, {
-- Forward calls to the actual UCI cursor
__index = function(t, f)
return function(self, ...)
local c = self.cursor
return c[f](c, ...)
end
end,
})
function M.cursor()
return setmetatable({
cursor = uci.cursor(),
}, {
__index = Cursor,
})
end
function Cursor:set(config, section, option, value) function Cursor:set(config, section, option, value)
if value ~= nil and not (type(value) == 'table' and #value == 0) then if value ~= nil and not (type(value) == 'table' and #value == 0) then
@ -29,7 +39,7 @@ function Cursor:set(config, section, option, value)
value = value and '1' or '0' value = value and '1' or '0'
end end
return uciset(self, config, section, option, value) return self.cursor:set(config, section, option, value)
else else
self:delete(config, section, option) self:delete(config, section, option)
return true return true
@ -69,7 +79,7 @@ end
function Cursor:section(config, type, name, values) function Cursor:section(config, type, name, values)
local stat = true local stat = true
if name then if name then
stat = uciset(self, config, name, type) stat = self.cursor:set(config, name, type)
else else
name = self:add(config, type) name = self:add(config, type)
stat = name and true stat = name and true
@ -136,3 +146,6 @@ function Cursor:set_list(config, section, option, value)
end end
return false return false
end end
return M