From 39383b9fb153a4d20e0eea1b17de07aabfdc70c8 Mon Sep 17 00:00:00 2001 From: Ron Au <5785323+ronvoluted@users.noreply.github.com> Date: Thu, 11 May 2023 06:23:01 +1000 Subject: [PATCH] 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` --- .../mods/dmf/modules/debug/table_dump.lua | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/dmf/scripts/mods/dmf/modules/debug/table_dump.lua b/dmf/scripts/mods/dmf/modules/debug/table_dump.lua index 608b765..8ba3b2f 100644 --- a/dmf/scripts/mods/dmf/modules/debug/table_dump.lua +++ b/dmf/scripts/mods/dmf/modules/debug/table_dump.lua @@ -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("", dumped_object_name)) + if object_name then + log_and_console_print(string.format("", 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) .. ")"