From 6a2b2062b3ea1f3a3dad8de9697017005a325755 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 13 Oct 2024 12:09:56 +0300 Subject: [PATCH] feat: array of tables support --- src/coders/toml.cpp | 22 ++++++++++++++++++++++ test/coders/toml.cpp | 9 ++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/coders/toml.cpp b/src/coders/toml.cpp index 468e94b6..080df90e 100644 --- a/src/coders/toml.cpp +++ b/src/coders/toml.cpp @@ -178,7 +178,29 @@ class TomlReader : BasicParser { } char c = nextChar(); if (c == '[') { + if (hasNext() && peek() == '[') { + pos++; + // parse list of tables + dv::value& list = parseLValue(root); + if (list == nullptr) { + list = dv::list(); + } else if (!list.isList()) { + throw error("target is not an array"); + } + expect(']'); + expect(']'); + dv::value section = dv::object(); + readSection(section, root); + list.add(std::move(section)); + return; + } + // parse table dv::value& section = parseLValue(root); + if (section == nullptr) { + section = dv::object(); + } else if (!section.isObject()) { + throw error("target is not a table"); + } expect(']'); readSection(section, root); return; diff --git a/test/coders/toml.cpp b/test/coders/toml.cpp index fa491ba3..e8a63f19 100644 --- a/test/coders/toml.cpp +++ b/test/coders/toml.cpp @@ -74,7 +74,14 @@ inline std::string SRC_EXAMPLE = "[servers.beta]\n" "ip = \"10.0.0.2\"\n" "role = \"\"\"back\\\n" - "end\"\"\""; + "end\"\"\"\n" + "\n" + "[[users]]\n" + "name = \"noname\"\n" + "\n" + "[[users]]\n" + "name = \"user1\"\n" + "suspended = true\n"; TEST(TOML, ExampleCode) { try {