From 9cd95bb0eb73521bef07f6f0d5e8b78f3e309ebf Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 29 Jun 2025 12:33:57 +0300 Subject: [PATCH] fix toml encoder --- src/coders/toml.cpp | 50 +++++++++++++++++++++++- src/logic/scripting/lua/libs/libtoml.cpp | 3 +- test/coders/toml.cpp | 3 ++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/coders/toml.cpp b/src/coders/toml.cpp index 1a249435..1c05dff4 100644 --- a/src/coders/toml.cpp +++ b/src/coders/toml.cpp @@ -239,13 +239,52 @@ dv::value toml::parse(std::string_view file, std::string_view source) { return TomlReader(file, source).read(); } +static void to_string(std::stringstream& ss, const dv::value& value); + +static void list_to_string(std::stringstream& ss, const dv::value& list) { + ss << "["; + int index = 0; + for (const auto& value : list) { + if (index > 0) { + ss << ", "; + } + to_string(ss, value); + index++; + } + ss << "]"; +} + +static void object_to_string(std::stringstream& ss, const dv::value& object) { + ss << "{"; + int index = 0; + for (const auto& [key, value] : object.asObject()) { + if (index > 0) { + ss << ", "; + } + ss << key << " = "; + to_string(ss, value); + index++; + } + ss << "}"; +} + +static void to_string(std::stringstream& ss, const dv::value& value) { + if (value.isObject()) { + object_to_string(ss, value); + } else if (value.isList()) { + list_to_string(ss, value); + } else { + ss << value; + } +} + std::string toml::stringify(const dv::value& root, const std::string& name) { std::stringstream ss; if (!name.empty()) { ss << "[" << name << "]\n"; } for (const auto& [key, value] : root.asObject()) { - if (!value.isObject()) { + if (!value.isObject() && !value.isList()) { ss << key << " = " << value << "\n"; } } @@ -253,6 +292,15 @@ std::string toml::stringify(const dv::value& root, const std::string& name) { if (value.isObject()) { ss << "\n" << toml::stringify(value, name.empty() ? key : name + "." + key); + } else if (value.isList()) { + ss << (name.empty() ? key : name + "." + key) << " = ["; + for (size_t i = 0; i < value.size(); i++) { + if (i > 0) { + ss << ", "; + } + to_string(ss, value[i]); + } + ss << "]"; } } return ss.str(); diff --git a/src/logic/scripting/lua/libs/libtoml.cpp b/src/logic/scripting/lua/libs/libtoml.cpp index d2f7b87e..bf5461c0 100644 --- a/src/logic/scripting/lua/libs/libtoml.cpp +++ b/src/logic/scripting/lua/libs/libtoml.cpp @@ -23,4 +23,5 @@ static int l_toml_parse(lua::State* L) { const luaL_Reg tomllib[] = { {"tostring", lua::wrap}, {"parse", lua::wrap}, - {NULL, NULL}}; + {NULL, NULL} +}; diff --git a/test/coders/toml.cpp b/test/coders/toml.cpp index c520298f..2eccde07 100644 --- a/test/coders/toml.cpp +++ b/test/coders/toml.cpp @@ -26,6 +26,9 @@ TEST(TOML, EncodeDecode) { object["score"] = score; object["visible"] = visible; object["data"] = srcBytes; + object["values"] = dv::list({ + 5, 3, std::string("hello"), dv::object({{"number", 1234}}) + }); text = toml::stringify(object, ""); std::cout << text << std::endl;