mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
process now handles most values. But what to do with keys?
This commit is contained in:
parent
7195bfa423
commit
a1b30ad8a3
38
inspect.lua
38
inspect.lua
@ -143,12 +143,32 @@ local function makePath(path, key)
|
|||||||
return newPath
|
return newPath
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local processRecursive = function(object, path, process)
|
||||||
|
local processed = process(object, path)
|
||||||
|
if type(processed) == 'table' then
|
||||||
|
local processed2 = {}
|
||||||
|
|
||||||
|
for k,v in pairs(processed) do
|
||||||
|
processed2[k] = process(v, makePath(path, k), process)
|
||||||
|
end
|
||||||
|
|
||||||
|
local mt = process(getmetatable(processed), makePath(path, '<metatable>'))
|
||||||
|
setmetatable(processed2, mt)
|
||||||
|
processed = processed2
|
||||||
|
end
|
||||||
|
return processed
|
||||||
|
end
|
||||||
|
|
||||||
-------------------------------------------------------------------
|
-------------------------------------------------------------------
|
||||||
function inspect.inspect(rootObject, options)
|
function inspect.inspect(root, options)
|
||||||
options = options or {}
|
options = options or {}
|
||||||
local depth = options.depth or math.huge
|
local depth = options.depth or math.huge
|
||||||
|
local process = options.process
|
||||||
|
if process then
|
||||||
|
root = processRecursive(root, {}, process)
|
||||||
|
end
|
||||||
|
|
||||||
local tableAppearances = countTableAppearances(rootObject)
|
local tableAppearances = countTableAppearances(root)
|
||||||
|
|
||||||
local buffer = {}
|
local buffer = {}
|
||||||
local maxIds = setmetatable({}, maxIdsMetaTable)
|
local maxIds = setmetatable({}, maxIdsMetaTable)
|
||||||
@ -203,7 +223,7 @@ function inspect.inspect(rootObject, options)
|
|||||||
puts("]")
|
puts("]")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function putTable(t, path)
|
local function putTable(t)
|
||||||
if alreadyVisited(t) then
|
if alreadyVisited(t) then
|
||||||
puts('<table ', getId(t), '>')
|
puts('<table ', getId(t), '>')
|
||||||
elseif level >= depth then
|
elseif level >= depth then
|
||||||
@ -227,7 +247,7 @@ function inspect.inspect(rootObject, options)
|
|||||||
for i=1, length do
|
for i=1, length do
|
||||||
needsComma = commaControl(needsComma)
|
needsComma = commaControl(needsComma)
|
||||||
puts(' ')
|
puts(' ')
|
||||||
putValue(t[i], makePath(path, i))
|
putValue(t[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
for _,k in ipairs(dictKeys) do
|
for _,k in ipairs(dictKeys) do
|
||||||
@ -235,14 +255,14 @@ function inspect.inspect(rootObject, options)
|
|||||||
tabify()
|
tabify()
|
||||||
putKey(k)
|
putKey(k)
|
||||||
puts(' = ')
|
puts(' = ')
|
||||||
putValue(t[k], makePath(path, k))
|
putValue(t[k])
|
||||||
end
|
end
|
||||||
|
|
||||||
if mt then
|
if mt then
|
||||||
needsComma = commaControl(needsComma)
|
needsComma = commaControl(needsComma)
|
||||||
tabify()
|
tabify()
|
||||||
puts('<metatable> = ')
|
puts('<metatable> = ')
|
||||||
putValue(mt, makePath(path, '<metatable>'))
|
putValue(mt)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -258,7 +278,7 @@ function inspect.inspect(rootObject, options)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- putvalue is forward-declared before putTable & putKey
|
-- putvalue is forward-declared before putTable & putKey
|
||||||
putValue = function(v, path)
|
putValue = function(v)
|
||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
|
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
@ -266,13 +286,13 @@ function inspect.inspect(rootObject, options)
|
|||||||
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
|
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
|
||||||
puts(tostring(v))
|
puts(tostring(v))
|
||||||
elseif tv == 'table' then
|
elseif tv == 'table' then
|
||||||
putTable(v, path)
|
putTable(v)
|
||||||
else
|
else
|
||||||
puts('<',tv,' ',getId(v),'>')
|
puts('<',tv,' ',getId(v),'>')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
putValue(rootObject, {})
|
putValue(root, {})
|
||||||
|
|
||||||
return table.concat(buffer)
|
return table.concat(buffer)
|
||||||
end
|
end
|
||||||
|
@ -180,6 +180,48 @@ describe( 'inspect', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('the process option', function()
|
||||||
|
|
||||||
|
it('can be used to remove a particular element easily', function()
|
||||||
|
local names = {'Andrew', 'Peter', 'Ann' }
|
||||||
|
local removeAnn = function(item) if item ~= 'Ann' then return item end end
|
||||||
|
assert.equals(inspect(names, {process = removeAnn}), '{ "Andrew", "Peter" }')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('can use the path', function()
|
||||||
|
local names = {'Andrew', 'Peter', 'Ann' }
|
||||||
|
local removeThird = function(item, path) if path[1] ~= 3 then return item end end
|
||||||
|
assert.equals(inspect(names, {process = removeThird}), '{ "Andrew", "Peter" }')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('can replace values', function()
|
||||||
|
local names = {'Andrew', 'Peter', 'Ann' }
|
||||||
|
local filterAnn = function(item) return item == 'Ann' and '<filtered>' or item end
|
||||||
|
assert.equals(inspect(names, {process = filterAnn}), '{ "Andrew", "Peter", "<filtered>" }')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('can nullify metatables', function()
|
||||||
|
local mt = {'world'}
|
||||||
|
local t = setmetatable({'hello'}, mt)
|
||||||
|
local removeMt = function(item) if item ~= mt then return item end end
|
||||||
|
assert.equals(inspect(t, {process = removeMt}), '{ "hello" }')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('can nullify metatables via their paths', function()
|
||||||
|
local mt = {'world'}
|
||||||
|
local t = setmetatable({'hello'}, mt)
|
||||||
|
local removeMt = function(item, path) if path[#path] ~= '<metatable>' then return item end end
|
||||||
|
assert.equals(inspect(t, {process = removeMt}), '{ "hello" }')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('can nullify the root object', function()
|
||||||
|
local names = {'Andrew', 'Peter', 'Ann' }
|
||||||
|
local removeNames = function(item) if item ~= names then return item end end
|
||||||
|
assert.equals(inspect(names, {process = removeNames}), 'nil')
|
||||||
|
end)
|
||||||
|
|
||||||
|
end)
|
||||||
|
|
||||||
describe('metatables', function()
|
describe('metatables', function()
|
||||||
|
|
||||||
it('includes the metatable as an extra hash attribute', function()
|
it('includes the metatable as an extra hash attribute', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user