Skip to content

Commit

Permalink
Add typedef for ErrorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Mar 15, 2024
1 parent 02fd07f commit bf31920
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package de.sipgate.federmappe.common

typealias ErrorHandler<T> = (Throwable) -> T?
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package de.sipgate.federmappe.firestore
import com.google.firebase.Timestamp
import com.google.firebase.firestore.DocumentSnapshot
import de.sipgate.federmappe.common.DefaultSerializersModule
import de.sipgate.federmappe.common.ErrorHandler
import de.sipgate.federmappe.common.toObjectWithSerializer
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.modules.SerializersModule

@ExperimentalSerializationApi
inline fun <reified T : Any> DocumentSnapshot.toObject(
customSerializers: SerializersModule = DefaultSerializersModule,
errorHandler: (Throwable) -> T? = { throw it }): T? =
try {
data?.toObjectWithSerializer<T>(
customSerializers = customSerializers,
subtypeDecoder = { (it as? Timestamp)?.let(::FirebaseTimestampDecoder) }
)
} catch (ex: Throwable) {
errorHandler(ex)
}
errorHandler: ErrorHandler<T> = { throw it }
): T? = try {
data?.toObjectWithSerializer<T>(
customSerializers = customSerializers,
subtypeDecoder = { (it as? Timestamp)?.let(::FirebaseTimestampDecoder) }
)
} catch (ex: Throwable) {
errorHandler(ex)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package de.sipgate.federmappe.firestore
import com.google.firebase.Timestamp
import com.google.firebase.firestore.QuerySnapshot
import de.sipgate.federmappe.common.DefaultSerializersModule
import de.sipgate.federmappe.common.ErrorHandler
import de.sipgate.federmappe.common.toObjectWithSerializer
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.modules.SerializersModule

@ExperimentalSerializationApi
inline fun <reified T : Any> QuerySnapshot.toObject(
customSerializers: SerializersModule = DefaultSerializersModule,
errorHandler: (Throwable) -> T? = { throw it }
): List<T?> =
map { documentSnapshot ->
try {
documentSnapshot.data.toObjectWithSerializer(
customSerializers = customSerializers,
subtypeDecoder = { (it as? Timestamp)?.let(::FirebaseTimestampDecoder) })
} catch (ex: Throwable) {
errorHandler(ex)
}
errorHandler: ErrorHandler<T> = { throw it }
): List<T?> = map { documentSnapshot ->
try {
documentSnapshot.data.toObjectWithSerializer(
customSerializers = customSerializers,
subtypeDecoder = { (it as? Timestamp)?.let(::FirebaseTimestampDecoder) })
} catch (ex: Throwable) {
errorHandler(ex)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.sipgate.federmappe.realtimedb

import com.google.firebase.database.DataSnapshot
import de.sipgate.federmappe.common.ErrorHandler
import de.sipgate.federmappe.common.StringMapToObjectDecoder
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
Expand All @@ -11,7 +12,7 @@ import kotlinx.serialization.serializer
@ExperimentalSerializationApi
inline fun <reified T : Any> DataSnapshot.toObject(
customSerializers: SerializersModule = EmptySerializersModule(),
crossinline errorHandler: (Throwable) -> T? = { throw it }
crossinline errorHandler: ErrorHandler<T> = { throw it }
): T? = try {
toObjectWithSerializer(customSerializers = customSerializers)
} catch (ex: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package de.sipgate.federmappe.realtimedb

import com.google.firebase.database.DatabaseReference
import de.sipgate.federmappe.common.ErrorHandler
import kotlinx.coroutines.tasks.await
import kotlinx.serialization.ExperimentalSerializationApi

@ExperimentalSerializationApi
suspend inline fun <reified T : Any> DatabaseReference.toObject(
crossinline errorHandler: (Throwable) -> T? = { throw it }
crossinline errorHandler: ErrorHandler<T> = { throw it }
): T? = get().await().toObject<T>(errorHandler = errorHandler)

@ExperimentalSerializationApi
suspend inline fun <reified T : Any> DatabaseReference.toObjects(
crossinline errorHandler: (Throwable) -> T? = { throw it }
crossinline errorHandler: ErrorHandler<T> = { throw it }
): List<T> = get().await().children.mapNotNull { childSnapshot ->
childSnapshot.toObject<T>(errorHandler = errorHandler)
}

0 comments on commit bf31920

Please sign in to comment.