Add parameter defaults and better error messages for dump and dump_to_file (#37)

* feat(dump_to_file): Allow empty object_name

* feat(dump, dump_to_file): Allow empty max_depth

* fix(dump, dump_to_file): Return actual name in error

* style(dump): Rename `dumped_object_name` to `object_name`
- To be consistent with `dump_to_file`
This commit is contained in:
Ron Au 2023-05-11 06:23:01 +10:00 committed by GitHub
parent a11efd275d
commit 39383b9fb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -60,18 +60,22 @@ local function table_dump(key, value, depth, max_depth)
end
end
DMFMod.dump = function (self, dumped_object, dumped_object_name, max_depth)
if dmf.check_wrong_argument_type(self, "dump", "dumped_object_name", dumped_object_name, "string", "nil") or
dmf.check_wrong_argument_type(self, "dump", "max_depth", max_depth, "number")
DMFMod.dump = function (self, dumped_object, object_name, max_depth)
if dmf.check_wrong_argument_type(self, "dump", "dumped_object_name", object_name, "string", "nil") or
dmf.check_wrong_argument_type(self, "dump", "max_depth", max_depth, "number", "nil")
then
return
end
local object_type = type(dumped_object)
max_depth = max_depth or 1
if object_type ~= "table" then
local error_message = "(dump): \"object_name\" is not a table. It's " .. object_type
local error_message = string.format(
'(dump): "%s" is not a table but of type "%s"',
object_name or "Dump object",
object_type
)
if object_type ~= "nil" then
error_message = error_message .. " (" .. tostring(dumped_object) .. ")"
@ -81,13 +85,8 @@ DMFMod.dump = function (self, dumped_object, dumped_object_name, max_depth)
return
end
if dumped_object_name then
log_and_console_print(string.format("<%s>", dumped_object_name))
end
if not max_depth then
self:error("(dump): maximum depth is not specified")
return
if object_name then
log_and_console_print(string.format("<%s>", object_name))
end
local success, error_message = pcall(function()
@ -100,17 +99,12 @@ DMFMod.dump = function (self, dumped_object, dumped_object_name, max_depth)
self:error("(dump): %s", tostring(error_message))
end
if dumped_object_name then
log_and_console_print(string.format("</%s>", dumped_object_name))
if object_name then
log_and_console_print(string.format("</%s>", object_name))
end
end
local function table_dump_to_file(dumped_table, dumped_table_name, max_depth)
-- #####################
@ -348,17 +342,22 @@ end
DMFMod.dump_to_file = function (self, dumped_object, object_name, max_depth)
if dmf.check_wrong_argument_type(self, "dump_to_file", "object_name", object_name, "string") or
dmf.check_wrong_argument_type(self, "dump_to_file", "max_depth", max_depth, "number")
if dmf.check_wrong_argument_type(self, "dump_to_file", "object_name", object_name, "string", "nil") or
dmf.check_wrong_argument_type(self, "dump_to_file", "max_depth", max_depth, "number", "nil")
then
return
end
local object_type = type(dumped_object)
object_name = object_name or "mod_dump_to_file"
max_depth = max_depth or 1
if object_type ~= "table" then
local error_message = "(dump_to_file): \"object_name\" is not a table. It's " .. object_type
local error_message = string.format(
'(dump_to_file): "%s" is not a table but of type "%s"',
object_name or "Dump object",
object_type
)
if object_type ~= "nil" then
error_message = error_message .. " (" .. tostring(dumped_object) .. ")"