Commit 8c574553 authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

show original variable definitions in dump output

PolicyConfig.variables removed
PolicyConfig.eval merged to PolicyConfig.expand
parent 8e289c58
......@@ -121,22 +121,24 @@ config = policyset:load()
if mode == 'dump' then
require 'json'
function dump(title, data)
print(title..':')
local lines = {}
for k, v in pairs(data) do
table.insert(lines, k..' = '..json.encode(v))
expconfig = config:expand()
for i, section in ipairs({'variable', 'zone'}) do
if config.data[section] then
print(string.upper(string.sub(section, 1, 1))..string.sub(section, 2, -1)..'s:')
lines = {}
for k, v in pairs(config.data[section]) do
def = json.encode(v)
exp = json.encode(expconfig[section][k])
if exp ~= def then def = def..' = '..exp end
table.insert(lines, k..' = '..def)
end
table.sort(lines)
for i, line in ipairs(lines) do print(line) end
print()
end
table.sort(lines)
for i, line in ipairs(lines) do print(line) end
print()
end
dump('Variables', config:variables())
config = config:expand()
dump('Zones', config.zone)
os.exit()
end
......
......@@ -23,44 +23,34 @@ function PolicyConfig:init(data)
self.data = data
end
function PolicyConfig:eval(value)
local visited = {}
local pattern = '%$(%a[%w_]*)'
while type(value) == 'string' and string.find(value, pattern) do
local si, ei, name = string.find(value, pattern)
if util.contains(visited, name) then
error('Circular variable definition: '..name)
end
table.insert(visited, name)
local var = self.data.variable[name]
if not var then error('Invalid variable reference: '..name) end
if si == 1 and ei == string.len(value) then value = var
elseif util.contains({'number', 'string'}, type(var)) then
value = string.sub(value, 1, si - 1)..var..string.sub(value, ei + 1, -1)
else
error('Attempted to concatenate complex variable: '..name)
end
end
return value ~= '' and value or nil
end
function PolicyConfig:expand()
function PolicyConfig:variables()
local res = {}
for name, value in pairs(self.data.variable or {}) do
res[name] = self:eval('$'..name)
end
return res
end
local function expand(value)
if type(value) == 'table' then return util.map(value, expand) end
function PolicyConfig:expand()
local visited = {}
local pattern = '%$(%a[%w_]*)'
while type(value) == 'string' and string.find(value, pattern) do
local si, ei, name = string.find(value, pattern)
if util.contains(visited, name) then
error('Circular variable definition: '..name)
end
table.insert(visited, name)
local var = self.data.variable[name]
if not var then error('Invalid variable reference: '..name) end
if si == 1 and ei == string.len(value) then value = var
elseif util.contains({'number', 'string'}, type(var)) then
value = string.sub(value, 1, si - 1)..var..string.sub(value, ei + 1, -1)
else
error('Attempted to concatenate complex variable: '..name)
end
end
local function expand(obj)
return type(obj) == 'table' and util.map(obj, expand) or self:eval(obj)
return value ~= '' and value or nil
end
return expand(self.data)
......
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