Browse Source

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.
pull/226/head
Matthias Schiffer 2 years ago
parent
commit
cf0619a0f9
No known key found for this signature in database GPG Key ID: 16EF3F64CB201D9C
  1. 37
      libs/lua-simple-uci/src/simple-uci.lua

37
libs/lua-simple-uci/src/simple-uci.lua

@ -5,23 +5,33 @@
-- This is basically everything useful from luci.model.uci
-- without any luci dependency.
local uci = require "uci"
local table = require "table"
local getmetatable = getmetatable
local next, pairs, ipairs = next, pairs, ipairs
local type, tonumber = type, tonumber
local uci = require 'uci'
module "simple-uci"
cursor = uci.cursor
local M = {}
APIVERSION = uci.APIVERSION
M.APIVERSION = uci.APIVERSION
local Cursor = getmetatable(cursor())
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
local uciset = Cursor.set
function Cursor:set(config, section, option, value)
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'
end
return uciset(self, config, section, option, value)
return self.cursor:set(config, section, option, value)
else
self:delete(config, section, option)
return true
@ -69,7 +79,7 @@ end
function Cursor:section(config, type, name, values)
local stat = true
if name then
stat = uciset(self, config, name, type)
stat = self.cursor:set(config, name, type)
else
name = self:add(config, type)
stat = name and true
@ -136,3 +146,6 @@ function Cursor:set_list(config, section, option, value)
end
return false
end
return M
Loading…
Cancel
Save