diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ec7729..fca3e66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.4.1](https://github.com/LebJe/toml.lua/releases/tag/0.4.1) - 2024-11-19 + +### Fixed +- The formatting options that were passed as a parameter to `toml.encode`, `toml.encodeToFile`, `toml.toJSON`, and `toml.toYAML` previously had no effect when overriding values. +- Resolved CMake `FetchContent_Populate` warning. + +### Changed +- Updated to magic_enum v0.9.7. + +### Added +- Added tests for encoding options. + ## [0.4.0](https://github.com/LebJe/toml.lua/releases/tag/0.4.0) - 2024-01-02 ### Added diff --git a/CMakeLists.txt b/CMakeLists.txt index faae37a..ed4573d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,27 +57,24 @@ FetchContent_Declare( GIT_REPOSITORY "https://github.com/Neargye/magic_enum.git" GIT_SHALLOW ON GIT_SUBMODULES "" - GIT_TAG "v0.9.5" + GIT_TAG "v0.9.7" ) FetchContent_GetProperties(${TOML++}) if(NOT ${TOML++}_POPULATED) message(STATUS "Cloning ${TOML++}") - #FetchContent_Populate(${TOML++}) FetchContent_MakeAvailable(${TOML++}) endif() FetchContent_GetProperties(${SOL2}) if(NOT ${SOL2}_POPULATED) message(STATUS "Cloning ${SOL2}") - FetchContent_Populate(${SOL2}) FetchContent_MakeAvailable(${SOL2}) endif() FetchContent_GetProperties(${MAGIC_ENUM}) if(NOT ${MAGIC_ENUM}_POPULATED) message(STATUS "Cloning ${MAGIC_ENUM}") - FetchContent_Populate(${MAGIC_ENUM}) FetchContent_MakeAvailable(${MAGIC_ENUM}) endif() diff --git a/README.md b/README.md index ef48d53..313401c 100644 --- a/README.md +++ b/README.md @@ -481,7 +481,7 @@ int3 = 0x169F #### Formatting TOML, JSON, or YAML -`toml.encode`, `toml.encodeToFile`, `toml.toJSON`, and `toml.toYAML` all take an optional second parameter: a table containing keys that disable or enable different formatting options. +`toml.encode`, `toml.encodeToFile`, `toml.toJSON`, and `toml.toYAML` all take an optional second (third in the case of `toml.encodeToFile`) parameter: a table containing keys that disable or enable different formatting options. Passing an empty table removes all options, while not providing a table will use the default options. ```lua @@ -514,10 +514,10 @@ Passing an empty table removes all options, while not providing a table will use allowHexadecimalIntegers = true, --- Apply indentation to tables nested within other tables/arrays. - indentSubTables = true, + indentSubTables = false, --- Apply indentation to array elements when the array is forced to wrap over multiple lines. - indentArrayElements = true, + indentArrayElements = false, --- Combination of `indentSubTables` and `indentArrayElements`. indentation = true, diff --git a/scripts/buildLuaJIT.ps1 b/scripts/buildLuaJIT.ps1 index da7526e..75bc300 100644 --- a/scripts/buildLuaJIT.ps1 +++ b/scripts/buildLuaJIT.ps1 @@ -17,10 +17,10 @@ New-Item -Path $installDir -ItemType "directory" -Name "include" New-Item -Path $installDir -ItemType "directory" -Name "share" New-Item -Path $installDir -ItemType "directory" -Name "jit" -Invoke-WebRequest -Uri "https://luajit.org/download/LuaJIT-2.1.0-beta3.tar.gz" -OutFile "LuaJIT.tar.gz" +Invoke-WebRequest -Uri "https://github.com/LuaJIT/LuaJIT/archive/refs/tags/v2.1.ROLLING.tar.gz" -OutFile "LuaJIT.tar.gz" tar -xzvf "LuaJIT.tar.gz" Remove-Item "LuaJIT.tar.gz" -Set-Location "LuaJIT-2.1.0-beta3" +Set-Location "LuaJIT-2.1.ROLLING" mingw32-make.exe @@ -35,4 +35,4 @@ Copy-Item "src\luajit.exe" -Destination "$($installDir)\bin" Copy-Item "src\lua51.dll" -Destination "$($installDir)\bin" Set-Location "$($pwd.Path)\.." -Remove-Item -Recurse -Force "LuaJit-2.1.0-beta3" +Remove-Item -Recurse -Force "LuaJIT-2.1.ROLLING" \ No newline at end of file diff --git a/scripts/buildLuaRocks.sh b/scripts/buildLuaRocks.sh index 9c25652..57014c8 100755 --- a/scripts/buildLuaRocks.sh +++ b/scripts/buildLuaRocks.sh @@ -1,10 +1,10 @@ #!/bin/bash sudo apt install build-essential libreadline-dev unzip wget -wget https://luarocks.org/releases/luarocks-3.8.0.tar.gz -tar -zxpf luarocks-3.8.0.tar.gz -rm luarocks-3.8.0.tar.gz -cd luarocks-3.8.0 +wget https://luarocks.org/releases/luarocks-3.11.0.tar.gz +tar -zxpf luarocks-3.11.0.tar.gz +rm luarocks-3.11.0.tar.gz +cd luarocks-3.11.0 ./configure make sudo make install diff --git a/scripts/installLuaRocks.ps1 b/scripts/installLuaRocks.ps1 index 760c929..b5e74f8 100644 --- a/scripts/installLuaRocks.ps1 +++ b/scripts/installLuaRocks.ps1 @@ -9,10 +9,10 @@ if (-not (Test-Path -Path $installDir)) { $installDir = Resolve-Path $installDir -Invoke-WebRequest -Uri "http://luarocks.github.io/luarocks/releases/luarocks-3.8.0-windows-32.zip" -OutFile "luarocks.zip" +Invoke-WebRequest -Uri "http://luarocks.github.io/luarocks/releases/luarocks-3.11.0-windows-32.zip" -OutFile "luarocks.zip" Expand-Archive -Path "luarocks.zip" -DestinationPath "." -Move-Item -Path "luarocks-3.8.0-windows-32\luarocks.exe" -Destination "$($installDir)" -Move-Item -Path "luarocks-3.8.0-windows-32\luarocks-admin.exe" -Destination "$($installDir)" +Move-Item -Path "luarocks-3.11.0-windows-32\luarocks.exe" -Destination "$($installDir)" +Move-Item -Path "luarocks-3.11.0-windows-32\luarocks-admin.exe" -Destination "$($installDir)" Remove-Item "luarocks.zip" -Remove-Item -Recurse -Force "luarocks-3.8.0-windows-32" \ No newline at end of file +Remove-Item -Recurse -Force "luarocks-3.11.0-windows-32" \ No newline at end of file diff --git a/src/utilities/utilities.cpp b/src/utilities/utilities.cpp index 6533244..8eeede0 100644 --- a/src/utilities/utilities.cpp +++ b/src/utilities/utilities.cpp @@ -19,6 +19,7 @@ static std::map defaultFlags = { format_flags::allow_hexadecimal_integers, true }, { format_flags::allow_octal_integers, true }, { format_flags::indent_sub_tables, false }, + { format_flags::indent_array_elements, false }, { format_flags::indentation, true }, { format_flags::relaxed_float_precision, false }, { format_flags::terse_key_value_pairs, false } }; @@ -92,19 +93,8 @@ inline toml::format_flags defaultFormatFlags() { return flags; } -void addFlag(toml::format_flags & flags, sol::table & flagsTable, toml::format_flags flagToAdd) { - auto tableFlag = flagsTable[camelCase(magic_enum::enum_name(flagToAdd))]; - - if (tableFlag.valid()) { - flags |= tableFlag.get() ? flagToAdd : flags; - } else { - // Use default - flags |= defaultFlags[flagToAdd] ? flagToAdd : flags; - }; -} - toml::format_flags tableToFormatFlags(sol::optional t) { - auto flags = format_flags(); + auto flags = format_flags::none; // Set default flags. if (!t) { @@ -116,14 +106,26 @@ toml::format_flags tableToFormatFlags(sol::optional t) { // User passed an empty table to clear all flags. if (table.empty()) return flags; - - constexpr auto f = magic_enum::enum_values(); - for (auto flag : f) { - addFlag(flags, table, flag); + + // Set default flags, and allow user to override + std::map userFlags = defaultFlags; + + for (auto [flag, enabled] : userFlags) { + std::string camelCaseFlagName = camelCase(magic_enum::enum_name(flag)); + if (table[camelCaseFlagName].valid()) { + userFlags[flag] = table[camelCaseFlagName].get(); + } } - - // `format_flags::indentation` is not returned from `enum_values`. - addFlag(flags, table, format_flags::indentation); + + // `format_flags::indentation` is returned as an empty string from `magic_enum::enum_name`, so we must handle it separately. + if (table["indentation"].valid()) { + userFlags[toml::format_flags::indentation] = table["indentation"].get(); + } + + for (auto [flag, enabled] : userFlags) { + if (enabled) flags |= flag; + } + return flags; } diff --git a/src/utilities/utilities.hpp b/src/utilities/utilities.hpp index 228f642..83c1664 100644 --- a/src/utilities/utilities.hpp +++ b/src/utilities/utilities.hpp @@ -12,7 +12,7 @@ /// Converts a string into [Camel Case](https://en.wikipedia.org/wiki/Camel_case). /// -/// The code in this function is based on https://en.wikipedia.org/wiki/Camel_case +/// The code in this function is based on https://codereview.stackexchange.com/a/263761 std::string camelCase(std::string s) noexcept; /// Version of `camelCase` that accepts a `string_view`. @@ -36,7 +36,8 @@ std::string parseErrorToString(toml::parse_error e); /// Inserts the values in `e` into `table`. void parseErrorToTable(toml::parse_error e, sol::table & table); -/// Takes a Lua table, with keys representing flag names, and values +/// Takes a Lua table, with keys representing flag names, and values, and converts it to `toml::format_flags`. +/// If the table is nil, all format flags are set to their default value, if the table is empty, `toml::format_flags` is set to none. toml::format_flags tableToFormatFlags(sol::optional t); Options tableToOptions(sol::optional t); @@ -55,4 +56,9 @@ std::optional keyToString(sol::object key); /// If a string is not on the stack, then an integer from `luaL_argerror` is returned. std::variant getTableFromStringInState(sol::state_view state, int index = 1); +template <> +struct magic_enum::customize::enum_range { + static constexpr bool is_flags = true; +}; + #endif /* UTILITIES_H */ diff --git a/tests/test-data/encoding/noIndentation.json b/tests/test-data/encoding/noIndentation.json new file mode 100644 index 0000000..1b4f0c1 --- /dev/null +++ b/tests/test-data/encoding/noIndentation.json @@ -0,0 +1,38 @@ +{ +"database" : { +"data" : [ +[ +"delta", +"phi" +], +[ +3.1400000000000001 +] +], +"enabled" : true, +"ports" : [ +8000, +8001, +8002 +], +"temp_targets" : { +"case" : 72, +"cpu" : 79.5 +} +}, +"owner" : { +"dob" : "1979-05-27T07:32:00-08:00", +"name" : "Tom Preston-Werner" +}, +"servers" : { +"alpha" : { +"ip" : "10.0.0.1", +"role" : "frontend" +}, +"beta" : { +"ip" : "10.0.0.2", +"role" : "backend" +} +}, +"title" : "TOML Example" +} \ No newline at end of file diff --git a/tests/test-data/encoding/terseKeys+qoutedTimestamps.toml b/tests/test-data/encoding/terseKeys+qoutedTimestamps.toml new file mode 100644 index 0000000..562f729 --- /dev/null +++ b/tests/test-data/encoding/terseKeys+qoutedTimestamps.toml @@ -0,0 +1,22 @@ +title="TOML Example" + +[database] +data=[ [ "delta", "phi" ], [ 3.1400000000000001 ] ] +enabled=true +ports=[ 8000, 8001, 8002 ] + + [database.temp_targets] + case=72 + cpu=79.5 + +[owner] +dob="1979-05-27T07:32:00-08:00" +name="Tom Preston-Werner" + +[servers.alpha] +ip="10.0.0.1" +role="frontend" + +[servers.beta] +ip="10.0.0.2" +role="backend" \ No newline at end of file diff --git a/tests/tests.lua b/tests/tests.lua index c5d8359..62f43c0 100644 --- a/tests/tests.lua +++ b/tests/tests.lua @@ -1,4 +1,4 @@ -lu = require("luaunit") +local lu = require("luaunit") local toml = require("toml") local data = require("tests/tables") @@ -29,6 +29,17 @@ function TestEncoder:testEncodeMassiveTable() lu.assertEquals(toml.encode(data.tableForMassiveToml), massive) end +function TestEncoder:testEncodingOptions() + local terseKeysToml = read("tests/test-data/encoding/terseKeys+qoutedTimestamps.toml") + + lu.assertEquals(toml.encode(toml.decode(terseKeysToml), { terseKeyValuePairs = true, quoteDatesAndTimes = true }), terseKeysToml) + + local noIndentationJSON = read("tests/test-data/encoding/noIndentation.json") + + lu.assertEquals(toml.toJSON(toml.decode(terseKeysToml), { indentation = false }), noIndentationJSON) + +end + function TestDecoder:testDecodeSamples() lu.assertEquals(data.tableForTestConfigToml, toml.decodeFromFile("tests/test-data/testConfig.toml")) lu.assertEquals(data.tableForTestConfig2Toml, toml.decodeFromFile("tests/test-data/testConfig2.toml")) diff --git a/toml-0.4.0-0.rockspec b/toml-0.4.1-0.rockspec similarity index 97% rename from toml-0.4.0-0.rockspec rename to toml-0.4.1-0.rockspec index a944321..9ea0376 100644 --- a/toml-0.4.0-0.rockspec +++ b/toml-0.4.1-0.rockspec @@ -1,5 +1,5 @@ package = "toml" -version = "0.4.0-0" +version = "0.4.1-0" local v = version:gsub("%-%d", "") diff --git a/toml.d.tl b/toml.d.tl index 25aae08..41e4e54 100644 --- a/toml.d.tl +++ b/toml.d.tl @@ -20,7 +20,7 @@ local record toml --- Encodes a Lua table into a TOML document, and **appends** that document to a file. --- @param data table: The Lua table to be encoded. - --- @param file string: The file to write the document to/ + --- @param file string: The file to write the document to. --- @param options FormattingOptions|nil: Options for formatting the YAML document. Pass `{}` to remove all options, or no table to use the default options. encodeToFile: function(data: { string: any }, fileOrOptions: string|EncodeToFileOptions, options: FormattingOptions|nil)