extend filter with a path parameter. Fixes #12

This commit is contained in:
kikito 2014-02-09 18:42:38 +01:00
parent b718a2e55c
commit a2ccd9220c
2 changed files with 32 additions and 9 deletions

View File

@ -145,6 +145,13 @@ local function parse_filter(filter)
return function(x) return dictionary[x] end return function(x) return dictionary[x] end
end end
local function makePath(path, key)
local newPath, len = {}, #path
for i=1, len do newPath[i] = path[i] end
newPath[len+1] = key
return newPath
end
------------------------------------------------------------------- -------------------------------------------------------------------
function inspect.inspect(rootObject, options) function inspect.inspect(rootObject, options)
options = options or {} options = options or {}
@ -202,11 +209,11 @@ function inspect.inspect(rootObject, options)
local function putKey(k) local function putKey(k)
if isIdentifier(k) then return puts(k) end if isIdentifier(k) then return puts(k) end
puts( "[" ) puts( "[" )
putValue(k) putValue(k, {})
puts("]") puts("]")
end end
local function putTable(t) local function putTable(t, path)
if alreadyVisited(t) then if alreadyVisited(t) then
puts('<table ', getId(t), '>') puts('<table ', getId(t), '>')
elseif level >= depth then elseif level >= depth then
@ -230,7 +237,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]) putValue(t[i], makePath(path, i))
end end
for _,k in ipairs(dictKeys) do for _,k in ipairs(dictKeys) do
@ -238,14 +245,14 @@ function inspect.inspect(rootObject, options)
tabify() tabify()
putKey(k) putKey(k)
puts(' = ') puts(' = ')
putValue(t[k]) putValue(t[k], makePath(path, k))
end end
if mt then if mt then
needsComma = commaControl(needsComma) needsComma = commaControl(needsComma)
tabify() tabify()
puts('<metatable> = ') puts('<metatable> = ')
putValue(mt) putValue(mt, makePath(path, '<metatable>'))
end end
end) end)
@ -261,8 +268,8 @@ function inspect.inspect(rootObject, options)
end end
-- putvalue is forward-declared before putTable & putKey -- putvalue is forward-declared before putTable & putKey
putValue = function(v) putValue = function(v, path)
if filter(v) then if filter(v, path) then
puts('<filtered>') puts('<filtered>')
else else
local tv = type(v) local tv = type(v)
@ -272,14 +279,14 @@ 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) putTable(v, path)
else else
puts('<',tv,' ',getId(v),'>') puts('<',tv,' ',getId(v),'>')
end end
end end
end end
putValue(rootObject) putValue(rootObject, {})
return table.concat(buffer) return table.concat(buffer)
end end

View File

@ -216,6 +216,22 @@ describe( 'inspect', function()
end) end)
it('filters by path', function()
local people = { tony = { age = 21 }, martha = { age = 34} }
local hideMarthaAge = function(_,path)
return table.concat(path, '.') == 'martha.age'
end
assert.equals(inspect(people, {filter = hideMarthaAge}), [[{
martha = {
age = <filtered>
},
tony = {
age = 21
}
}]])
end)
it('does not increase the table ids', function() it('does not increase the table ids', function()
local a = {'this is a'} local a = {'this is a'}
local b = {} local b = {}