Skip to content

Commit 2376f8c

Browse files
authored
0.3.0 (#9)
* * Added `toml.decodeFromFile`, `toml.encodeToFile`. * Renamed `toml.tomlToJSON` and `toml.tomlToYAML` to `toml.toJSON` and `toml.toYAML` * Add tests for the property accessors of `toml.Date`, `toml.Time`, `toml.DateTime`, and `toml.TimeOffset`, and for `toml.toJSON` and `toml.toYAML`
1 parent 7b48dec commit 2376f8c

18 files changed

Lines changed: 410 additions & 133 deletions

File tree

.busted

Lines changed: 0 additions & 1 deletion
This file was deleted.

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repos:
77
- id: "prettier"
88
name: "Format YAML & Markdown"
99
tags: ["md"]
10+
exclude: "tests/test-data"
1011
- repo: "https://github.com/doublify/pre-commit-clang-format.git"
1112
rev: "62302476d0da01515660132d76902359bed0f782"
1213
hooks:

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ 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.3.0](https://github.com/LebJe/toml.lua/releases/tag/0.2.0) - 2023-02-19
9+
10+
### Added
11+
12+
- `toml.decodeFromFile(filePath: string)`:
13+
- Decodes a TOML document at `filePath`. Throws the same errors as `toml.decode`.
14+
- `toml.encodeToFile(data: table, fileOrOptions: string|table)`:
15+
- Encodes `data` to the file specified in `fileOrOptions`. the file will be created if it doesn't exist.
16+
- When `fileOrOptions` is a string, it simply is the file path.
17+
- When `fileOrOptions` is a table, it should have`file`, and optionally, `overwrite` as keys. `file` is the file path, and `overwrite` should be `true` when `file` should be `overwritten` with `data`, and `false` when `data` should be appended to `file`.
18+
- Added tests that cover:
19+
- The property accessors of `toml.Date`, `toml.Time`, `toml.DateTime`, and `toml.TimeOffset`.
20+
- `toml.toJSON` and `toml.toYAML`.
21+
22+
### Changed
23+
24+
- `toml.tomlToJSON` and `toml.tomlToYAML` have been renamed to `toml.toJSON` and `toml.toYAML`.
25+
- They have been renamed because they now have two functions: converting a TOML string to JSON/YAML (as before), or converting a table into JSON/YAML.
26+
- The first parameter can be a string containing TOML (as before), or a table.
27+
828
## [0.2.0](https://github.com/LebJe/toml.lua/releases/tag/0.2.0) - 2023-02-12
929

1030
### Added

README.md

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Build and Test on Linux](https://github.com/LebJe/toml.lua/actions/workflows/buildAndTest-Linux.yml/badge.svg)](https://github.com/LebJe/toml.lua/actions/workflows/buildAndTest-Linux.yml)
77
[![Build and Test on Windows](https://github.com/LebJe/toml.lua/actions/workflows/buildAndTest-Windows.yml/badge.svg)](https://github.com/LebJe/toml.lua/actions/workflows/buildAndTest-Windows.yml)
88

9-
toml.lua is a [Lua](https://www.lua.org) wrapper around [toml++](https://github.com/marzer/tomlplusplus/), allowing you to parse and serialize [TOML](https://toml.io) files in Lua.
9+
toml.lua is a [Lua](https://www.lua.org) wrapper around [toml++](https://github.com/marzer/tomlplusplus/), allowing you to parse and serialize [TOML](https://toml.io) in Lua.
1010

1111
## Table of Contents
1212

@@ -33,18 +33,18 @@ toml.lua is a [Lua](https://www.lua.org) wrapper around [toml++](https://github.
3333
- [temporalTypesAsUserData](#temporaltypesasuserdata)
3434
- [Encoding](#encoding)
3535
- [Error Handling](#error-handling)
36+
- [Inline Tables](#inline-tables)
3637
- [TOML Conversion](#toml-conversion)
3738
- [JSON](#json)
3839
- [YAML](#yaml)
39-
- [Inline Tables](#inline-tables)
4040
- [Output Formatting](#output-formatting)
41-
- [Formatting TOML, JSON, YAML](#formatting-toml-json-yaml)
41+
- [Formatting TOML, JSON, or YAML](#formatting-toml-json-or-yaml)
4242
- [Date and Time](#date-and-time)
4343
- [Dependencies](#dependencies)
4444
- [Licenses](#licenses)
4545
- [Contributing](#contributing)
4646

47-
<!-- Added by: lebje, at: Sun Feb 12 09:15:16 EST 2023 -->
47+
<!-- Added by: lebje, at: Sun Feb 19 22:27:48 EST 2023 -->
4848

4949
<!--te-->
5050

@@ -167,8 +167,12 @@ i = 1979-05-27T07:32:00-07:00
167167
local toml = require("toml")
168168
local inspect = require("inspect")
169169

170+
-- Decode from string
170171
local succeeded, table = pcall(toml.decode, tomlStr)
171172

173+
-- Decode from file
174+
succeeded, table = pcall(toml.decodeFromFile, "configuration.toml")
175+
172176
if succeeded then
173177
-- Use `table`.
174178
print(inspect(table))
@@ -290,9 +294,22 @@ local table = {
290294
inlineTable = inlineTable
291295
}
292296

293-
local tomlDocument = toml.encode(table)
297+
-- Encode to string
298+
local succeeded, documentOrErrorMessage = pcall(toml.encode, table)
294299

295-
print(tomlDocument)
300+
-- Encode to file, this will **append** to the file.
301+
succeeded, documentOrErrorMessage = pcall(toml.encodeToFile, table, "configuration.toml")
302+
303+
-- Encode to file, this will **overwrite** the file.
304+
succeeded, documentOrErrorMessage = pcall(toml.encodeToFile, table, { file = "configuration.toml", overwrite = true })
305+
306+
if succeeded then
307+
-- Successfully encoded to string / wrote to file
308+
print(tomlDocumentOrErrorMessage)
309+
else
310+
-- Error occurred
311+
print(tomlDocumentOrErrorMessage)
312+
end
296313

297314
--[[
298315
inlineTable = { a = 1275892, b = "Hello, World!", c = true, d = 124.2548 }
@@ -349,6 +366,10 @@ else
349366
end
350367
```
351368

369+
### Inline Tables
370+
371+
Use `setmetatable(myTable, { inline = true })` to create an [inline table](https://toml.io/en/v1.0.0#inline-table).
372+
352373
### TOML Conversion
353374

354375
```lua
@@ -371,21 +392,23 @@ i = 1979-05-27T07:32:00-07:00
371392
#### JSON
372393

373394
```lua
374-
local json = toml.tomlToJSON(tomlStr)
395+
-- Convert from a string
396+
local json = toml.toJSON(tomlStr)
397+
398+
-- or from a table
399+
json = toml.toJSON(toml.decode(tomlStr))
400+
375401
print(json)
376402
```
377403

378404
#### YAML
379405

380406
```lua
381-
local yaml = toml.tomlToYAML(tomlStr)
407+
local yaml = toml.toYAML(tomlStr)
408+
yaml = toml.toYAML(toml.decode(tomlStr))
382409
print(yaml)
383410
```
384411

385-
### Inline Tables
386-
387-
Use `setmetatable(myTable, { inline = true })` to create an [inline table](https://toml.io/en/v1.0.0#inline-table).
388-
389412
### Output Formatting
390413

391414
<!---
@@ -422,9 +445,9 @@ int3 = 0x169F
422445
```
423446
-->
424447

425-
#### Formatting TOML, JSON, YAML
448+
#### Formatting TOML, JSON, or YAML
426449

427-
`toml.encode`, `toml.tomlToJSON`, and `toml.tomlToYAML` all take an optional second parameter: a table containing keys that disable or enable different formatting options.
450+
`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.
428451
Passing an empty table removes all options, while not providing a table will use the default options.
429452

430453
```lua

examples/example.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ print("\n\nEncoding:\n")
4545
print(toml.encode(data, { indentation = true }))
4646

4747
print("\n\nTOML to JSON:\n")
48-
print(toml.tomlToJSON(tomlStr, { indentation = true }))
48+
print(toml.toJSON(tomlStr, { indentation = true }))
4949

5050
print("\n\nTOML to YAML:\n")
51-
print(toml.tomlToYAML(tomlStr, { indentation = true }))
51+
print(toml.toYAML(tomlStr, { indentation = true }))

src/DataTypes/TOMLInt/TOMLInt.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "magic_enum.hpp"
21
#include "toml.hpp"
32
#include <DataTypes/TOMLInt/TOMLInt.hpp>
43
#include <bitset>

src/Options.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define OPTIONS_H
33

44
struct Options {
5-
bool formattedIntAsUserData = false;
5+
bool formattedIntsAsUserData = false;
66
bool temporalTypesAsUserData = true;
77
};
88
#endif /* OPTIONS_H */

src/decoding/decoding.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "decoding.hpp"
22
#include <DataTypes/TOMLInt/TOMLInt.hpp>
3-
#include <magic_enum.hpp>
43
#include <sol/sol.hpp>
54
#include <toml.hpp>
65

@@ -20,13 +19,13 @@ void insertNodeInTable(
2019
case toml::node_type::integer: {
2120
auto v = *node->as_integer();
2221
try {
23-
if (options.formattedIntAsUserData && v.flags() != toml::value_flags::none) {
22+
if (options.formattedIntsAsUserData && v.flags() != toml::value_flags::none) {
2423
luaTable[std::get<std::string>(keyOrIndex)] = TOMLInt(v, v.flags());
2524
} else {
2625
luaTable[std::get<std::string>(keyOrIndex)] = v.get();
2726
}
2827
} catch (std::bad_variant_access) {
29-
if (options.formattedIntAsUserData && v.flags() != toml::value_flags::none) {
28+
if (options.formattedIntsAsUserData && v.flags() != toml::value_flags::none) {
3029
luaTable[std::get<size_t>(keyOrIndex)] = TOMLInt(v, v.flags());
3130
} else {
3231
luaTable[std::get<size_t>(keyOrIndex)] = v.get();

src/decoding/decoding.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <Options.hpp>
66
#include <cstddef>
77
#include <sol/sol.hpp>
8-
#include <string>
98
#include <toml.hpp>
109

1110
/// Convert `luaTable` into a `toml::table`.

src/encoding/encoding.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <DataTypes/DateAndTime/dateAndTime.hpp>
44
#include <DataTypes/TOMLInt/TOMLInt.hpp>
55
#include <cassert>
6-
#include <iostream>
7-
#include <magic_enum.hpp>
86
#include <sol/sol.hpp>
97
#include <string>
108
#include <utilities/utilities.hpp>

0 commit comments

Comments
 (0)