Skip to content

Commit

Permalink
More protocol conformances, CTOML improvements (#1)
Browse files Browse the repository at this point in the history
* More protocol conformances, CTOML improvements.

* Don't generate docs in pull requests.

* Update CHANGELOG.md
  • Loading branch information
LebJe authored Jun 17, 2021
1 parent 971aa8a commit 11461d4
Show file tree
Hide file tree
Showing 30 changed files with 1,188 additions and 899 deletions.
45 changes: 24 additions & 21 deletions .github/workflows/generateDocumentation.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
name: Generate Documentation
name: "Generate Documentation"

on: push
on:
push:
branches:
- "main"

jobs:
GenerateDocumentation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Generate Documentation
uses: SwiftDocOrg/swift-doc@master
with:
inputs: "Sources"
module-name: TOMLKit
format: html
base-url: "https://lebje.github.io/TOMLKit/"
output: ./.build/documentation
- name: Change Permissions
run: sudo chmod o+r -R ./.build/documentation
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./.build/documentation
"GenerateDocumentation":
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v2"
- name: "Generate Documentation"
uses: "SwiftDocOrg/swift-doc@master"
with:
inputs: "Sources"
module-name: "TOMLKit"
format: "html"
base-url: "https://lebje.github.io/TOMLKit/"
output: "./.build/documentation"
- name: "Change Permissions"
run: "sudo chmod o+r -R ./.build/documentation"
- name: "Deploy to GitHub Pages"
uses: "peaceiris/actions-gh-pages@v3"
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: "./.build/documentation"
15 changes: 11 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/hodovani/pre-commit-swift
rev: 0551a937b9f98a839fd98d2c3e6ce0b6c0a1e093
hooks:
- id: swift-format
- repo: "https://github.com/hodovani/pre-commit-swift"
rev: "0551a937b9f98a839fd98d2c3e6ce0b6c0a1e093"
hooks:
- id: "swift-format"
name: "Format Swift"
- repo: "https://github.com/pre-commit/mirrors-prettier"
rev: "4309ae0bc8b9f39c207bca9c5040ec8a35561a41"
hooks:
- id: "prettier"
name: "Format Markdown"
tags: ["md"]
4 changes: 4 additions & 0 deletions .prettierrc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trailingComma = "es5"
tabWidth = 4
singleQuote = false
useTabs = true
35 changes: 33 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,44 @@ 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.1.0](https://github.com/LebJe/TOMLKit/releases/tag/0.1.0) - 2021-06-17

### Added

- Add support for tvOS and watchOS.

- `TOMLArray` conforms to:

- `Collection`,
- `RandomAccessCollection`,
- `BidirectionalCollection`,
- `RangeReplaceableCollection`,
- `MutableCollection`

- `TOMLArray`'s and `TOMLTable`'s two subscripts have been merged: only one subscript is used for insert and retrieving values.
- `structs` conforming to `TOMLValueConvertible` don't insert themselves into a `TOMLArray` or a `TOMLTables` anymore.
Now they have a `tomlValue` property, which converts them into `TOMLValue`s. The `TOMLValue` then inserts itself into a `TOMLTable` or a `TOMLArray`.
- `TOMLTable.remove(at:)` now returns a copy of the removed `TOMLValueConvertible`.
- Added functions to check if a key is in a `TOMLTable`.
- Tried to improve the code in `Sources/CTOML`.

### Removed

- Removed the `inline` parameter from `TOMLTable.init(string:)`.

### Fixed

- Retrieving a value from a `TOMLArray` no longer returns an `Optional` value.

## [0.0.2](https://github.com/LebJe/TOMLKit/releases/tag/0.0.2) - 2021-06-10

### Added
- Made `TOMLType` conform to `RawRepresentable`.

- Made `TOMLType` conform to `RawRepresentable`.

### Fixed
- Fixed formatting.

- Fixed formatting.
- `throw` `DecodingError` instead of crashing when decoding.

## [0.0.1](https://github.com/LebJe/TOMLKit/releases/tag/0.0.1) - 2021-06-10
Expand Down
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FLebJe%2FTOMLKit%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/LebJe/TOMLKit)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FLebJe%2FTOMLKit%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/LebJe/TOMLKit)


TOMLKit is a [Swift](https://swift.org) wrapper around [toml++](https://github.com/marzer/tomlplusplus/), allowing you to read and write [TOML](https://toml.io/en/) files in Swift.

## Table of Contents
Expand Down Expand Up @@ -92,7 +91,7 @@ field1 = ""

**NOTE**

Use `myTable.inline = true // or false` to make a table inline/not inline,
Use `myTable.inline = true/false` to make a table inline/not inline,
and `myTable.inline` to check if a table is inlined.

---
Expand Down Expand Up @@ -132,7 +131,7 @@ do {
}
```

To insert values, make sure the value conforms to `TOMLValueConvertible`, then use the `subscript`, or the `insert(_ value:at:)` method:
To insert values, make sure the value conforms to `TOMLValueConvertible`, then use the `subscript`, or the `insert(_ newElement:at:)` method:

```swift
let table = TOMLTable()
Expand Down Expand Up @@ -231,16 +230,16 @@ Use the [`tomlInt` property in `FixedWidthInteger`](https://lebje.github.io/TOML

```swift
let table: TOMLTable = ...
if let string = table["string"]?.string? {
if let string = table["string"]?.string {
print(string)
}

if let bool = table["bool"]?.bool? {
if let bool = table["bool"]?.bool {
print(bool)
}

if let double = table["InnerTable"]?["InnerInnerTable"].double? {
print(double)
if let double = table["InnerTable"]?.tomlValue["InnerInnerTable"]?["InnerInnerInnerTable"]?.double {
print(double)
}

...
Expand All @@ -250,15 +249,15 @@ if let double = table["InnerTable"]?["InnerInnerTable"].double? {

```swift
let array: TOMLArray = ...
if let string = array[0]?.string? {
if let string = array[0].string? {
print(string)
}

if let bool = array[1]?.bool? {
if let bool = array[1].bool? {
print(bool)
}

if let double = array[2]?[0].double? {
if let double = array[2][0].double? {
print(double)
}

Expand Down
129 changes: 98 additions & 31 deletions Sources/CTOML/Sources/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,8 @@ extern "C" {

// MARK: - Value Insertion

/// Insert a \c bool into \c array .
void arrayInsertBool(CTOMLArray * array, int64_t index, bool boolean) {
auto arr = reinterpret_cast<toml::array *>(array);
arr->insert(arr->cbegin() + index, boolean);
}

/// Insert a \c int64_t into \c array .
void arrayInsertInt(CTOMLArray * array, int64_t index, int64_t integer, uint8_t flags) {

auto v = toml::value { integer };

auto arr = reinterpret_cast<toml::array *>(array);
Expand All @@ -65,58 +58,132 @@ extern "C" {
arr->get(index)->as_integer()->flags(toml::value_flags { flags });
}

/// Insert a \c double into \c array .
void arrayInsertDouble(CTOMLArray * array, int64_t index, double d) {
/// Insert a \c toml::table into \c array .
void arrayInsertTable(CTOMLArray * array, int64_t index, CTOMLTable * _Nonnull table) {
auto arr = reinterpret_cast<toml::array *>(array);
arr->emplace<toml::table>(arr->cbegin() + index, *reinterpret_cast<toml::table *>(table));
}

/// Insert a \c toml::array into \c array .
void arrayInsertArray(CTOMLArray * array, int64_t index, CTOMLArray * _Nonnull array2) {
auto arr = reinterpret_cast<toml::array *>(array);
arr->emplace<toml::array>(arr->cbegin() + index, *reinterpret_cast<toml::array *>(array2));
}

/// Insert a \c toml::node into \c array.
void arrayInsertNode(CTOMLArray * array, int64_t index, CTOMLNode * _Nonnull node) {
auto arr = reinterpret_cast<toml::array *>(array);
arr->insert(arr->cbegin() + index, *reinterpret_cast<toml::node *>(node));
}

/// Replace the \c bool at \c index with \c b .
void arrayReplaceBool(CTOMLArray * array, int64_t index, bool b) {
auto arr = reinterpret_cast<toml::array *>(array);

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->emplace<bool>(arr->cbegin() + index, b);
}

/// Replace the \c int64_t at \c index with \c i .
void arrayReplaceInt(CTOMLArray * array, int64_t index, int64_t i, uint8_t flags) {
auto arr = reinterpret_cast<toml::array *>(array);
auto v = toml::value { i };

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->insert(arr->cbegin() + index, v);
arr->get(index)->as_integer()->flags(toml::value_flags { flags });
}

/// Replace the \c double at \c index with \c d .
void arrayReplaceDouble(CTOMLArray * array, int64_t index, double d) {
auto arr = reinterpret_cast<toml::array *>(array);

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->insert(arr->cbegin() + index, d);
}

/// Insert a \c char* into \c array .
void arrayInsertString(CTOMLArray * array, int64_t index, const char * _Nonnull string) {
/// Replace the \c std::string at \c index with \c s .
void arrayReplaceString(CTOMLArray * array, int64_t index, const char * s) {
auto arr = reinterpret_cast<toml::array *>(array);
arr->insert(arr->cbegin() + index, string);

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->insert(arr->cbegin() + index, std::string(s));
}

/// Insert a \c CTOMLDate into \c array .
void arrayInsertDate(CTOMLArray * array, int64_t index, CTOMLDate date) {
/// Replace the \c toml::date at \c index with \c date .
void arrayReplaceDate(CTOMLArray * array, int64_t index, CTOMLDate date) {
auto arr = reinterpret_cast<toml::array *>(array);

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->insert(arr->cbegin() + index, cTOMLDateToTomlDate(date));
}

/// Insert a \c CTOMLTime into \c array .
void arrayInsertTime(CTOMLArray * array, int64_t index, CTOMLTime time) {
/// Replace the \c toml::time at \c index with \c time .
void arrayReplaceTime(CTOMLArray * array, int64_t index, CTOMLTime time) {
auto arr = reinterpret_cast<toml::array *>(array);

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->insert(arr->cbegin() + index, cTOMLTimeToTomlTime(time));
}

/// Insert a \c CTOMLDateTime into \c array .
void arrayInsertDateTime(CTOMLArray * array, int64_t index, CTOMLDateTime dateTime) {
/// Replace the \c toml::date_time at \c index with \c dateTime .
void arrayReplaceDateTime(CTOMLArray * array, int64_t index, CTOMLDateTime dateTime) {
auto arr = reinterpret_cast<toml::array *>(array);

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->insert(arr->cbegin() + index, cTOMLDateTimeToTomlDateTime(dateTime));
}

/// Insert a \c toml::table into \c array .
void arrayInsertTable(CTOMLArray * array, int64_t index, CTOMLTable * _Nonnull table) {
/// Replace the \c toml::array at \c index with \c arrayToEmplace .
void arrayReplaceArray(CTOMLArray * array, int64_t index, CTOMLArray * _Nonnull arrayToEmplace) {
auto arr = reinterpret_cast<toml::array *>(array);
arr->insert(arr->cbegin() + index, *reinterpret_cast<toml::table *>(table));

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->insert(arr->cbegin() + index, *reinterpret_cast<toml::array *>(arrayToEmplace));
}

/// Insert a \c toml::array into \c array .
void arrayInsertArray(CTOMLArray * array, int64_t index, CTOMLArray * _Nonnull array2) {
/// Replace the \c toml::table at \c index with \c table .
void arrayReplaceTable(CTOMLArray * array, int64_t index, CTOMLTable * _Nonnull table) {
auto arr = reinterpret_cast<toml::array *>(array);
arr->insert(arr->cbegin() + index, *reinterpret_cast<toml::array *>(array2));

auto a = arr->get(0);

if (arr->get(index)) {
arr->erase(arr->cbegin() + index);
}

arr->insert(arr->cbegin() + index, *reinterpret_cast<toml::table *>(table));
}

// MARK: - Value Retrieval

/// Retrieves a \c toml::node from \c array at \c index .
CTOMLNode * _Nullable arrayGetNode(CTOMLArray * array, int64_t index) {
auto arr = reinterpret_cast<toml::array *>(array);
if (arr->get(index)) {
return reinterpret_cast<CTOMLNode *>( arr->get(index) );
} else {
return NULL;
}
CTOMLNode * _Nonnull arrayGetNode(CTOMLArray * array, int64_t index) {
return reinterpret_cast<CTOMLNode *>( reinterpret_cast<toml::array *>(array)->get(index) );
}

// MARK: - Value Removal
Expand Down
Loading

0 comments on commit 11461d4

Please sign in to comment.