Skip to content

Commit 30598e4

Browse files
authored
0.4.1 (#14)
* 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. * Updated to magic_enum v0.9.7. * Added encoding options tests. * Update to luarocks 3.11 in CI * Switch to LuaJIT rolling tarball
1 parent 44a4056 commit 30598e4

File tree

13 files changed

+130
-42
lines changed

13 files changed

+130
-42
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.4.1](https://github.com/LebJe/toml.lua/releases/tag/0.4.1) - 2024-11-19
9+
10+
### Fixed
11+
- 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.
12+
- Resolved CMake `FetchContent_Populate` warning.
13+
14+
### Changed
15+
- Updated to magic_enum v0.9.7.
16+
17+
### Added
18+
- Added tests for encoding options.
19+
820
## [0.4.0](https://github.com/LebJe/toml.lua/releases/tag/0.4.0) - 2024-01-02
921

1022
### Added

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,24 @@ FetchContent_Declare(
5757
GIT_REPOSITORY "https://github.com/Neargye/magic_enum.git"
5858
GIT_SHALLOW ON
5959
GIT_SUBMODULES ""
60-
GIT_TAG "v0.9.5"
60+
GIT_TAG "v0.9.7"
6161
)
6262

6363
FetchContent_GetProperties(${TOML++})
6464
if(NOT ${TOML++}_POPULATED)
6565
message(STATUS "Cloning ${TOML++}")
66-
#FetchContent_Populate(${TOML++})
6766
FetchContent_MakeAvailable(${TOML++})
6867
endif()
6968

7069
FetchContent_GetProperties(${SOL2})
7170
if(NOT ${SOL2}_POPULATED)
7271
message(STATUS "Cloning ${SOL2}")
73-
FetchContent_Populate(${SOL2})
7472
FetchContent_MakeAvailable(${SOL2})
7573
endif()
7674

7775
FetchContent_GetProperties(${MAGIC_ENUM})
7876
if(NOT ${MAGIC_ENUM}_POPULATED)
7977
message(STATUS "Cloning ${MAGIC_ENUM}")
80-
FetchContent_Populate(${MAGIC_ENUM})
8178
FetchContent_MakeAvailable(${MAGIC_ENUM})
8279
endif()
8380

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ int3 = 0x169F
481481

482482
#### Formatting TOML, JSON, or YAML
483483

484-
`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.
484+
`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.
485485
Passing an empty table removes all options, while not providing a table will use the default options.
486486

487487
```lua
@@ -514,10 +514,10 @@ Passing an empty table removes all options, while not providing a table will use
514514
allowHexadecimalIntegers = true,
515515

516516
--- Apply indentation to tables nested within other tables/arrays.
517-
indentSubTables = true,
517+
indentSubTables = false,
518518

519519
--- Apply indentation to array elements when the array is forced to wrap over multiple lines.
520-
indentArrayElements = true,
520+
indentArrayElements = false,
521521

522522
--- Combination of `indentSubTables` and `indentArrayElements`.
523523
indentation = true,

scripts/buildLuaJIT.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ New-Item -Path $installDir -ItemType "directory" -Name "include"
1717
New-Item -Path $installDir -ItemType "directory" -Name "share"
1818
New-Item -Path $installDir -ItemType "directory" -Name "jit"
1919

20-
Invoke-WebRequest -Uri "https://luajit.org/download/LuaJIT-2.1.0-beta3.tar.gz" -OutFile "LuaJIT.tar.gz"
20+
Invoke-WebRequest -Uri "https://github.com/LuaJIT/LuaJIT/archive/refs/tags/v2.1.ROLLING.tar.gz" -OutFile "LuaJIT.tar.gz"
2121
tar -xzvf "LuaJIT.tar.gz"
2222
Remove-Item "LuaJIT.tar.gz"
23-
Set-Location "LuaJIT-2.1.0-beta3"
23+
Set-Location "LuaJIT-2.1.ROLLING"
2424

2525
mingw32-make.exe
2626

@@ -35,4 +35,4 @@ Copy-Item "src\luajit.exe" -Destination "$($installDir)\bin"
3535
Copy-Item "src\lua51.dll" -Destination "$($installDir)\bin"
3636

3737
Set-Location "$($pwd.Path)\.."
38-
Remove-Item -Recurse -Force "LuaJit-2.1.0-beta3"
38+
Remove-Item -Recurse -Force "LuaJIT-2.1.ROLLING"

scripts/buildLuaRocks.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/bin/bash
22

33
sudo apt install build-essential libreadline-dev unzip wget
4-
wget https://luarocks.org/releases/luarocks-3.8.0.tar.gz
5-
tar -zxpf luarocks-3.8.0.tar.gz
6-
rm luarocks-3.8.0.tar.gz
7-
cd luarocks-3.8.0
4+
wget https://luarocks.org/releases/luarocks-3.11.0.tar.gz
5+
tar -zxpf luarocks-3.11.0.tar.gz
6+
rm luarocks-3.11.0.tar.gz
7+
cd luarocks-3.11.0
88
./configure
99
make
1010
sudo make install

scripts/installLuaRocks.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ if (-not (Test-Path -Path $installDir)) {
99

1010
$installDir = Resolve-Path $installDir
1111

12-
Invoke-WebRequest -Uri "http://luarocks.github.io/luarocks/releases/luarocks-3.8.0-windows-32.zip" -OutFile "luarocks.zip"
12+
Invoke-WebRequest -Uri "http://luarocks.github.io/luarocks/releases/luarocks-3.11.0-windows-32.zip" -OutFile "luarocks.zip"
1313
Expand-Archive -Path "luarocks.zip" -DestinationPath "."
14-
Move-Item -Path "luarocks-3.8.0-windows-32\luarocks.exe" -Destination "$($installDir)"
15-
Move-Item -Path "luarocks-3.8.0-windows-32\luarocks-admin.exe" -Destination "$($installDir)"
14+
Move-Item -Path "luarocks-3.11.0-windows-32\luarocks.exe" -Destination "$($installDir)"
15+
Move-Item -Path "luarocks-3.11.0-windows-32\luarocks-admin.exe" -Destination "$($installDir)"
1616

1717
Remove-Item "luarocks.zip"
18-
Remove-Item -Recurse -Force "luarocks-3.8.0-windows-32"
18+
Remove-Item -Recurse -Force "luarocks-3.11.0-windows-32"

src/utilities/utilities.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static std::map<format_flags, bool> defaultFlags =
1919
{ format_flags::allow_hexadecimal_integers, true },
2020
{ format_flags::allow_octal_integers, true },
2121
{ format_flags::indent_sub_tables, false },
22+
{ format_flags::indent_array_elements, false },
2223
{ format_flags::indentation, true },
2324
{ format_flags::relaxed_float_precision, false },
2425
{ format_flags::terse_key_value_pairs, false } };
@@ -92,19 +93,8 @@ inline toml::format_flags defaultFormatFlags() {
9293
return flags;
9394
}
9495

95-
void addFlag(toml::format_flags & flags, sol::table & flagsTable, toml::format_flags flagToAdd) {
96-
auto tableFlag = flagsTable[camelCase(magic_enum::enum_name(flagToAdd))];
97-
98-
if (tableFlag.valid()) {
99-
flags |= tableFlag.get<bool>() ? flagToAdd : flags;
100-
} else {
101-
// Use default
102-
flags |= defaultFlags[flagToAdd] ? flagToAdd : flags;
103-
};
104-
}
105-
10696
toml::format_flags tableToFormatFlags(sol::optional<sol::table> t) {
107-
auto flags = format_flags();
97+
auto flags = format_flags::none;
10898

10999
// Set default flags.
110100
if (!t) {
@@ -116,14 +106,26 @@ toml::format_flags tableToFormatFlags(sol::optional<sol::table> t) {
116106

117107
// User passed an empty table to clear all flags.
118108
if (table.empty()) return flags;
119-
120-
constexpr auto f = magic_enum::enum_values<format_flags>();
121-
for (auto flag : f) {
122-
addFlag(flags, table, flag);
109+
110+
// Set default flags, and allow user to override
111+
std::map<format_flags, bool> userFlags = defaultFlags;
112+
113+
for (auto [flag, enabled] : userFlags) {
114+
std::string camelCaseFlagName = camelCase(magic_enum::enum_name(flag));
115+
if (table[camelCaseFlagName].valid()) {
116+
userFlags[flag] = table[camelCaseFlagName].get<bool>();
117+
}
123118
}
124-
125-
// `format_flags::indentation` is not returned from `enum_values`.
126-
addFlag(flags, table, format_flags::indentation);
119+
120+
// `format_flags::indentation` is returned as an empty string from `magic_enum::enum_name`, so we must handle it separately.
121+
if (table["indentation"].valid()) {
122+
userFlags[toml::format_flags::indentation] = table["indentation"].get<bool>();
123+
}
124+
125+
for (auto [flag, enabled] : userFlags) {
126+
if (enabled) flags |= flag;
127+
}
128+
127129
return flags;
128130
}
129131

src/utilities/utilities.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/// Converts a string into [Camel Case](https://en.wikipedia.org/wiki/Camel_case).
1414
///
15-
/// The code in this function is based on https://en.wikipedia.org/wiki/Camel_case
15+
/// The code in this function is based on https://codereview.stackexchange.com/a/263761
1616
std::string camelCase(std::string s) noexcept;
1717

1818
/// Version of `camelCase` that accepts a `string_view`.
@@ -36,7 +36,8 @@ std::string parseErrorToString(toml::parse_error e);
3636
/// Inserts the values in `e` into `table`.
3737
void parseErrorToTable(toml::parse_error e, sol::table & table);
3838

39-
/// Takes a Lua table, with keys representing flag names, and values
39+
/// Takes a Lua table, with keys representing flag names, and values, and converts it to `toml::format_flags`.
40+
/// 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.
4041
toml::format_flags tableToFormatFlags(sol::optional<sol::table> t);
4142

4243
Options tableToOptions(sol::optional<sol::table> t);
@@ -55,4 +56,9 @@ std::optional<std::string> keyToString(sol::object key);
5556
/// If a string is not on the stack, then an integer from `luaL_argerror` is returned.
5657
std::variant<int, toml::table *> getTableFromStringInState(sol::state_view state, int index = 1);
5758

59+
template <>
60+
struct magic_enum::customize::enum_range<toml::format_flags> {
61+
static constexpr bool is_flags = true;
62+
};
63+
5864
#endif /* UTILITIES_H */
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"database" : {
3+
"data" : [
4+
[
5+
"delta",
6+
"phi"
7+
],
8+
[
9+
3.1400000000000001
10+
]
11+
],
12+
"enabled" : true,
13+
"ports" : [
14+
8000,
15+
8001,
16+
8002
17+
],
18+
"temp_targets" : {
19+
"case" : 72,
20+
"cpu" : 79.5
21+
}
22+
},
23+
"owner" : {
24+
"dob" : "1979-05-27T07:32:00-08:00",
25+
"name" : "Tom Preston-Werner"
26+
},
27+
"servers" : {
28+
"alpha" : {
29+
"ip" : "10.0.0.1",
30+
"role" : "frontend"
31+
},
32+
"beta" : {
33+
"ip" : "10.0.0.2",
34+
"role" : "backend"
35+
}
36+
},
37+
"title" : "TOML Example"
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
title="TOML Example"
2+
3+
[database]
4+
data=[ [ "delta", "phi" ], [ 3.1400000000000001 ] ]
5+
enabled=true
6+
ports=[ 8000, 8001, 8002 ]
7+
8+
[database.temp_targets]
9+
case=72
10+
cpu=79.5
11+
12+
[owner]
13+
dob="1979-05-27T07:32:00-08:00"
14+
name="Tom Preston-Werner"
15+
16+
[servers.alpha]
17+
ip="10.0.0.1"
18+
role="frontend"
19+
20+
[servers.beta]
21+
ip="10.0.0.2"
22+
role="backend"

0 commit comments

Comments
 (0)