Skip to content

Commit

Permalink
fix: Support section list
Browse files Browse the repository at this point in the history
  • Loading branch information
sya-ri committed Jan 23, 2024
1 parent 07d9978 commit daa737f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/kotlin/dev/s7a/ktconfig/internal/Content.kt
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,14 @@ internal sealed class Content<T>(val type: KType) {
path: String,
value: T?,
) = value?.mapIndexed { index, v ->
content.serialize("$path[$index]", v)
val serialized = content.serialize("$path[$index]", v)
if (serialized is Section) {
serialized.values?.mapValues {
it.value.value
}
} else {
serialized
}
}

class ListType<U : Any?>(type: KType, content: Content<U>, isMarkedNullable: Boolean) : IterableType<List<U>, U>(
Expand Down
47 changes: 47 additions & 0 deletions src/test/kotlin/NestingDeserializeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ class NestingDeserializeTest {
)
}

@Test
fun another_list() {
data class Data1(val data: @UseSerializer(StringSerializerForNesting::class) String)

data class Data2(val data1: List<Data1>)

assertEquals(
Data2(listOf(Data1("hello"))),
ktConfigString(
"""
data1:
data: h_e_l_l_o
""".trimIndent(),
),
)
assertEquals(
Data2(listOf(Data1("hello"))),
ktConfigString(
"""
data1:
- data: h_e_l_l_o
""".trimIndent(),
),
)
}

@Test
fun another_map() {
data class Data1(val data: @UseSerializer(StringSerializerForNesting::class) String)
Expand Down Expand Up @@ -58,4 +84,25 @@ class NestingDeserializeTest {
),
)
}

@Test
fun recursive_list() {
data class Data(val int: Int, val data: List<Data>?)

assertEquals(
Data(1, listOf(Data(21, listOf(Data(302, listOf(Data(4003, null))))))),
ktConfigString(
"""
int: 1
data:
- int: 21
data:
- int: 302
data:
- int: 4003
data: null
""".trimIndent(),
),
)
}
}
38 changes: 38 additions & 0 deletions src/test/kotlin/NestingSerializeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ class NestingSerializeTest {
)
}

@Test
fun another_list() {
data class Data1(val data: @UseSerializer(StringSerializerForNesting::class) String)

data class Data2(val data1: List<Data1>)

assertEquals(
"""
data1:
- data: h_e_l_l_o
""".trimIndent(),
saveKtConfigString(Data2(listOf(Data1("hello")))),
)
}

@Test
fun another_map() {
data class Data1(val data: @UseSerializer(StringSerializerForNesting::class) String)
Expand Down Expand Up @@ -57,4 +73,26 @@ class NestingSerializeTest {
),
)
}

@Test
fun recursive_list() {
data class Data(val int: Int, val data: List<Data>?)

assertEquals(
"""
data:
- data:
- data:
- data: null
int: 4003
int: 302
int: 21
int: 1
""".trimIndent(),
saveKtConfigString(
Data(1, listOf(Data(21, listOf(Data(302, listOf(Data(4003, null))))))),
),
)
}
}

0 comments on commit daa737f

Please sign in to comment.