Skip to content

Commit 3c99409

Browse files
committed
sort as int if key is a number when decoding maps to lists as although rtdb show numeric string keys in numeric order they are returned by the sdk in lexicographically
1 parent 6210ab6 commit 3c99409

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

firebase-common/src/androidMain/kotlin/dev/gitlive/firebase/_decoders.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ actual fun FirebaseDecoder.structureDecoder(descriptor: SerialDescriptor): Compo
2020
}
2121
}
2222
}
23-
StructureKind.LIST -> ((value as? Map<*, *>)?.values?.toList() ?: value as List<*>).let {
24-
FirebaseCompositeDecoder(it.size) { _, index -> it[index] }
25-
}
23+
StructureKind.LIST ->
24+
when(value) {
25+
is List<*> -> value
26+
is Map<*, *> -> value.asSequence()
27+
.sortedBy { (it) -> it.toString().toIntOrNull() }
28+
.map { (_, it) -> it }
29+
.toList()
30+
else -> error("unexpected type, got $value when expecting a list")
31+
}
32+
.let { FirebaseCompositeDecoder(it.size) { _, index -> it[index] } }
2633
StructureKind.MAP -> (value as Map<*, *>).entries.toList().let {
2734
FirebaseCompositeDecoder(it.size) { _, index -> it[index/2].run { if(index % 2 == 0) key else value } }
2835
}

firebase-common/src/iosMain/kotlin/dev/gitlive/firebase/_decoders.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ actual fun FirebaseDecoder.structureDecoder(descriptor: SerialDescriptor): Compo
2020
}
2121
}
2222
}
23-
StructureKind.LIST -> ((value as? Map<*, *>)?.values?.toList() ?: value as List<*>).let {
24-
FirebaseCompositeDecoder(it.size) { _, index -> it[index] }
25-
}
23+
StructureKind.LIST ->
24+
when(value) {
25+
is List<*> -> value
26+
is Map<*, *> -> value.asSequence()
27+
.sortedBy { (it) -> it.toString().toIntOrNull() }
28+
.map { (_, it) -> it }
29+
.toList()
30+
else -> error("unexpected type, got $value when expecting a list")
31+
}
32+
.let { FirebaseCompositeDecoder(it.size) { _, index -> it[index] } }
2633
StructureKind.MAP -> (value as Map<*, *>).entries.toList().let {
2734
FirebaseCompositeDecoder(it.size) { _, index -> it[index/2].run { if(index % 2 == 0) key else value } }
2835
}

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/_decoders.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ actual fun FirebaseDecoder.structureDecoder(descriptor: SerialDescriptor): Compo
2222
}
2323
}
2424
}
25-
StructureKind.LIST -> (js("Object").values(value) as Array<*>).let {
26-
FirebaseCompositeDecoder(it.size) { _, index -> it[index] }
27-
}
25+
StructureKind.LIST ->
26+
when(value) {
27+
is Array<*> -> value
28+
else -> (js("Object").entries(value) as Array<Array<Any>>)
29+
.asSequence()
30+
.sortedBy { (it) -> it.toString().toIntOrNull() }
31+
.map { (_, it) -> it }
32+
.toList()
33+
}
34+
.let { FirebaseCompositeDecoder(it.size) { _, index -> it[index] } }
2835
StructureKind.MAP -> (js("Object").entries(value) as Array<Array<Any>>).let {
2936
FirebaseCompositeDecoder(it.size) { _, index -> it[index/2].run { if(index % 2 == 0) get(0) else get(1) } }
3037
}

0 commit comments

Comments
 (0)