Skip to content

Commit ffc000e

Browse files
committed
- 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.
1 parent 44a4056 commit ffc000e

File tree

10 files changed

+157
-30
lines changed

10 files changed

+157
-30
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,

src/utilities/utilities.cpp

Lines changed: 59 additions & 18 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,28 @@ 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-
}
96+
// void addFlag(toml::format_flags & flags, sol::table & flagsTable, toml::format_flags flagToAdd) {
97+
// auto tableFlag = flagsTable[camelCase(magic_enum::enum_name(flagToAdd))];
98+
99+
// std::cout << "addFlag: \nenum_name: " << magic_enum::enum_name(flagToAdd) << "\n";
100+
101+
// std::cout << "camel case name: " << camelCase(magic_enum::enum_name(flagToAdd)) << "\n";
102+
103+
// if (tableFlag.valid()) {
104+
// std::cout << "Is valid: " << camelCase(magic_enum::enum_name(flagToAdd)) << "\n";
105+
// if (tableFlag.get<bool>()) {
106+
// flags |= flagToAdd;
107+
// }
108+
// } else {
109+
// // Use default
110+
// if (defaultFlags[flagToAdd]) {
111+
// flags |= flagToAdd;
112+
// }
113+
// };
114+
// }
105115

106116
toml::format_flags tableToFormatFlags(sol::optional<sol::table> t) {
107-
auto flags = format_flags();
117+
auto flags = format_flags::none;
108118

109119
// Set default flags.
110120
if (!t) {
@@ -116,14 +126,45 @@ toml::format_flags tableToFormatFlags(sol::optional<sol::table> t) {
116126

117127
// User passed an empty table to clear all flags.
118128
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);
129+
130+
// Set default flags, and allow user to override
131+
//flags = defaultFormatFlags();
132+
133+
// Set default flags, and allow user to override
134+
std::map<format_flags, bool> userFlags = defaultFlags;
135+
136+
for (auto [flag, enabled] : userFlags) {
137+
std::string camelCaseFlagName = camelCase(magic_enum::enum_name(flag));
138+
if (table[camelCaseFlagName].valid()) {
139+
userFlags[flag] = table[camelCaseFlagName].get<bool>();
140+
}
123141
}
124-
125-
// `format_flags::indentation` is not returned from `enum_values`.
126-
addFlag(flags, table, format_flags::indentation);
142+
143+
// `format_flags::indentation` is returned as an empty string from `magic_enum::enum_name`, so we must handle it separately.
144+
if (table["indentation"].valid()) {
145+
userFlags[toml::format_flags::indentation] = table["indentation"].get<bool>();
146+
}
147+
148+
std::cout << "UserFlags:\n";
149+
150+
for (auto [flag, enabled] : userFlags) {
151+
std::cout << "Flag: " << magic_enum::enum_name(flag) << ", Enabled: " << (enabled ? "True" : "False") << "\n";
152+
}
153+
154+
for (auto [flag, enabled] : userFlags) {
155+
if (enabled) flags |= flag;
156+
}
157+
158+
// constexpr auto f = magic_enum::enum_values<format_flags>();
159+
// for (auto flag : f) {
160+
// addFlag(flags, table, flag);
161+
// }
162+
163+
// // `format_flags::indentation` is not returned from `enum_values`, so we must handle it separately.
164+
// if (table["indentation"].valid()) {
165+
// flags |= toml::format_flags::indentation;
166+
// }
167+
127168
return flags;
128169
}
129170

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"

tests/tests.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
lu = require("luaunit")
1+
local lu = require("luaunit")
22
local toml = require("toml")
33
local data = require("tests/tables")
44

@@ -29,6 +29,17 @@ function TestEncoder:testEncodeMassiveTable()
2929
lu.assertEquals(toml.encode(data.tableForMassiveToml), massive)
3030
end
3131

32+
function TestEncoder:testEncodingOptions()
33+
local terseKeysToml = read("tests/test-data/encoding/terseKeys+qoutedTimestamps.toml")
34+
35+
lu.assertEquals(toml.encode(toml.decode(terseKeysToml), { terseKeyValuePairs = true, quoteDatesAndTimes = true }), terseKeysToml)
36+
37+
local noIndentationJSON = read("tests/test-data/encoding/noIndentation.json")
38+
39+
lu.assertEquals(toml.toJSON(toml.decode(terseKeysToml), { indentation = false }), noIndentationJSON)
40+
41+
end
42+
3243
function TestDecoder:testDecodeSamples()
3344
lu.assertEquals(data.tableForTestConfigToml, toml.decodeFromFile("tests/test-data/testConfig.toml"))
3445
lu.assertEquals(data.tableForTestConfig2Toml, toml.decodeFromFile("tests/test-data/testConfig2.toml"))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "toml"
2-
version = "0.4.0-0"
2+
version = "0.4.1-0"
33

44
local v = version:gsub("%-%d", "")
55

toml.d.tl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local record toml
2020

2121
--- Encodes a Lua table into a TOML document, and **appends** that document to a file.
2222
--- @param data table: The Lua table to be encoded.
23-
--- @param file string: The file to write the document to/
23+
--- @param file string: The file to write the document to.
2424
--- @param options FormattingOptions|nil: Options for formatting the YAML document. Pass `{}` to remove all options, or no table to use the default options.
2525
encodeToFile: function(data: { string: any }, fileOrOptions: string|EncodeToFileOptions, options: FormattingOptions|nil)
2626

0 commit comments

Comments
 (0)