kikito / inspect.lua

Human-readable representation of Lua tables
https://github.com/kikito/inspect.lua
MIT License
1.38k stars 196 forks source link

Safe inspect version #46

Closed zugzug90 closed 3 years ago

zugzug90 commented 5 years ago

For cases when you need to print out whatsoever results of some function call you can use another version of inspect function: safe_inspect. Usage scenario example below:

inspect = require("inspect")

-- Returns 2 values: one is a Lua table, another - just a primitive type - the number
function two_values()
    return {x = 15, y = 6}, 2
end

print(inspect.safe_inspect(two_values()))

Result will be:

{
  x = 15,
  y = 6
}

This allows to prevent primitive type handling errors when using original inspect function by just throwing away all the rest returned values if they are not of the table type otherwise using 2nd arg as parameters map like inspect function does.

See issue: https://github.com/kikito/inspect.lua/issues/39

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-1.6%) to 97.706% when pulling 6ba0d91c3a817434661dc9e8bb5b65fce490c7da on zugzug90:safe-inspect into b611db6bfa9c12ce35dd4972032fbbd2ad5ba965 on kikito:master.

justinmk commented 5 years ago

"safe" isn't really the right term here. And this seems like a very specific function for a very specific case.

39 can be addressed simply by wrapping the input in a table parentheses, e.g.:

print(inspect.inspect((two_values())))

It doesn't make sense to have an entirely new function instead of that.

kikito commented 3 years ago

Use a table to wrap all the possible values returned by the function. Instead of:

print(inspect(two_values())

Do this:

print(inspect({two_values()})

You can usually drop the two parenthesis of the call and just leave the brackets. This is equivalent to the previous one:

print(inspect{two_values()})

So inspect(call()) is "unsafe", but inspect{call()} is "safe"