mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
fix problem with recursive tables when using the 'process' option
This commit is contained in:
parent
a998635207
commit
d372d2ab08
37
inspect.lua
37
inspect.lua
@ -165,28 +165,37 @@ local function makePath(path, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function processRecursive(process, item, path)
|
local function processRecursive(process, item, path)
|
||||||
if item == nil then return nil end
|
local visited = {}
|
||||||
|
|
||||||
local processed = process(item, path)
|
local function processRecursive2(item, path)
|
||||||
if type(processed) == 'table' then
|
if item == nil then return nil end
|
||||||
local processedCopy = {}
|
if visited[item] then return visited[item] end
|
||||||
local processedKey
|
|
||||||
|
|
||||||
for k,v in pairs(processed) do
|
local processed = process(item, path)
|
||||||
processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY))
|
if type(processed) == 'table' then
|
||||||
if processedKey ~= nil then
|
local processedCopy = {}
|
||||||
processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey))
|
visited[item] = processedCopy
|
||||||
|
local processedKey
|
||||||
|
|
||||||
|
for k,v in pairs(processed) do
|
||||||
|
processedKey = processRecursive2(k, makePath(path, k, inspect.KEY))
|
||||||
|
if processedKey ~= nil then
|
||||||
|
processedCopy[processedKey] = processRecursive2(v, makePath(path, processedKey))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local mt = processRecursive2(getmetatable(processed), makePath(path, inspect.METATABLE))
|
||||||
|
setmetatable(processedCopy, mt)
|
||||||
|
processed = processedCopy
|
||||||
end
|
end
|
||||||
|
return processed
|
||||||
end
|
end
|
||||||
|
|
||||||
local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE))
|
return processRecursive2(item, path)
|
||||||
setmetatable(processedCopy, mt)
|
|
||||||
processed = processedCopy
|
|
||||||
end
|
|
||||||
return processed
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-------------------------------------------------------------------
|
-------------------------------------------------------------------
|
||||||
|
|
||||||
local Inspector = {}
|
local Inspector = {}
|
||||||
|
@ -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