Added 'ignore' parameter to allow ignoring table values

This commit is contained in:
Paul Kulchenko 2012-06-16 19:43:15 -07:00
parent d9e1055bd0
commit ad84a79966
3 changed files with 14 additions and 7 deletions

View File

@ -59,13 +59,14 @@ internal function, but set different options by default:
* nocode (true/False) -- disable bytecode serialization for easy comparison
* nohuge (true/False) -- disable checking numbers against undefined and huge values
* maxlevel (number) -- specify max level up to which to expand nested tables
* ignore (table) -- allows to specify a list of values to ignore (as keys)
* custom (function) -- provide custom output for tables
These options can be provided as a second parameter to Serpent functions.
```lua
block(a, {fatal = true})
line(a, {nocode = true})
line(a, {nocode = true, ignore = {[arrayToIgnore] = true}})
function todiff(a) return dump(a, {nocode = true, indent = ' '}) end
```

View File

@ -60,14 +60,17 @@ local function s(t, opts)
if opts.sortkeys then alphanumsort(o, opts.sortkeys) end
for n, key in ipairs(o) do
local value, ktype, plainindex = t[key], type(key), n <= maxn and not sparse
if badtype[ktype] then plainindex, key = true, '['..globerr(key)..']' end
if sparse and value == nil then -- skipping nils; do nothing
if opts.ignore and opts.ignore[value] -- skip ignored values; do nothing
or sparse and value == nil then -- skipping nils; do nothing
elseif ktype == 'table' or ktype == 'function' then
if not seen[key] and not globals[key] then
table.insert(sref, 'local '..val2str(key,gensym(key),indent)) end
table.insert(sref, seen[t]..'['..(seen[key] or globals[key] or gensym(key))
..']'..space..'='..space..(seen[value] or val2str(value,nil,indent)))
else table.insert(out,val2str(value,key,indent,spath,plainindex,level+1)) end
else
if badtype[ktype] then plainindex, key = true, '['..globerr(key)..']' end
table.insert(out,val2str(value,key,indent,spath,plainindex,level+1))
end
end
local prefix = string.rep(indent or '', level)
local head = indent and '{\n'..prefix..indent or '{'

View File

@ -11,6 +11,7 @@ local serialize = require("lua-nucleo.tserialize").tserialize --]]
local b = {text="ha'ns", ['co\nl or']='bl"ue', str="\"\n'\\\000"}
local c = function() return 1 end
local d = {'sometable'}
local a = {
x=1, [true] = {b}, [not true]=2, -- boolean as key
['true'] = 'some value', -- keyword as a key
@ -23,13 +24,14 @@ local a = {
['label 2'] = b, -- shared reference
[b] = 0/0, -- table as key, undefined value as value
[math.huge] = -math.huge, -- huge as number value
ignore = d -- table to ignore
}
a.c = a -- self-reference
a[a] = a -- self-reference with table as key
print("pretty: " .. serpent.block(a) .. "\n")
print("line: " .. serpent.line(a) .. "\n")
local str = serpent.dump(a)
print("pretty: " .. serpent.block(a, {ignore = {[d] = true}}) .. "\n")
print("line: " .. serpent.line(a, {ignore = {[d] = true}}) .. "\n")
local str = serpent.dump(a, {ignore = {[d] = true}})
print("full: " .. str .. "\n")
local fun, err = assert(loadstring(str))
@ -52,6 +54,7 @@ assert(#(_a.list[7]) == 0, "empty table stays empty: failed")
assert(_a.list[-1] == -1, "negative index is in the right place: failed")
assert(_a.list['3'] == 33, "string that looks like number as index: failed")
assert(_a.list[4] == 'f', "specific table element preserves its value: failed")
assert(_a.ignore == nil, "ignored table not serialized: failed")
-- test without sparsness to check the number of elements in the list with nil
_a = loadstring(serpent.dump(a, {sparse = false, nocode = true}))()