diff --git a/bson/src/commonMain/kotlin/BsonWriter.kt b/bson/src/commonMain/kotlin/BsonWriter.kt index 06848801..28ebafb8 100644 --- a/bson/src/commonMain/kotlin/BsonWriter.kt +++ b/bson/src/commonMain/kotlin/BsonWriter.kt @@ -19,7 +19,6 @@ package opensavvy.ktmongo.bson import opensavvy.ktmongo.bson.types.Decimal128 import opensavvy.ktmongo.bson.types.ObjectId import opensavvy.ktmongo.dsl.LowLevelApi -import kotlin.reflect.KClass @DslMarker annotation class BsonWriterDsl @@ -72,7 +71,7 @@ interface BsonValueWriter { * * All nested values are escaped as necessary such that the result is a completely inert BSON document. */ - @LowLevelApi fun writeObjectSafe(obj: T, context: BsonContext, type: KClass) + @LowLevelApi fun writeObjectSafe(obj: T, context: BsonContext) } /** @@ -124,7 +123,7 @@ interface BsonFieldWriter { * * All nested values are escaped as necessary such that the result is a completely inert BSON document. */ - @LowLevelApi fun writeObjectSafe(name: String, obj: T, context: BsonContext, type: KClass) + @LowLevelApi fun writeObjectSafe(name: String, obj: T, context: BsonContext) } /** @@ -165,25 +164,3 @@ interface BsonFieldWriter { */ @LowLevelApi expect fun buildBsonDocument(block: BsonFieldWriter.() -> Unit): Bson - -// region Extensions - -@LowLevelApi -inline fun BsonValueWriter.writeObjectSafe(obj: T, context: BsonContext) = - writeObjectSafe(obj, context, T::class) - -@Suppress("UNUSED_PARAMETER") -@LowLevelApi -fun BsonValueWriter.writeObjectSafe(obj: Nothing?, context: BsonContext) = - writeNull() - -@LowLevelApi -inline fun BsonFieldWriter.writeObjectSafe(name: String, obj: T, context: BsonContext) = - writeObjectSafe(name, obj, context, T::class) - -@Suppress("UNUSED_PARAMETER") -@LowLevelApi -fun BsonFieldWriter.writeObjectSafe(name: String, obj: Nothing?, context: BsonContext) = - writeNull(name) - -// endregion diff --git a/bson/src/commonMain/kotlin/types/TypedValue.kt b/bson/src/commonMain/kotlin/types/TypedValue.kt deleted file mode 100644 index 3b1bf78c..00000000 --- a/bson/src/commonMain/kotlin/types/TypedValue.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2024, OpenSavvy and contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package opensavvy.ktmongo.bson.types - -import kotlin.reflect.KClass - -/** - * A holder class for a [value] and its [type]. - * - * To get an instance of this type, see [typed]. - */ -sealed class TypedValue { - - abstract val value: T - - /** - * The type of [value]. - * - * If [value] is `null`, this field is `null` too. - */ - abstract val type: KClass? - - class NonNullable( - override val value: T, - override val type: KClass - ) : TypedValue() { - - override fun toString() = "$value of $type" - } - - object Null : TypedValue() { - override val value: Nothing? get() = null - override val type: Nothing? get() = null - - override fun toString() = "null" - } -} - -fun typed(value: T): TypedValue.NonNullable = - TypedValue.NonNullable(value, value::class) - -fun typed(@Suppress("UNUSED_PARAMETER") value: Nothing? = null): TypedValue.Null = - TypedValue.Null - -fun typed(value: T) = - if (value == null) typed() - else typed(value) diff --git a/bson/src/jvmMain/kotlin/BsonWriter.jvm.kt b/bson/src/jvmMain/kotlin/BsonWriter.jvm.kt index bf0fc941..d25313be 100644 --- a/bson/src/jvmMain/kotlin/BsonWriter.jvm.kt +++ b/bson/src/jvmMain/kotlin/BsonWriter.jvm.kt @@ -22,7 +22,6 @@ import opensavvy.ktmongo.dsl.LowLevelApi import org.bson.* import org.bson.codecs.Encoder import org.bson.codecs.EncoderContext -import kotlin.reflect.KClass @OptIn(LowLevelApi::class) private class JavaBsonWriter( @@ -104,9 +103,9 @@ private class JavaBsonWriter( writer.writeEndArray() } - override fun writeObjectSafe(name: String, obj: T, context: BsonContext, type: KClass) { + override fun writeObjectSafe(name: String, obj: T, context: BsonContext) { writer.writeName(name) - writeObjectSafe(obj, context, type) + writeObjectSafe(obj, context) } @Deprecated(DEPRECATED_IN_BSON_SPEC) @@ -193,11 +192,12 @@ private class JavaBsonWriter( writer.writeEndArray() } - override fun writeObjectSafe(obj: T, context: BsonContext, type: KClass) { + override fun writeObjectSafe(obj: T, context: BsonContext) { if (obj == null) { writer.writeNull() } else { - val codec: Encoder = context.codecRegistry.get(type.java) + @Suppress("UNCHECKED_CAST", "UNNECESSARY_NOT_NULL_ASSERTION") + val codec = context.codecRegistry.get(obj!!::class.java) as Encoder codec.encode( writer, obj,