Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfixes for the release #113

Merged
merged 2 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ To import `ktoml` library you need to add following dependencies to your code:
<dependency>
<groupId>com.akuleshov7</groupId>
<artifactId>ktoml-core</artifactId>
<version>0.2.10</version>
<version>0.2.11</version>
</dependency>
<dependency>
<groupId>com.akuleshov7</groupId>
<artifactId>ktoml-file</artifactId>
<version>0.2.10</version>
<version>0.2.11</version>
</dependency>
```
</details>
Expand All @@ -95,17 +95,17 @@ To import `ktoml` library you need to add following dependencies to your code:
<summary>Gradle Groovy</summary>

```groovy
implementation 'com.akuleshov7:ktoml-core:0.2.10'
implementation 'com.akuleshov7:ktoml-file:0.2.10'
implementation 'com.akuleshov7:ktoml-core:0.2.11'
implementation 'com.akuleshov7:ktoml-file:0.2.11'
```
</details>

<details>
<summary>Gradle Kotlin</summary>

```kotlin
implementation("com.akuleshov7:ktoml-core:0.2.10")
implementation("com.akuleshov7:ktoml-file:0.2.10")
implementation("com.akuleshov7:ktoml-core:0.2.11")
implementation("com.akuleshov7:ktoml-file:0.2.11")
```
</details>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ package com.akuleshov7.ktoml.parsers

import com.akuleshov7.ktoml.exceptions.ParseException

/**
* method to find the beginning of the comments in TOML string
*
* @param startSearchFrom the index after that the search will be done
* @return the index of the first hash symbol or the index of the end of the string
*/
internal fun String.findBeginningOfTheComment(startSearchFrom: Int) =
(startSearchFrom until this.length).filter { this[it] == '#' }.minOrNull() ?: this.length

/**
* Splitting dot-separated string to the list of tokens:
* a.b.c -> [a, b, c]; a."b.c".d -> [a, "b.c", d];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public value class TomlParser(private val config: TomlConfig) {
}
}
}
tomlFileHead.prettyPrint()
return tomlFileHead
}

Expand All @@ -119,14 +118,11 @@ public value class TomlParser(private val config: TomlConfig) {
return this
}

private fun String.isArrayOfTables(): Boolean {
val trimmed = this.trim()
return trimmed.startsWith("[[") && trimmed.endsWith("]]")
}
private fun String.isArrayOfTables(): Boolean = this.trim().startsWith("[[")

private fun String.isTableNode(): Boolean {
val trimmed = this.trim()
return trimmed.startsWith("[") && trimmed.endsWith("]")
return trimmed.startsWith("[")
}

private fun String.isComment() = this.trim().startsWith("#")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ package com.akuleshov7.ktoml.tree

import com.akuleshov7.ktoml.TomlConfig
import com.akuleshov7.ktoml.exceptions.ParseException
import com.akuleshov7.ktoml.parsers.findBeginningOfTheComment
import com.akuleshov7.ktoml.parsers.splitKeyToTokens
import com.akuleshov7.ktoml.parsers.trimDoubleBrackets
import com.akuleshov7.ktoml.parsers.trimQuotes

/**
* @property isSynthetic
* Class representing array of tables
*
* @throws ParseException if the content is wrong
*/
// FixMe: this class is mostly identical to the TomlTable - we should unify them together
public class TomlArrayOfTables(
Expand All @@ -37,8 +40,18 @@ public class TomlArrayOfTables(
public override lateinit var fullTableName: String

init {
val lastIndexOfBrace = content.lastIndexOf("]]")
if (lastIndexOfBrace == -1) {
throw ParseException("Invalid Array of Tables provided: $content." +
" It has missing closing brackets: ']]'", lineNo)
}

// finding the index of the beginning of the comment (if any)
val firstHash = content.findBeginningOfTheComment(lastIndexOfBrace)

// getting the content inside brackets ([a.b] -> a.b)
val sectionFromContent = content.trim().trimDoubleBrackets().trim()
val sectionFromContent = content.substring(0, firstHash).trim().trimDoubleBrackets()
.trim()

if (sectionFromContent.isBlank()) {
throw ParseException("Incorrect blank name for array of tables: $content", lineNo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public class TomlInlineTable(

}
}

tomlTable.prettyPrint()
return tomlTable
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.akuleshov7.ktoml.tree

import com.akuleshov7.ktoml.TomlConfig
import com.akuleshov7.ktoml.exceptions.ParseException
import com.akuleshov7.ktoml.parsers.findBeginningOfTheComment

/**
* Interface that contains all common methods that are used in KeyValue nodes
Expand Down Expand Up @@ -69,7 +70,7 @@ public fun String.splitKeyValue(lineNo: Int, config: TomlConfig = TomlConfig()):

// finding the index of a commented part of the string
// search starts goes from the closingQuoteIndex to the end of the string
val firstHash = (closingQuoteIndex until this.length).filter { this[it] == '#' }.minOrNull() ?: this.length
val firstHash = this.findBeginningOfTheComment(closingQuoteIndex)

// searching for an equals sign that should be placed main part of the string (not in the comment)
val firstEqualsSign = this.substring(0, firstHash).indexOfFirst { it == '=' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.akuleshov7.ktoml.tree

import com.akuleshov7.ktoml.TomlConfig
import com.akuleshov7.ktoml.exceptions.ParseException
import com.akuleshov7.ktoml.parsers.findBeginningOfTheComment
import com.akuleshov7.ktoml.parsers.splitKeyToTokens
import com.akuleshov7.ktoml.parsers.trimBrackets
import com.akuleshov7.ktoml.parsers.trimQuotes
Expand Down Expand Up @@ -39,8 +40,18 @@ public class TomlTablePrimitive(
public override lateinit var fullTableName: String

init {
val lastIndexOfBrace = content.lastIndexOf("]")
if (lastIndexOfBrace == -1) {
throw ParseException("Invalid Tables provided: $content." +
" It has missing closing bracket: ']'", lineNo)
}

// finding the index of the beginning of the comment (if any)
val firstHash = content.findBeginningOfTheComment(lastIndexOfBrace)

// getting the content inside brackets ([a.b] -> a.b)
val sectionFromContent = content.trim().trimBrackets().trim()
val sectionFromContent = content.substring(0, firstHash).trim().trimBrackets()
.trim()

if (sectionFromContent.isBlank()) {
throw ParseException("Incorrect blank table name: $content", lineNo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,27 @@ class ReadMeExampleTest {
fun readmeExampleTest() {
val test =
"""
|someBooleanProperty = true
|# inline tables in gradle 'libs.versions.toml' notation
|gradle-libs-like-property = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
|[table1]
|property1 = null
|property2 = 6
|
|[table2]
|someNumber = 5
| [table2."akuleshov7.com"]
| name = 'this is a "literal" string'
| # empty lists are also supported
| configurationList = ["a", "b", "c", null]
|
|# such redeclaration of table2
|# is prohibited in toml specification;
|# but ktoml is allowing it in non-strict mode:
|[table2]
|otherNumber = 5.56
|
someBooleanProperty = true
# inline tables in gradle 'libs.versions.toml' notation
gradle-libs-like-property = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }

[table1]
property1 = null # null is prohibited by the TOML spec,
property2 = 6

[table2]
someNumber = 5
[table2."akuleshov7.com"]
name = 'this is a "literal" string'
# empty lists are also supported
configurationList = ["a", "b", "c", null]

# such redeclaration of table2
# is prohibited in toml specification;
# but ktoml is allowing it in non-strict mode:
[table2]
otherNumber = 5.56

""".trimMargin()

val decoded = Toml.decodeFromString<MyClass>(test)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.akuleshov7.ktoml.parsers

import com.akuleshov7.ktoml.Toml
import kotlin.test.Test

class CommentsParsing {
@Test
fun commentsParsing() {
val string = """
# comment 1
[a] # comment 2
# comment 3
test = 1 # comment 4
[[a.b]] # comment 5
test = 1
""".trimIndent()
val parsedToml = Toml.tomlParser.parseString(string)
parsedToml.prettyPrint()
}
}