-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7b8458
commit da8ba51
Showing
9 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...federmappe/firestore/SharedEnumDecoder.kt → ...te/federmappe/common/SharedEnumDecoder.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
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
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
61 changes: 61 additions & 0 deletions
61
realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/ListDecoder.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,61 @@ | ||
package de.sipgate.federmappe.realtimedb | ||
|
||
import com.google.firebase.database.DataSnapshot | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.StructureKind | ||
import kotlinx.serialization.encoding.AbstractDecoder | ||
import kotlinx.serialization.encoding.CompositeDecoder | ||
import kotlinx.serialization.modules.EmptySerializersModule | ||
import kotlinx.serialization.modules.SerializersModule | ||
|
||
@ExperimentalSerializationApi | ||
class ListDecoder( | ||
private val list: ArrayDeque<DataSnapshot>, | ||
private val elementsCount: Int = 0, | ||
override val serializersModule: SerializersModule = EmptySerializersModule(), | ||
) : AbstractDecoder() { | ||
private var index = 0 | ||
|
||
override fun decodeSequentially(): Boolean = true | ||
|
||
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int = elementsCount | ||
|
||
override fun decodeValue(): Any = list.removeFirst().value!! | ||
|
||
override fun decodeElementIndex(descriptor: SerialDescriptor): Int = | ||
when (index) { | ||
elementsCount -> CompositeDecoder.DECODE_DONE | ||
else -> index++ | ||
} | ||
|
||
override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = | ||
decodeEnum(enumDescriptor, ::decodeValue) | ||
|
||
override fun decodeNotNullMark(): Boolean = | ||
when { | ||
list.firstOrNull()?.value != null -> true | ||
else -> false.also { list.removeFirst() } | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder { | ||
val value = list.removeFirst() | ||
|
||
when (descriptor.kind) { | ||
StructureKind.CLASS -> | ||
return SnapshotDecoder( | ||
dataSnapshot = value, | ||
ignoreUnknownProperties = true, | ||
serializersModule = this.serializersModule, | ||
) | ||
StructureKind.LIST -> { | ||
val subList = (value as Iterable<DataSnapshot>).toCollection(mutableListOf()) | ||
return ListDecoder(ArrayDeque(subList), subList.size, serializersModule) | ||
} | ||
else -> {} | ||
} | ||
|
||
return ListDecoder(list, descriptor.elementsCount) | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/MapDecoder.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,92 @@ | ||
package de.sipgate.federmappe.realtimedb | ||
|
||
import android.util.Log | ||
import com.google.firebase.database.DataSnapshot | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.StructureKind | ||
import kotlinx.serialization.encoding.AbstractDecoder | ||
import kotlinx.serialization.encoding.CompositeDecoder | ||
import kotlinx.serialization.modules.EmptySerializersModule | ||
import kotlinx.serialization.modules.SerializersModule | ||
|
||
@ExperimentalSerializationApi | ||
class MapDecoder( | ||
private val list: List<DataSnapshot?>, | ||
override val serializersModule: SerializersModule = EmptySerializersModule(), | ||
private val ignoreUnknownProperties: Boolean = false, | ||
) : AbstractDecoder() { | ||
private val keysIterator = list.iterator() | ||
private var index: Int = -2 | ||
|
||
private val skippedValues = mutableSetOf<String>() | ||
|
||
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int = list.size | ||
|
||
override fun decodeValue(): Any = list[index]!!.value!! | ||
|
||
override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = | ||
decodeEnum(enumDescriptor, ::decodeValue) | ||
|
||
override fun decodeNotNullMark(): Boolean = list[index] != null | ||
|
||
override fun decodeElementIndex(descriptor: SerialDescriptor): Int { | ||
while (keysIterator.hasNext()) { | ||
val nextKey = keysIterator.next() | ||
if (index >= 0) { | ||
if (index % 2 == 0) { | ||
index += 1 | ||
return index | ||
} | ||
} | ||
val nextIndex = | ||
if (descriptor.kind == StructureKind.MAP) { | ||
list.indexOf(nextKey) | ||
} else { | ||
descriptor.getElementIndex(nextKey!!.key!!) | ||
} | ||
if (nextIndex == CompositeDecoder.UNKNOWN_NAME) { | ||
Log.w("MapDecoder", "encountered unknown key while decoding") | ||
skippedValues.add(nextKey!!.key!!) | ||
continue | ||
} | ||
|
||
index = nextIndex | ||
return nextIndex | ||
} | ||
|
||
return CompositeDecoder.DECODE_DONE | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder { | ||
val value = | ||
if (index % 2 == 0) { | ||
list[index + 1] | ||
} else { | ||
list[index] | ||
}!! | ||
|
||
when (descriptor.kind) { | ||
StructureKind.CLASS -> { | ||
return SnapshotDecoder( | ||
dataSnapshot = value, | ||
ignoreUnknownProperties = ignoreUnknownProperties, | ||
serializersModule = serializersModule, | ||
) | ||
} | ||
StructureKind.MAP -> { | ||
return MapDecoder( | ||
list = value.children.map { it }, | ||
ignoreUnknownProperties = ignoreUnknownProperties, | ||
) | ||
} | ||
StructureKind.LIST -> { | ||
val list = value.children.map { it } | ||
return ListDecoder(ArrayDeque(list), list.size, serializersModule) | ||
} | ||
else -> throw SerializationException("Given value is neither a list nor a type $value") | ||
} | ||
} | ||
} |
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