Skip to content

Commit

Permalink
lua-simple-uci: do not use module(), do not rely on UCI internals
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
neocturne committed Jun 16, 2019
1 parent 9d26ec1 commit cf0619a
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions libs/lua-simple-uci/src/simple-uci.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,41 @@
-- 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
if type(value) == 'boolean' then
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -136,3 +146,6 @@ function Cursor:set_list(config, section, option, value)
end
return false
end


return M

0 comments on commit cf0619a

Please sign in to comment.