From 1a9b2a5726b9ba7b5d328e54ae36047ffe1730f2 Mon Sep 17 00:00:00 2001 From: Jeff L <51171427+LebJe@users.noreply.github.com> Date: Tue, 2 Jan 2024 12:05:36 -0500 Subject: [PATCH] 0.4.0 (#10) * Add support for formatted integers * Update README and CHANGELOG * Update tests * Update MagicEnum & Toml++ * Test both `decodeFromFile` and `decode` in `testInvalidInputs`. * Update workflows * Build with MSVC * Run Pre-Commit * Update TOC --- .github/ISSUE_TEMPLATE/bug-report.md | 23 ++-- .github/ISSUE_TEMPLATE/feature_request.md | 9 +- .github/workflows/buildAndTest-Linux.yml | 2 +- .github/workflows/buildAndTest-MacOS.yml | 2 +- .github/workflows/buildAndTest-Windows.yml | 35 ++++-- .github/workflows/pre-commit.yml | 16 +-- .gitignore | 6 +- .pre-commit-config.yaml | 2 +- .prettierrc | 6 + .prettierrc.toml | 4 - CHANGELOG.md | 30 ++++- CMakeLists.txt | 13 +- LICENSE | 2 +- README.md | 117 +++++++++++------- TODO.md | 1 - src/DataTypes/TOMLInt/TOMLInt.hpp | 3 + src/toml.cpp | 19 +-- src/utilities/utilities.cpp | 2 +- src/utilities/utilities.hpp | 2 +- tests/tables.lua | 70 ++++++++--- tests/tests.lua | 86 +++++++++++-- ...-0.3.0-0.rockspec => toml-0.4.0-0.rockspec | 2 +- toml.d.tl | 25 +++- 23 files changed, 346 insertions(+), 131 deletions(-) create mode 100644 .prettierrc delete mode 100644 .prettierrc.toml delete mode 100644 TODO.md rename toml-0.3.0-0.rockspec => toml-0.4.0-0.rockspec (97%) diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md index 21d018c..7b19b46 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.md +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -1,24 +1,29 @@ --- -name: Bug report -about: Create a report to help us improve -title: '' +name: "Bug report" +about: "Create a report to help us improve" +title: "" labels: bug assignees: LebJe - --- ## Describe the bug + A clear and concise description of what the bug is. ## Version and Installation Info -* toml.lua version: -* Installation method: (Manual compilation or `luarocks install toml`) -* Compilation log, or Luarocks installation log: + +- toml.lua version: +- Operating system: +- Installation method: (Manual compilation or `luarocks install toml`) +- Compilation log, or Luarocks installation log: + ```shell -# For compilation logs, include the compilation command, and compiler version +# Paste installation or compilation log here +# For compilation logs, include the compilation command, and compiler version ``` ## To Reproduce + Provide a **minimal** TOML file that demonstrates the issue: ```toml @@ -33,7 +38,9 @@ local toml = require("toml") ``` ## Expected behavior + A clear and concise description of what you expected to happen. ## Additional context + Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index a6b3f1c..5085df2 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -1,10 +1,9 @@ --- -name: Feature request -about: Suggest an idea for this project -title: '' -labels: '' +name: "Feature request" +about: "Suggest an idea for this project" +title: "" +labels: "" assignees: LebJe - --- **Is your feature request related to a problem? Please describe.** diff --git a/.github/workflows/buildAndTest-Linux.yml b/.github/workflows/buildAndTest-Linux.yml index de2b197..8969603 100644 --- a/.github/workflows/buildAndTest-Linux.yml +++ b/.github/workflows/buildAndTest-Linux.yml @@ -16,7 +16,7 @@ jobs: "liblua5.1-0-dev lua5.1", ] steps: - - uses: "actions/checkout@v3" + - uses: "actions/checkout@v4" - name: "Add Dependencies" run: | export COMPILER=${{ matrix.compiler }} diff --git a/.github/workflows/buildAndTest-MacOS.yml b/.github/workflows/buildAndTest-MacOS.yml index 3dd2038..e780d42 100644 --- a/.github/workflows/buildAndTest-MacOS.yml +++ b/.github/workflows/buildAndTest-MacOS.yml @@ -9,7 +9,7 @@ jobs: matrix: lua: ["lua", "luajit"] steps: - - uses: "actions/checkout@v3" + - uses: "actions/checkout@v4" - name: "Install Dependencies" run: | brew install ${{ matrix.lua }} luarocks diff --git a/.github/workflows/buildAndTest-Windows.yml b/.github/workflows/buildAndTest-Windows.yml index 5beec12..b9af0bd 100644 --- a/.github/workflows/buildAndTest-Windows.yml +++ b/.github/workflows/buildAndTest-Windows.yml @@ -7,9 +7,9 @@ jobs: runs-on: "windows-latest" strategy: matrix: - compiler: ["LLVM", "MinGW"] + compiler: ["LLVM", "MinGW", "MSVC"] steps: - - uses: "actions/checkout@v3" + - uses: "actions/checkout@v4" - name: "Install Dependencies" run: "choco install -y cmake ninja" - name: "Build LuaJIT" @@ -21,13 +21,13 @@ jobs: uses: "egor-tensin/setup-clang@v1" - name: "Set up MinGW" if: "${{ matrix.compiler == 'MinGW' }}" - uses: "egor-tensin/setup-mingw@v2" + uses: "e-t-l/setup-mingw@patch-1" with: platform: "x64" - name: "Set up MSVC" if: "${{ matrix.compiler == 'MSVC' }}" - uses: "seanmiddleditch/gha-setup-vsdevenv@master" - - name: "Build Project" + uses: "compnerd/gha-setup-vsdevenv@main" + - name: "Configure Project" run: | New-Item -Path "LuaRocks\tree\luaRocksConfig.lua" -ItemType File @@ -35,13 +35,25 @@ jobs: $env:LUAROCKS_LUADIR=$(Resolve-Path LuaJIT\) $env:LUAROCKS_CONFIG="$(Resolve-Path LuaRocks\tree\luaRocksConfig.lua)" - LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" config variables.LUA_LIBDIR "$(Resolve-Path LuaJIT\bin\)" - - if (("${{ matrix.compiler }}" -eq "MinGW") -or ("${{ matrix.compiler }}" -eq "MSVC")) { + if (("${{ matrix.compiler }}" -eq "MinGW")) { LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" config variables.LINK_FLAGS "$(Resolve-Path LuaJIT\bin\lua51.dll)" } - LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" config cmake_generator "Ninja Multi-Config" + if (("${{ matrix.compiler }}" -eq "MSVC")) { + Copy-Item "$(Resolve-Path libs\lua51.lib)" -Destination "$(Resolve-Path LuaJIT\bin)" + + LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" config "variables.LUA_LIBDIR" "$(Resolve-Path LuaJIT\bin\)" + LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" config cmake_generator "Visual Studio 17 2022" + } else { + LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" config "variables.LUA_LIBDIR" "$(Resolve-Path LuaJIT\bin)" + LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" config cmake_generator "Ninja Multi-Config" + } + - name: "Build Project" + run: | + $env:LUAROCKS_TREE=$(Resolve-Path LuaRocks\tree) + $env:LUAROCKS_LUADIR=$(Resolve-Path LuaJIT\) + $env:LUAROCKS_CONFIG="$(Resolve-Path LuaRocks\tree\luaRocksConfig.lua)" + LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" make - name: "Run Tests" run: | @@ -51,6 +63,9 @@ jobs: $env:LUAROCKS_CONFIG="$(Resolve-Path LuaRocks\tree\luaRocksConfig.lua)" $env:LUA_CPATH="$(Resolve-Path LuaRocks\tree\lib\lua\5.1\)?.dll" $env:LUA_PATH="$(Resolve-Path LuaRocks\tree\share\lua\5.1\)?.lua;$($pwd.Path)\?.lua" + LuaRocks\luarocks.exe --lua-dir "$($env:LUAROCKS_LUADIR)" --tree "$($env:LUAROCKS_TREE)" install luaunit - LuaJIT\bin\luajit.exe tests\tests.lua + if (("${{ matrix.compiler }}" -ne "MSVC")) { + LuaJIT\bin\luajit.exe tests\tests.lua + } diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 5ffb7df..640dd1d 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -1,15 +1,15 @@ -name: "Run pre-commit" -on: ["pull_request"] +name: "Run Pre-Commit" + +on: + push: + branches: + - "main" jobs: PreCommit: runs-on: "macos-latest" steps: - - uses: "actions/checkout@v3" - - name: "Checkout PR" - run: "gh pr checkout ${{ github.event.pull_request.number }}" - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - uses: "actions/checkout@v4" - name: "Install Dependencies" - run: "brew bundle --no-lock" + run: "brew bundle" - uses: "LebJe/pre-commit-composite-action@0.0.1" diff --git a/.gitignore b/.gitignore index 1b1dfc1..bc04c27 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,8 @@ compile_commands.json Brewfile.lock.json .vscode/ .nova/ -.vim/ \ No newline at end of file +.vim/ + +# Test files +test.lua +inspect.lua diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 63b92ca..3dbeba5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: "https://github.com/pre-commit/mirrors-prettier.git" - rev: "v2.4.1" + rev: "v4.0.0-alpha.8" hooks: - id: "prettier" name: "Format YAML & Markdown" diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1ee82da --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 4, + "singleQuote": false, + "useTabs": true +} diff --git a/.prettierrc.toml b/.prettierrc.toml deleted file mode 100644 index 08b1562..0000000 --- a/.prettierrc.toml +++ /dev/null @@ -1,4 +0,0 @@ -trailingComma = "es5" -tabWidth = 4 -singleQuote = false -useTabs = true diff --git a/CHANGELOG.md b/CHANGELOG.md index 78129cc..7ec7729 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,33 @@ 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.3.0](https://github.com/LebJe/toml.lua/releases/tag/0.2.0) - 2023-02-19 +## [0.4.0](https://github.com/LebJe/toml.lua/releases/tag/0.4.0) - 2024-01-02 + +### Added + +- `toml.Int` for formatted integers. + +```lua +local formattedIntegers = { + int1 = toml.Int.new(2582, toml.formatting.int.octal), + int2 = toml.Int.new(3483, toml.formatting.int.binary), + int3 = toml.Int.new(5791, toml.formatting.int.hexadecimal) +} + +print(toml.encode(formattedIntegers)) +--[[ +int1 = 0o5026 +int2 = 0b110110011011 +int3 = 0x169F +--]] +``` + +- `formattedIntsAsUserdata` can be passed to the options table of `toml.decode` (see "Decoding Options" in the README). +- Updated to toml++ v3.4.0. +- Updated to MagicEnum v0.9.5. +- toml.lua compiles with MSVC. + +## [0.3.0](https://github.com/LebJe/toml.lua/releases/tag/0.3.0) - 2023-02-19 ### Added @@ -14,7 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `toml.encodeToFile(data: table, fileOrOptions: string|table)`: - Encodes `data` to the file specified in `fileOrOptions`. the file will be created if it doesn't exist. - When `fileOrOptions` is a string, it simply is the file path. - - 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`. + - 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`. - Added tests that cover: - The property accessors of `toml.Date`, `toml.Time`, `toml.DateTime`, and `toml.TimeOffset`. - `toml.toJSON` and `toml.toYAML`. diff --git a/CMakeLists.txt b/CMakeLists.txt index ab3884c..faae37a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ if(NOT TOML_LUA_VERSION) execute_process(COMMAND ${GIT_EXECUTABLE} describe --dirty WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE ret - OUTPUT_VARIABLE LUA_RAPIDJSON_VERSION + OUTPUT_VARIABLE TOML_LUA_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE ) if(NOT ret EQUAL "0") @@ -41,7 +41,7 @@ FetchContent_Declare( GIT_REPOSITORY "https://github.com/marzer/tomlplusplus.git" GIT_SHALLOW ON GIT_SUBMODULES "" - GIT_TAG "v3.3.0" + GIT_TAG "v3.4.0" ) FetchContent_Declare( @@ -57,13 +57,13 @@ FetchContent_Declare( GIT_REPOSITORY "https://github.com/Neargye/magic_enum.git" GIT_SHALLOW ON GIT_SUBMODULES "" - GIT_TAG "v0.8.2" + GIT_TAG "v0.9.5" ) FetchContent_GetProperties(${TOML++}) if(NOT ${TOML++}_POPULATED) message(STATUS "Cloning ${TOML++}") - FetchContent_Populate(${TOML++}) + #FetchContent_Populate(${TOML++}) FetchContent_MakeAvailable(${TOML++}) endif() @@ -107,7 +107,6 @@ else(UNIX) endif(WIN32) endif(UNIX) - if(NOT LUA_INCLUDE_DIR OR (WIN32 AND NOT LUA_LIBRARIES)) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") find_package(Lua) @@ -127,10 +126,10 @@ set(SOURCES add_library(toml.lua MODULE ${SOURCES}) source_group(src FILES ${SOURCES}) -if(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") +if(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR MSVC) target_link_options(toml.lua PUBLIC ${PROJECT_SOURCE_DIR}\\libs\\lua51.lib) else() - target_link_libraries(toml.lua ${LUA_LIBRARIES}) + target_link_libraries(toml.lua ${LUA_LIBRARIES} tomlplusplus::tomlplusplus) endif() if (LINK_FLAGS) diff --git a/LICENSE b/LICENSE index 8bb1066..7928020 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Jeff Lebrun +Copyright (c) 2024 Jeff Lebrun Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 16b1691..ef48d53 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ toml.lua is a [Lua](https://www.lua.org) wrapper around [toml++](https://github. - [Decoding](#decoding) - [Decoding Options](#decoding-options) - [temporalTypesAsUserData](#temporaltypesasuserdata) + - [formattedIntsAsUserData](#formattedintsasuserdata) - [Encoding](#encoding) - [Error Handling](#error-handling) - [Inline Tables](#inline-tables) @@ -39,13 +40,15 @@ toml.lua is a [Lua](https://www.lua.org) wrapper around [toml++](https://github. - [JSON](#json) - [YAML](#yaml) - [Output Formatting](#output-formatting) + - [Formatting Integers](#formatting-integers) - [Formatting TOML, JSON, or YAML](#formatting-toml-json-or-yaml) - [Date and Time](#date-and-time) - [Dependencies](#dependencies) - [Licenses](#licenses) - [Contributing](#contributing) - + + @@ -55,7 +58,7 @@ Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc) ### Requirements -- A C++ 17 compiler +- A C++ 17 compiler (Clang, GCC, MinGW) - [CMake](https://cmake.org) - Lua C headers (`lua.h`, `lualib.h`, and `lauxlib.h`) - Lua library (e.g. `liblua51.`) @@ -73,7 +76,7 @@ luarocks install toml ##### LLVM -If you have installed Clang, and CMake is configured to use it, you can run: +If you have installed Clang (), and CMake is configured to use it, you can run: ```powershell luarocks install toml @@ -81,7 +84,7 @@ luarocks install toml ##### MinGW -If you have installed MinGW, and CMake is configured to use it, you can run: +If you have installed [MinGW](https://www.mingw-w64.org/), and CMake is configured to use it, you can run: ```powershell luarocks config variables.LINK_FLAGS "path\to\LuaJIT\bin\lua51.dll" @@ -205,55 +208,74 @@ end - `temporalTypesAsUserData = false`: Lua tables are used to represent TOML date and time types. +> The default value is `true` + +##### `formattedIntsAsUserData` + +- `formattedIntsAsUserData = true`: The userdata type `toml.Int` is used to represent integers in octal, binary, or hexadecimal format. +- `formattedIntsAsUserData = false`: Integers in octal, binary, or hexadecimal format will be represented in decimal. + +> The default value is `false` + ```lua local tomlStr = [[ date = 1979-05-27 time = 07:32:00 datetime = 1979-05-27T07:32:00-07:00 + +hexadecimal = 0x16C3 +binary = 0b110110011011 +octal = 0x169F ]] -local table1 = toml.decode(tomlStr, { temporalTypesAsUserData = true }) -local table2 = toml.decode(tomlStr, { temporalTypesAsUserData = false }) +local table1 = toml.decode(tomlStr, { temporalTypesAsUserData = true, formattedIntsAsUserData = true }) +local table2 = toml.decode(tomlStr, { temporalTypesAsUserData = false, formattedIntsAsUserData = false }) print(inspect(table1)) --[[ { date = -- 1979-05-27, <-- toml.Date - time = -- 07:32:00, <-- toml.Time - datetime = -- 1979-05-27T07:32:00-07:00 <-- toml.DateTime + time = -- 07:32:00 <-- toml.Time + datetime = -- 1979-05-27T07:32:00-07:00, <-- toml.DateTime + binary = -- 0b10011011, <-- toml.Int (with `toml.formatting.int.binary` flag) + hexadecimal = -- 0x16c3, <-- toml.Int (with `toml.formatting.int.octal` flag) + octal = -- 0x169f, <-- toml.Int (with `toml.formatting.int.hexadecimal` flag) } --]] print(inspect(table2)) --[[ { - date = { - day = 27, - month = 5, - year = 1979 - }, - datetime = { - date = { - day = 27, - month = 5, - year = 1979 - }, - time = { - hour = 7, - minute = 32, - nanoSecond = 0, - second = 0 - }, - timeOffset = { - minutes = -420 - } - }, - time = { - hour = 7, - minute = 32, - nanoSecond = 0, - second = 0 - } + date = { + day = 27, + month = 5, + year = 1979 + }, + time = { + hour = 7, + minute = 32, + nanoSecond = 0, + second = 0 + }, + datetime = { + date = { + day = 27, + month = 5, + year = 1979 + }, + time = { + hour = 7, + minute = 32, + nanoSecond = 0, + second = 0 + }, + timeOffset = { + minutes = -420 + } + }, + binary = 3483, + hexadecimal = 5827, + octal = 5791, } --]] ``` @@ -412,10 +434,8 @@ print(yaml) ### Output Formatting - #### Formatting TOML, JSON, or YAML @@ -471,13 +504,13 @@ Passing an empty table removes all options, while not providing a table will use --- Allow non-ASCII characters in strings (as opposed to their escaped form, e.g. `\u00DA`). allow_unicode_strings = true, - --- Allow integers with `toml.formatting.int.binary` to be emitted as binary. (Not implemented yet) + --- Allow integers with `toml.formatting.int.binary` to be emitted as binary. allowBinaryIntegers = true, - --- Allow integers with `toml.formatting.int.octal` to be emitted as octal. (Not implemented yet) + --- Allow integers with `toml.formatting.int.octal` to be emitted as octal. allowOctalIntegers = true, - --- Allow integers with `toml.formatting.int.hexadecimal` to be emitted as hexadecimal. (Not implemented yet) + --- Allow integers with `toml.formatting.int.hexadecimal` to be emitted as hexadecimal. allowHexadecimalIntegers = true, --- Apply indentation to tables nested within other tables/arrays. diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 7fd8ba3..0000000 --- a/TODO.md +++ /dev/null @@ -1 +0,0 @@ -- Finish TOMLInt diff --git a/src/DataTypes/TOMLInt/TOMLInt.hpp b/src/DataTypes/TOMLInt/TOMLInt.hpp index 0f6e168..2fd7dec 100644 --- a/src/DataTypes/TOMLInt/TOMLInt.hpp +++ b/src/DataTypes/TOMLInt/TOMLInt.hpp @@ -28,6 +28,9 @@ struct TOMLInt { int64_t getInt() const { return tomlInt.get(); } void setInt(int64_t int64) { tomlInt = int64; } + toml::value_flags getFlags() const { return tomlInt.flags(); } + void setFlags(toml::value_flags flags) { tomlInt.flags(flags); } + bool operator==(const TOMLInt & right) const { return tomlInt == right.tomlInt && tomlInt.flags() == right.tomlInt.flags(); } diff --git a/src/toml.cpp b/src/toml.cpp index 80eda60..9a09740 100644 --- a/src/toml.cpp +++ b/src/toml.cpp @@ -229,19 +229,20 @@ extern "C" { // Setup formatting flags - // auto formattingTable = module.create_named("formatting"); + auto formattingTable = module.create_named("formatting"); - // formattingTable.new_enum( - // "int", { { "binary", toml::value_flags::format_as_binary }, - // { "hexadecimal", toml::value_flags::format_as_hexadecimal }, - // { "octal", toml::value_flags::format_as_octal } }); + formattingTable.new_enum( + "int", { { "binary", toml::value_flags::format_as_binary }, + { "hexadecimal", toml::value_flags::format_as_hexadecimal }, + { "octal", toml::value_flags::format_as_octal } }); - // // Setup UserType = Int + // Setup UserType - Int - // sol::usertype tomlInt = module.new_usertype( - // "Int", sol::constructors()); + sol::usertype tomlInt = module.new_usertype( + "Int", sol::constructors()); - // tomlInt["int"] = sol::property(&TOMLInt::getInt, &TOMLInt::setInt); + tomlInt["int"] = sol::property(&TOMLInt::getInt, &TOMLInt::setInt); + tomlInt["flags"] = sol::property(&TOMLInt::getFlags, &TOMLInt::setFlags); // Setup UserType - Date diff --git a/src/utilities/utilities.cpp b/src/utilities/utilities.cpp index 792415c..6533244 100644 --- a/src/utilities/utilities.cpp +++ b/src/utilities/utilities.cpp @@ -2,7 +2,7 @@ #include "Options.hpp" #include "toml.hpp" #include -#include +#include #include #include diff --git a/src/utilities/utilities.hpp b/src/utilities/utilities.hpp index 301e509..228f642 100644 --- a/src/utilities/utilities.hpp +++ b/src/utilities/utilities.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include diff --git a/tests/tables.lua b/tests/tables.lua index ac22823..2044175 100644 --- a/tests/tables.lua +++ b/tests/tables.lua @@ -2,24 +2,62 @@ local toml = require("toml") local M = {} M.tableForFormattingTest = { - asUserdata = { - dob = toml.DateTime.new(toml.Date.new(1979, 05, 27), toml.Time.new(7, 32, 0, 0), toml.TimeOffset.new(-8, 0)), - }, - asTables = { - dob = { - date = { - day = 27, - month = 5, - year = 1979, + temporalTypes = { + asUserdata = { + dob = toml.DateTime.new( + toml.Date.new(1979, 05, 27), + toml.Time.new(7, 32, 0, 0), + toml.TimeOffset.new(-8, 0) + ), + }, + asTables = { + dob = { + date = { + day = 27, + month = 5, + year = 1979, + }, + time = { + hour = 7, + minute = 32, + nanoSecond = 0, + second = 0, + }, + timeOffset = { + minutes = -480, + }, }, - time = { - hour = 7, - minute = 32, - nanoSecond = 0, - second = 0, + }, + }, + integers = { + asUserdata = { + formattedIntegers = { + int1 = toml.Int.new(2582, toml.formatting.int.octal), + int2 = toml.Int.new(3483, toml.formatting.int.binary), + int3 = toml.Int.new(5791, toml.formatting.int.hexadecimal), + table = { + int1 = toml.Int.new(2582, toml.formatting.int.octal), + int2 = toml.Int.new(3483, toml.formatting.int.binary), + int3 = toml.Int.new(5791, toml.formatting.int.hexadecimal), + }, + array = { + toml.Int.new(2582, toml.formatting.int.octal), + toml.Int.new(3483, toml.formatting.int.binary), + toml.Int.new(5791, toml.formatting.int.hexadecimal), + }, }, - timeOffset = { - minutes = -480, + }, + asTables = { + formattedIntegers = { + int1 = 2582, + int2 = 3483, + int3 = 5791, + table = { + int1 = 2582, + int2 = 3483, + int3 = 5791, + }, + array = { 2582, 3483, 5791 }, }, }, }, diff --git a/tests/tests.lua b/tests/tests.lua index 2c08c2c..c5d8359 100644 --- a/tests/tests.lua +++ b/tests/tests.lua @@ -62,8 +62,22 @@ function TestDecoder:testInvalidInputs() reason = "Error while parsing key-value pair: cannot redefine existing integer as dotted key-value pair", } - local succeeded1, table1 = pcall(toml.decodeFromFile, "tests/test-data/invalidTable.toml") - local succeeded2, table2 = pcall(toml.decodeFromFile, "tests/test-data/multipleDotKeyInvalid.toml") + -- Test `decodeFromFile` + local decodeFromFileSucceeded1, decodeFromFileTable1 = + pcall(toml.decodeFromFile, "tests/test-data/invalidTable.toml") + local decodeFromFileSucceeded2, decodeFromFileTable2 = + pcall(toml.decodeFromFile, "tests/test-data/multipleDotKeyInvalid.toml") + + lu.assertFalse(decodeFromFileSucceeded1) + lu.assertFalse(decodeFromFileSucceeded2) + lu.assertEquals(decodeFromFileTable1, expectedError1) + lu.assertEquals(decodeFromFileTable2, expectedError2) + + -- Test `decode` + local invalidTable = read("tests/test-data/invalidTable.toml") + local multipleDotKeyInvalid = read("tests/test-data/multipleDotKeyInvalid.toml") + local succeeded1, table1 = pcall(toml.decode, invalidTable) + local succeeded2, table2 = pcall(toml.decode, multipleDotKeyInvalid) lu.assertFalse(succeeded1) lu.assertFalse(succeeded2) @@ -72,9 +86,30 @@ function TestDecoder:testInvalidInputs() end function TestDecoder:testFormattingOptions() - local asTables = - toml.decode(toml.encode(data.tableForFormattingTest.asUserdata), { temporalTypesAsUserData = false }) - lu.assertEquals(asTables, data.tableForFormattingTest.asTables) + -- Temporal types + local temporalTypesAsTables = toml.decode( + toml.encode(data.tableForFormattingTest.temporalTypes.asUserdata), + { temporalTypesAsUserData = false } + ) + lu.assertEquals(temporalTypesAsTables, data.tableForFormattingTest.temporalTypes.asTables) + + local temporalTypesAsUserData = toml.decode( + toml.encode(data.tableForFormattingTest.temporalTypes.asUserdata), + { temporalTypesAsUserData = true } + ) + lu.assertEquals(temporalTypesAsUserData, data.tableForFormattingTest.temporalTypes.asUserdata) + + -- Formatted Integers + + local unformattedIntegers = toml.decode( + toml.encode(data.tableForFormattingTest.integers.asUserdata), + { allowBinaryIntegers = false, allowHexadecimalIntegers = false, allowOctalIntegers = false } + ) + lu.assertEquals(unformattedIntegers, data.tableForFormattingTest.integers.asTables) + + local formattedIntegers = + toml.decode(toml.encode(data.tableForFormattingTest.integers.asUserdata), { formattedIntsAsUserData = true }) + lu.assertEquals(formattedIntegers, data.tableForFormattingTest.integers.asUserdata) end local function testFormatters(fileType, formatter) @@ -98,8 +133,8 @@ function TestFormatters:testYAMLFormatter() end function TestUserdataAccessors:testTemporalTypesAccessors() - local dateTime = data.tableForFormattingTest.asUserdata.dob - local dateTimeTable = data.tableForFormattingTest.asTables.dob + local dateTime = data.tableForFormattingTest.temporalTypes.asUserdata.dob + local dateTimeTable = data.tableForFormattingTest.temporalTypes.asTables.dob -- time offset lu.assertEquals(dateTime.timeOffset.minutes, dateTimeTable.timeOffset.minutes) @@ -116,4 +151,41 @@ function TestUserdataAccessors:testTemporalTypesAccessors() lu.assertEquals(dateTime.date.year, dateTimeTable.date.year) end +function TestUserdataAccessors:testIntAccessor() + -- Formatted + local fI = data.tableForFormattingTest.integers.asUserdata.formattedIntegers + + -- Unformatted + local uFI = data.tableForFormattingTest.integers.asTables.formattedIntegers + + lu.assertEquals(fI.int1.int, uFI.int1) + lu.assertEquals(fI.int1.flags, toml.formatting.int.octal) + + lu.assertEquals(fI.int2.int, uFI.int2) + lu.assertEquals(fI.int2.flags, toml.formatting.int.binary) + + lu.assertEquals(fI.int3.int, uFI.int3) + lu.assertEquals(fI.int3.flags, toml.formatting.int.hexadecimal) + + -- fI.table, uFI.table + lu.assertEquals(fI.table.int1.int, uFI.table.int1) + lu.assertEquals(fI.table.int1.flags, toml.formatting.int.octal) + + lu.assertEquals(fI.table.int2.int, uFI.table.int2) + lu.assertEquals(fI.table.int2.flags, toml.formatting.int.binary) + + lu.assertEquals(fI.table.int3.int, uFI.table.int3) + lu.assertEquals(fI.table.int3.flags, toml.formatting.int.hexadecimal) + + -- fI.array, uFI.array + lu.assertEquals(fI.array[1].int, uFI.array[1]) + lu.assertEquals(fI.array[1].flags, toml.formatting.int.octal) + + lu.assertEquals(fI.array[2].int, uFI.array[2]) + lu.assertEquals(fI.array[2].flags, toml.formatting.int.binary) + + lu.assertEquals(fI.array[3].int, uFI.array[3]) + lu.assertEquals(fI.array[3].flags, toml.formatting.int.hexadecimal) +end + os.exit(lu.LuaUnit.run()) diff --git a/toml-0.3.0-0.rockspec b/toml-0.4.0-0.rockspec similarity index 97% rename from toml-0.3.0-0.rockspec rename to toml-0.4.0-0.rockspec index ee72782..a944321 100644 --- a/toml-0.3.0-0.rockspec +++ b/toml-0.4.0-0.rockspec @@ -1,5 +1,5 @@ package = "toml" -version = "0.3.0-0" +version = "0.4.0-0" local v = version:gsub("%-%d", "") diff --git a/toml.d.tl b/toml.d.tl index 4ee08ae..25aae08 100644 --- a/toml.d.tl +++ b/toml.d.tl @@ -84,14 +84,31 @@ local record toml metamethod __tostring: function(DateTime): string end + + record Int + userdata + int: number + + new: function(int: number, flags: formatting.int): Int + + metamethod __tostring: function(Int): string + end + + record formatting + record int + binary: number + octal: number + hexadecimal: number + end + end record DecodingOptions - --formattedIntsAsUserData: boolean + formattedIntsAsUserData: boolean temporalTypesAsUserData: boolean end record EncodeToFileOptions - file: string, + file: string overwrite: boolean end @@ -127,13 +144,13 @@ local record toml indentArrayElements: boolean --- Combination of `indentSubTables` and `indentArrayElements`. - indentation: boolean, + indentation: boolean --- Emit floating-point values with relaxed (human-friendly) precision. --- Warning: Setting this flag may cause serialized documents to no longer round- --- trip correctly since floats might have a less precise value upon being written out --- than they did when being read in. Use this flag at your own risk. - relaxedFloatPrecision: boolean, + relaxedFloatPrecision: boolean --- Avoids the use of whitespace around key-value pairs. terseKeyValuePairs: boolean