Skip to content

Commit

Permalink
Alllow shared prefix names (#1183)
Browse files Browse the repository at this point in the history
Allow names with a prefix that equals another name in the same class.

Fixes #1171

Co-authored-by: RM_TR <[email protected]>
  • Loading branch information
TorRanfelt and RM_TR authored Nov 5, 2020
1 parent f08c62f commit 31f646a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public sealed class Properties(
final override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
while (currentIndex < size) {
val name = descriptor.getTag(currentIndex++)
if (map.keys.any { it.startsWith(name) }) return currentIndex - 1
if (map.keys.any {
it.startsWith(name) && (it.length == name.length || it[name.length] == '.')
}) return currentIndex - 1
if (isCollection) {
// if map does not contain key we look for, then indices in collection have ended
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class PropertiesTest {
@Serializable
data class NullableEnumData(val data0: TestEnum?, val data1: TestEnum?)

@Serializable
data class SharedPrefixNames(
val first: String = "100",
val firstSecond: String = "100"
)

enum class TestEnum { ZERO, ONE }

private inline fun <reified T : Any> assertMappedAndRestored(
Expand Down Expand Up @@ -211,4 +217,11 @@ class PropertiesTest {
fun testCanReadSizeProperty() {
assertMappedAndRestored(mapOf("p" to "a", "size" to "b"), TestWithSize("a", "b"), TestWithSize.serializer())
}

@Test
fun testSharedPrefixNames() {
val map: Map<String, Any> = mapOf("firstSecond" to "42")
val restored = Properties.decodeFromMap(SharedPrefixNames.serializer(), map)
assertEquals(SharedPrefixNames("100", "42"), restored)
}
}

0 comments on commit 31f646a

Please sign in to comment.