mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
Merge pull request #26 from andreashofer123/process
fix problem with recursive tables when using the 'process' option
This commit is contained in:
commit
833b0bc183
40
inspect.lua
40
inspect.lua
@ -164,29 +164,33 @@ local function makePath(path, ...)
|
|||||||
return newPath
|
return newPath
|
||||||
end
|
end
|
||||||
|
|
||||||
local function processRecursive(process, item, path)
|
local function processRecursive(process, item, path, visited)
|
||||||
if item == nil then return nil end
|
|
||||||
|
if item == nil then return nil end
|
||||||
|
if visited[item] then return visited[item] end
|
||||||
|
|
||||||
|
local processed = process(item, path)
|
||||||
|
if type(processed) == 'table' then
|
||||||
|
local processedCopy = {}
|
||||||
|
visited[item] = processedCopy
|
||||||
|
local processedKey
|
||||||
|
|
||||||
local processed = process(item, path)
|
for k,v in pairs(processed) do
|
||||||
if type(processed) == 'table' then
|
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited)
|
||||||
local processedCopy = {}
|
if processedKey ~= nil then
|
||||||
local processedKey
|
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited)
|
||||||
|
end
|
||||||
for k,v in pairs(processed) do
|
|
||||||
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY))
|
|
||||||
if processedKey ~= nil then
|
|
||||||
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey))
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE))
|
local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited)
|
||||||
setmetatable(processedCopy, mt)
|
setmetatable(processedCopy, mt)
|
||||||
processed = processedCopy
|
processed = processedCopy
|
||||||
end
|
end
|
||||||
return processed
|
return processed
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-------------------------------------------------------------------
|
-------------------------------------------------------------------
|
||||||
|
|
||||||
local Inspector = {}
|
local Inspector = {}
|
||||||
@ -315,7 +319,7 @@ function inspect.inspect(root, options)
|
|||||||
local process = options.process
|
local process = options.process
|
||||||
|
|
||||||
if process then
|
if process then
|
||||||
root = processRecursive(process, root, {})
|
root = processRecursive(process, root, {}, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
local inspector = setmetatable({
|
local inspector = setmetatable({
|
||||||
|
@ -315,6 +315,12 @@ describe( 'inspect', function()
|
|||||||
}, items)
|
}, items)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('handles recursive tables correctly', function()
|
||||||
|
local tbl = { 1,2,3}
|
||||||
|
tbl.loop = tbl
|
||||||
|
inspect(tbl, { process=function(x) return x end})
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('metatables', function()
|
describe('metatables', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user