From 78c67816efb1b5902878a6667105cf44db67be96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garci=CC=81a=20Cota?= Date: Tue, 29 Mar 2022 14:30:13 +0200 Subject: [PATCH] (break) remove process option --- inspect.lua | 48 ----------------------- inspect.tl | 48 ----------------------- spec/inspect_spec.lua | 90 ------------------------------------------- 3 files changed, 186 deletions(-) diff --git a/inspect.lua b/inspect.lua index f8d69dc..701a54e 100644 --- a/inspect.lua +++ b/inspect.lua @@ -14,9 +14,6 @@ local inspect = {Options = {}, } - - - inspect._VERSION = 'inspect.lua 3.1.0' inspect._URL = 'http://github.com/kikito/inspect.lua' inspect._DESCRIPTION = 'human-readable representations of tables' @@ -153,46 +150,6 @@ local function countCycles(x, cycles) end end -local function makePath(path, a, b) - local newPath = {} - local len = #path - for i = 1, len do newPath[i] = path[i] end - - newPath[len + 1] = a - newPath[len + 2] = b - - return newPath -end - - -local function processRecursive(process, - item, - path, - visited) - 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 - - for k, v in rawpairs(processed) do - processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited) - if processedKey ~= nil then - processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited) - end - end - - local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited) - if type(mt) ~= 'table' then mt = nil end - setmetatable(processedCopy, mt) - processed = processedCopy - end - return processed -end - local function puts(buf, str) buf.n = buf.n + 1 buf[buf.n] = str @@ -304,11 +261,6 @@ function inspect.inspect(root, options) local depth = options.depth or (math.huge) local newline = options.newline or '\n' local indent = options.indent or ' ' - local process = options.process - - if process then - root = processRecursive(process, root, {}, {}) - end local cycles = {} countCycles(root, cycles) diff --git a/inspect.tl b/inspect.tl index 7ac64c8..1a959a7 100644 --- a/inspect.tl +++ b/inspect.tl @@ -7,13 +7,10 @@ local record inspect KEY: table METATABLE: table - type ProcessFunction = function(any, {any}): any - record Options depth: integer newline: string indent: string - process: ProcessFunction end end @@ -153,46 +150,6 @@ local function countCycles(x: any, cycles: {any:integer}): nil end end -local function makePath(path: {any}, a: any, b: any): {any} - local newPath: {any} = {} - local len: integer = #path - for i=1, len do newPath[i] = path[i] end - - newPath[len + 1] = a - newPath[len + 2] = b - - return newPath -end - - -local function processRecursive(process: inspect.ProcessFunction, - item: any, - path: {any}, - visited: {any:any}): any - if item == nil then return nil end - if visited[item] then return visited[item] end - - local processed: any = process(item, path) - if processed is table then - local processedCopy = {} - visited[item] = processedCopy - local processedKey: any - - for k,v in rawpairs(processed) do - processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited) - if processedKey ~= nil then - processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited) - end - end - - local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited) - if type(mt) ~= 'table' then mt = nil end -- ignore not nil/table __metatable field - setmetatable(processedCopy, mt as metatable<{any:any}>) - processed = processedCopy - end - return processed -end - local function puts(buf: table, str:string): nil buf.n = buf.n as integer + 1 buf[buf.n as integer] = str @@ -304,11 +261,6 @@ function inspect.inspect(root: any, options: inspect.Options): string local depth: integer = options.depth or (math.huge as integer) local newline: string = options.newline or '\n' local indent: string = options.indent or ' ' - local process: inspect.ProcessFunction = options.process - - if process then - root = processRecursive(process, root, {}, {}) - end local cycles = {} countCycles(root, cycles) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index a6ddd00..ff7d213 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -262,96 +262,6 @@ describe( 'inspect', function() end) end) - describe('the process option', function() - - it('removes one element', function() - local names = {'Andrew', 'Peter', 'Ann' } - local removeAnn = function(item) if item ~= 'Ann' then return item end end - assert.equals('{ "Andrew", "Peter" }', inspect(names, {process = removeAnn})) - end) - - it('uses the path', function() - local names = {'Andrew', 'Peter', 'Ann' } - local removeThird = function(item, path) if path[1] ~= 3 then return item end end - assert.equals('{ "Andrew", "Peter" }', inspect(names, {process = removeThird})) - end) - - it('replaces items', function() - local names = {'Andrew', 'Peter', 'Ann' } - local filterAnn = function(item) return item == 'Ann' and '' or item end - assert.equals('{ "Andrew", "Peter", "" }', inspect(names, {process = filterAnn})) - end) - - it('nullifies metatables', function() - local mt = {'world'} - local t = setmetatable({'hello'}, mt) - local removeMt = function(item) if item ~= mt then return item end end - assert.equals('{ "hello" }', inspect(t, {process = removeMt})) - end) - - it('nullifies metatables using their paths', function() - local mt = {'world'} - local t = setmetatable({'hello'}, mt) - local removeMt = function(item, path) if path[#path] ~= inspect.METATABLE then return item end end - assert.equals('{ "hello" }', inspect(t, {process = removeMt})) - end) - - it('nullifies the root object', function() - local names = {'Andrew', 'Peter', 'Ann' } - local removeNames = function(item) if item ~= names then return item end end - assert.equals('nil', inspect(names, {process = removeNames})) - end) - - it('changes keys', function() - local dict = {a = 1} - local changeKey = function(item) return item == 'a' and 'x' or item end - assert.equals('{\n x = 1\n}', inspect(dict, {process = changeKey})) - end) - - it('nullifies keys', function() - local dict = {a = 1, b = 2} - local removeA = function(item) return item ~= 'a' and item or nil end - assert.equals('{\n b = 2\n}', inspect(dict, {process = removeA})) - end) - - it('prints inspect.KEY & inspect.METATABLE', function() - local t = {inspect.KEY, inspect.METATABLE} - assert.equals("{ inspect.KEY, inspect.METATABLE }", inspect(t)) - end) - - it('marks key paths with inspect.KEY and metatables with inspect.METATABLE', function() - local t = { [{a=1}] = setmetatable({b=2}, {c=3}) } - - local items = {} - local addItem = function(item, path) - items[#items + 1] = {item = item, path = path} - return item - end - - inspect(t, {process = addItem}) - - assert.same({ - {item = t, path = {}}, - {item = {a=1}, path = {{a=1}, inspect.KEY}}, - {item = 'a', path = {{a=1}, inspect.KEY, 'a', inspect.KEY}}, - {item = 1, path = {{a=1}, inspect.KEY, 'a'}}, - {item = setmetatable({b=2}, {c=3}), path = {{a=1}}}, - {item = 'b', path = {{a=1}, 'b', inspect.KEY}}, - {item = 2, path = {{a=1}, 'b'}}, - {item = {c=3}, path = {{a=1}, inspect.METATABLE}}, - {item = 'c', path = {{a=1}, inspect.METATABLE, 'c', inspect.KEY}}, - {item = 3, path = {{a=1}, inspect.METATABLE, 'c'}} - }, items) - - 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) - describe('metatables', function() it('includes the metatable as an extra hash attribute', function()