mirror of
https://github.com/pkulchenko/serpent.git
synced 2024-11-21 23:24:24 +00:00
Added 'ignore' parameter to allow ignoring table values
This commit is contained in:
parent
d9e1055bd0
commit
ad84a79966
@ -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
|
||||
```
|
||||
|
||||
|
@ -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 '{'
|
||||
|
@ -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}))()
|
||||
|
Loading…
Reference in New Issue
Block a user