-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for nested and inlined maps (inside other maps) (#252)
### What's done: - Added a support for nested anonymous Maps
- Loading branch information
Showing
6 changed files
with
227 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/NestedMapTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.akuleshov7.ktoml.decoders | ||
|
||
import com.akuleshov7.ktoml.Toml | ||
import com.akuleshov7.ktoml.exceptions.IllegalTypeException | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.decodeFromString | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class NestedMapTest { | ||
@Serializable | ||
data class NestedTable( | ||
val outer: Map<String, Map<String, Long>> | ||
) | ||
|
||
@Serializable | ||
data class SimpleNestedTable( | ||
val map: Map<String, Long>, | ||
) | ||
|
||
@ExperimentalSerializationApi | ||
@Test | ||
fun nestedInvalidMapping() { | ||
val data = """ | ||
[map] | ||
[map.a] | ||
b = 1 | ||
""".trimIndent() | ||
|
||
assertFailsWith<IllegalTypeException> { | ||
Toml.decodeFromString<SimpleNestedTable>(data) | ||
} | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
@Test | ||
fun testSimpleNestedMaps() { | ||
val data = """ | ||
[outer] | ||
[outer.inner1] | ||
a = 5 | ||
b = 5 | ||
""".trimIndent() | ||
|
||
val result = Toml.decodeFromString<NestedTable>(data) | ||
assertEquals(NestedTable(outer = mapOf("inner1" to mapOf("a" to 5, "b" to 5))), result) | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
@Test | ||
fun testNestedMaps() { | ||
val data = """ | ||
[outer] | ||
[outer.inner1] | ||
a = 5 | ||
b = 5 | ||
[outer.inner2] | ||
c = 7 | ||
d = 12 | ||
""".trimIndent() | ||
|
||
val result = Toml.decodeFromString<NestedTable>(data) | ||
assertEquals(NestedTable(outer = mapOf("inner1" to mapOf("a" to 5, "b" to 5), "inner2" to mapOf("c" to 7, "d" to 12))), result) | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
@Test | ||
fun nestedMapFromReadme() { | ||
@Serializable | ||
data class MyClass( | ||
val a: Map<String, Map<String, String>> | ||
) | ||
|
||
val data = """ | ||
[a] | ||
b = 42 | ||
c = "String" | ||
[a.innerTable] | ||
d = 5 | ||
[a.otherInnerTable] | ||
d = "String" | ||
""".trimIndent() | ||
|
||
println(Toml.decodeFromString<MyClass>(data)) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/NestedTableTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.akuleshov7.ktoml.decoders | ||
|
||
import com.akuleshov7.ktoml.Toml | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.decodeFromString | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class NestedTableTest { | ||
@Serializable | ||
data class NestedTable( | ||
val outer: Outer | ||
) | ||
|
||
@Serializable | ||
data class Outer( | ||
val inner: Inner | ||
) | ||
|
||
@Serializable | ||
data class Inner( | ||
val a: Int | ||
) | ||
|
||
@ExperimentalSerializationApi | ||
@Test | ||
fun testDottedKeys() { | ||
val data = """ | ||
[outer] | ||
[outer.inner] | ||
a = 5 | ||
""".trimIndent() | ||
|
||
val result = Toml.decodeFromString<NestedTable>(data) | ||
assertEquals(NestedTable(Outer(Inner(a = 5))), result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters