mirror of
https://github.com/pkulchenko/serpent.git
synced 2024-11-21 23:24:24 +00:00
Added 'comment=num' option to limit comments up to particular depth level
This commit is contained in:
parent
ad84a79966
commit
3471396adb
13
README.md
13
README.md
@ -51,7 +51,7 @@ internal function, but set different options by default:
|
||||
|
||||
* name (string) -- name; triggers full serialization with self-ref section
|
||||
* indent (string) -- indentation; triggers long multi-line output
|
||||
* comment (true/False) -- provide stringified value in a comment
|
||||
* comment (true/False/maxlevel) -- provide stringified value in a comment (up to maxlevel of depth)
|
||||
* sortkeys (true/False) -- sort keys
|
||||
* sparse (true/False) -- force sparse encoding (no nil filling based on #t)
|
||||
* compact (true/False) -- remove spaces
|
||||
@ -128,22 +128,27 @@ See LICENSE file.
|
||||
|
||||
## History
|
||||
|
||||
Jun 17 2012 v0.15
|
||||
- Added `ignore` option to allow ignoring table values.
|
||||
- Added `comment=num` option to set the max level up to which add comments.
|
||||
- Changed all comments (except math.huge) to be controlled by `comment` option.
|
||||
|
||||
Jun 13 2012 v0.14
|
||||
- Fixed an issue with string keys with numeric values `['3']` getting mixed
|
||||
with real numeric keys (only with `sortkeys` option set to `true`).
|
||||
- Fixed an issue with negative and real value numeric keys being misplaced.
|
||||
|
||||
Jun 13 2012 v0.13
|
||||
- Added maxlevel option.
|
||||
- Added `maxlevel` option.
|
||||
- Fixed key sorting such that `true` and `'true'` are always sorted in
|
||||
the same order (for a more stable output).
|
||||
- Removed addresses from names of temporary variables (for stable output).
|
||||
|
||||
Jun 12 2012 v0.12
|
||||
- Added options to configure serialization process.
|
||||
- Added 'goto' to the list of keywords for Lua 5.2.
|
||||
- Added `goto` to the list of keywords for Lua 5.2.
|
||||
- Changed interface to dump/line/block methods.
|
||||
- Changed 'math.huge' to 1/0 for better portability.
|
||||
- Changed `math.huge` to 1/0 for better portability.
|
||||
- Replaced \010 with \n for better readability.
|
||||
|
||||
Jun 03 2012 v0.10
|
||||
|
@ -1,4 +1,4 @@
|
||||
local n, v = "serpent", 0.14 -- (C) 2012 Paul Kulchenko; MIT License
|
||||
local n, v = "serpent", 0.15 -- (C) 2012 Paul Kulchenko; MIT License
|
||||
local c, d = "Paul Kulchenko", "Serializer and pretty printer of Lua data types"
|
||||
local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'}
|
||||
local badtype = {thread = true, userdata = true}
|
||||
@ -14,15 +14,16 @@ local function s(t, opts)
|
||||
local name, indent, fatal = opts.name, opts.indent, opts.fatal
|
||||
local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge
|
||||
local space, maxl = (opts.compact and '' or ' '), (opts.maxlevel or math.huge)
|
||||
local comm = opts.comment and (tonumber(opts.comment) or math.huge)
|
||||
local seen, sref, syms, symn = {}, {}, {}, 0
|
||||
local function gensym(val) return tostring(val):gsub("[^%w]",""):gsub("(%d%w+)",
|
||||
function(s) if not syms[s] then symn = symn+1; syms[s] = symn end return syms[s] end) end
|
||||
local function safestr(s) return type(s) == "number" and (huge and snum[tostring(s)] or s)
|
||||
or type(s) ~= "string" and tostring(s) -- escape NEWLINE/010 and EOF/026
|
||||
or ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026") end
|
||||
local function comment(s) return opts.comment and ' --[['..tostring(s)..']]' or '' end
|
||||
local function globerr(s) return globals[s] and globals[s]..comment(s) or not fatal
|
||||
and safestr(tostring(s))..' --[[err]]' or error("Can't serialize "..tostring(s)) end
|
||||
local function comment(s,l) return comm and (l or 0) < comm and ' --[['..tostring(s)..']]' or '' end
|
||||
local function globerr(s,l) return globals[s] and globals[s]..comment(s,l) or not fatal
|
||||
and safestr(tostring(s))..comment('err',l) or error("Can't serialize "..tostring(s)) end
|
||||
local function safename(path, name) -- generates foo.bar, foo[3], or foo['b a r']
|
||||
local n = name == nil and '' or name
|
||||
local plain = type(n) == "string" and n:match("^[%l%u_][%w_]*$") and not keyword[n]
|
||||
@ -42,18 +43,18 @@ local function s(t, opts)
|
||||
(name ~= nil and sname..space..'='..space or '')
|
||||
if seen[t] then
|
||||
table.insert(sref, spath..space..'='..space..seen[t])
|
||||
return tag..'nil --[[ref]]'
|
||||
elseif badtype[ttype] then return tag..globerr(t)
|
||||
return tag..'nil'..comment('ref', level)
|
||||
elseif badtype[ttype] then return tag..globerr(t, level)
|
||||
elseif ttype == 'function' then
|
||||
seen[t] = spath
|
||||
local ok, res = pcall(string.dump, t)
|
||||
local func = ok and (opts.nocode and "function()error('dummy')end" or
|
||||
"loadstring("..safestr(res)..",'@serialized')"..comment(t))
|
||||
return tag..(func or globerr(t))
|
||||
local func = ok and ((opts.nocode and "function() end" or
|
||||
"loadstring("..safestr(res)..",'@serialized')")..comment(t, level))
|
||||
return tag..(func or globerr(t, level))
|
||||
elseif ttype == "table" then
|
||||
if level >= maxl then return tag..'{}'..comment('max') end
|
||||
if level >= maxl then return tag..'{}'..comment('max', level) end
|
||||
seen[t] = spath
|
||||
if next(t) == nil then return tag..'{}'..comment(t) end -- table empty
|
||||
if next(t) == nil then return tag..'{}'..comment(t, level) end -- table empty
|
||||
local maxn, o, out = #t, {}, {}
|
||||
for key = 1, maxn do table.insert(o, key) end
|
||||
for key in pairs(t) do if not o[key] then table.insert(o, key) end end
|
||||
@ -68,7 +69,7 @@ local function s(t, opts)
|
||||
table.insert(sref, seen[t]..'['..(seen[key] or globals[key] or gensym(key))
|
||||
..']'..space..'='..space..(seen[value] or val2str(value,nil,indent)))
|
||||
else
|
||||
if badtype[ktype] then plainindex, key = true, '['..globerr(key)..']' end
|
||||
if badtype[ktype] then plainindex, key = true, '['..globerr(key, level+1)..']' end
|
||||
table.insert(out,val2str(value,key,indent,spath,plainindex,level+1))
|
||||
end
|
||||
end
|
||||
@ -76,7 +77,7 @@ local function s(t, opts)
|
||||
local head = indent and '{\n'..prefix..indent or '{'
|
||||
local body = table.concat(out, ','..(indent and '\n'..prefix..indent or space))
|
||||
local tail = indent and "\n"..prefix..'}' or '}'
|
||||
return (custom and custom(tag,head,body,tail) or tag..head..body..tail)..comment(t)
|
||||
return (custom and custom(tag,head,body,tail) or tag..head..body..tail)..comment(t, level)
|
||||
else return tag..safestr(t) end -- handle all other types
|
||||
end
|
||||
local sepr = indent and "\n" or ";"..space
|
||||
|
13
t/test.lua
13
t/test.lua
@ -57,8 +57,7 @@ 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}))()
|
||||
assert(pcall(function() _a.z() end) == false, "nocode replaces functions with dummy errors: failed")
|
||||
_a = loadstring(serpent.dump(a, {sparse = false}))()
|
||||
assert(#(_a.list) == #(a.list), "size of array part stays the same: failed")
|
||||
|
||||
local diffable = {sortkeys = true, comment = false, nocode = true, indent = ' '}
|
||||
@ -69,3 +68,13 @@ assert(serpent.block(a, diffable) == serpent.block(_a, diffable),
|
||||
_a = loadstring(serpent.dump(a, {sparse = false, nocode = true, maxlevel = 1}))()
|
||||
assert(#(_a.list) == 0, "nested table 1 is empty with maxlevel=1: failed")
|
||||
assert(#(_a[true]) == 0, "nested table 2 is empty with maxlevel=1: failed")
|
||||
|
||||
-- test comment level
|
||||
local dump = serpent.block(a, {comment = 1, nocode = true})
|
||||
assert(dump:find(tostring(a)), "first level comment is present with comment=1: failed")
|
||||
assert(not dump:find(tostring(a.list)), "second level comment is not present with comment=1: failed")
|
||||
assert(dump:find("function%(%) end"), "nocode replaces functions with an empty body: failed")
|
||||
|
||||
assert(serpent.line(nil) == 'nil', "nil value serialized as 'nil': failed")
|
||||
assert(serpent.line(123) == '123', "numeric value serialized as number: failed")
|
||||
assert(serpent.line("123") == '"123"', "string value serialized as string: failed")
|
||||
|
Loading…
Reference in New Issue
Block a user