diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2014-09-18 13:55:45 +0300 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2014-09-18 13:55:45 +0300 |
commit | 0d6d0b76af382c00ada3add3e722482723158423 (patch) | |
tree | 129223d520872b238de07c2ca369fded638e01b1 | |
parent | f48f9461c9fd1b4c4643fc1d41038dba94983220 (diff) | |
download | awall-0d6d0b76af382c00ada3add3e722482723158423.tar.bz2 awall-0d6d0b76af382c00ada3add3e722482723158423.tar.xz |
utility function for setting default values
-rwxr-xr-x | awall-cli | 4 | ||||
-rw-r--r-- | awall/model.lua | 10 | ||||
-rw-r--r-- | awall/modules/filter.lua | 2 | ||||
-rw-r--r-- | awall/policy.lua | 2 | ||||
-rw-r--r-- | awall/util.lua | 5 |
5 files changed, 12 insertions, 11 deletions
@@ -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 diff --git a/awall/model.lua b/awall/model.lua index 6e29032..c261c51 100644 --- a/awall/model.lua +++ b/awall/model.lua @@ -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 diff --git a/awall/modules/filter.lua b/awall/modules/filter.lua index 5a9c8c1..31ae700 100644 --- a/awall/modules/filter.lua +++ b/awall/modules/filter.lua @@ -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 diff --git a/awall/policy.lua b/awall/policy.lua index 093390d..0160a6f 100644 --- a/awall/policy.lua +++ b/awall/policy.lua @@ -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 diff --git a/awall/util.lua b/awall/util.lua index 019a078..a34165c 100644 --- a/awall/util.lua +++ b/awall/util.lua @@ -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) |