Skip to content

Commit

Permalink
Expose ignoreUnknownProperties option to callers
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Mar 18, 2024
1 parent 68140c7 commit 1a269c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,29 @@ import kotlinx.serialization.serializer
@ExperimentalSerializationApi
inline fun <reified T : Any> DataSnapshot.toObject(
customSerializers: SerializersModule = EmptySerializersModule(),
ignoreUnknownProperties: Boolean = false,
crossinline errorHandler: ErrorHandler<T> = { throw it }
): T? = try {
toObjectWithSerializer(customSerializers = customSerializers)
toObjectWithSerializer(
ignoreUnknownProperties = ignoreUnknownProperties,
customSerializers = customSerializers
)
} catch (ex: Throwable) {
errorHandler(ex)
}

@ExperimentalSerializationApi
inline fun <reified T : Any> DataSnapshot.toObjectWithSerializer(
serializer: KSerializer<T> = serializer<T>(),
ignoreUnknownProperties: Boolean = false,
customSerializers: SerializersModule = EmptySerializersModule()
): T = serializer.deserialize(StringMapToObjectDecoder(toObjectMap().unwrapRoot(), customSerializers))
): T = serializer.deserialize(
StringMapToObjectDecoder(
data = toObjectMap().unwrapRoot(),
serializersModule = customSerializers,
ignoreUnknownProperties = ignoreUnknownProperties
)
)

@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
fun DataSnapshot.toObjectMap(): Pair<String, Any> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import kotlinx.serialization.ExperimentalSerializationApi

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

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

0 comments on commit 1a269c5

Please sign in to comment.