Commit 0d6d0b76 authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

utility function for setting default values

parent f48f9461
......@@ -134,9 +134,7 @@ end
if stringy.endswith(arg[0], '/awall-cli') then
basedir = arg[0]:sub(1, -11)
if not pol_paths.mandatory then
pol_paths.mandatory = {'/etc/awall'}
end
util.setdefault(pol_paths, 'mandatory', {'/etc/awall'})
table.insert(pol_paths.mandatory, basedir..'/json')
end
......
......@@ -24,6 +24,7 @@ local extend = util.extend
local filter = util.filter
local listpairs = util.listpairs
local maplist = util.maplist
local setdefault = util.setdefault
local startswith = require('stringy').startswith
......@@ -82,12 +83,9 @@ function M.ConfigObject:uniqueid(key)
if not key then key = '' end
if self.uniqueids[key] then return self.uniqueids[key] end
if not self.context.lastid then self.context.lastid = {} end
local lastid = self.context.lastid
local lastid = setdefault(self.context, 'lastid', {})
local res = join(key, self.label)
if not lastid[res] then lastid[res] = -1 end
lastid[res] = lastid[res] + 1
lastid[res] = setdefault(lastid, res, -1) + 1
res = res..'-'..lastid[res]
self.uniqueids[key] = res
......@@ -588,7 +586,7 @@ function M.Limit:init(...)
self.count = self[1]
end
if not self.interval then self.interval = 1 end
setdefault(self, 'interval', 1)
end
function M.Limit:rate() return math.ceil(self.count / self.interval) end
......
......@@ -40,7 +40,7 @@ local LoggingRule = class(TranslatingRule)
function LoggingRule:init(...)
LoggingRule.super(self):init(...)
if not self.action then self.action = 'accept' end
util.setdefault(self, 'action', 'accept')
if type(self.log) ~= 'table' then
self.log = loadclass('log').get(self, self.log, self.action ~= 'accept')
end
......
......@@ -208,7 +208,7 @@ function PolicySet:load()
raise('Invalid top-level attribute: '..cls..' ('..name..')')
end
if not source[cls] then source[cls] = {} end
util.setdefault(source, cls, {})
if not input[cls] then
input[cls] = objs
......
......@@ -79,6 +79,11 @@ function M.update(tbl1, tbl2)
return tbl1
end
function M.setdefault(t, k, v)
if t[k] == nil then t[k] = v end
return t[k]
end
function M.copy(tbl) return M.update({}, tbl) end
function M.compare(a, b)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment