diff --git a/bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt b/bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt index 7a064d2d..bdff1eae 100644 --- a/bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt +++ b/bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt @@ -204,6 +204,9 @@ class BsonFactory( override fun readDocument(bytes: ByteArray): BsonDocument = BsonDocument(this, Bytes(bytes.copyOf())) + override fun readDocument(document: opensavvy.ktmongo.bson.BsonDocument): BsonDocument = + super.readDocument(document) as BsonDocument + @LowLevelApi override fun buildArray(block: BsonValueWriter.() -> Unit): BsonArray = buildArbitraryTopLevel { @@ -226,6 +229,12 @@ class BsonFactory( override fun readArray(bytes: ByteArray): BsonArray = BsonArray(this, Bytes(bytes.copyOf())) + override fun readArray(array: opensavvy.ktmongo.bson.BsonArray): BsonArray = + super.readArray(array) as BsonArray + + override fun readValue(value: opensavvy.ktmongo.bson.BsonValue): BsonValue = + super.readValue(value) as BsonValue + @LowLevelApi @DangerousMongoApi internal interface TopCompletableBsonFieldWriter : CompletableBsonFieldWriter { diff --git a/bson-official/src/commonMain/kotlin/BsonFactory.kt b/bson-official/src/commonMain/kotlin/BsonFactory.kt index e5b58374..65ab1bb9 100644 --- a/bson-official/src/commonMain/kotlin/BsonFactory.kt +++ b/bson-official/src/commonMain/kotlin/BsonFactory.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, OpenSavvy and contributors. + * Copyright (c) 2025-2026, 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. @@ -38,6 +38,9 @@ expect class BsonFactory : BsonFactory { @LowLevelApi override fun readDocument(bytes: ByteArray): BsonDocument + @LowLevelApi + override fun readDocument(document: opensavvy.ktmongo.bson.BsonDocument): BsonDocument + @LowLevelApi override fun buildArray(block: BsonValueWriter.() -> Unit): BsonArray @@ -46,4 +49,10 @@ expect class BsonFactory : BsonFactory { @LowLevelApi override fun readArray(bytes: ByteArray): BsonArray + + @LowLevelApi + override fun readArray(array: opensavvy.ktmongo.bson.BsonArray): BsonArray + + @LowLevelApi + override fun readValue(value: opensavvy.ktmongo.bson.BsonValue): BsonValue } diff --git a/bson-official/src/jvmMain/kotlin/BsonFactory.jvm.kt b/bson-official/src/jvmMain/kotlin/BsonFactory.jvm.kt index e1d2f682..3154e56f 100644 --- a/bson-official/src/jvmMain/kotlin/BsonFactory.jvm.kt +++ b/bson-official/src/jvmMain/kotlin/BsonFactory.jvm.kt @@ -178,6 +178,10 @@ actual class BsonFactory( return readDocument(document) } + @LowLevelApi + actual override fun readDocument(document: opensavvy.ktmongo.bson.BsonDocument): BsonDocument = + super.readDocument(document) as BsonDocument + @LowLevelApi actual override fun buildArray(block: BsonValueWriter.() -> Unit): BsonArray { val nativeArray = org.bson.BsonArray() @@ -208,12 +212,20 @@ actual class BsonFactory( fun readArray(raw: org.bson.BsonArray): BsonArray = BsonArray(raw, this) + @LowLevelApi + actual override fun readArray(array: opensavvy.ktmongo.bson.BsonArray): BsonArray = + super.readArray(array) as BsonArray + /** * Wraps a [org.bson.BsonValue] from the official MongoDB driver into its KtMongo equivalent. */ fun readValue(raw: org.bson.BsonValue): BsonValue = BsonValue(raw, this) + @LowLevelApi + actual override fun readValue(value: opensavvy.ktmongo.bson.BsonValue): BsonValue = + super.readValue(value) as BsonValue + /** * Returns the instance of [Codec] (from the official MongoDB driver) * that is used to encode or decode the given [type]. diff --git a/bson/src/commonMain/kotlin/BsonFactory.kt b/bson/src/commonMain/kotlin/BsonFactory.kt index dfbd1c18..a8c6902f 100644 --- a/bson/src/commonMain/kotlin/BsonFactory.kt +++ b/bson/src/commonMain/kotlin/BsonFactory.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024-2025, OpenSavvy and contributors. + * Copyright (c) 2024-2026, 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. @@ -16,6 +16,7 @@ package opensavvy.ktmongo.bson +import opensavvy.ktmongo.dsl.DangerousMongoApi import opensavvy.ktmongo.dsl.LowLevelApi import kotlin.reflect.KType import kotlin.reflect.typeOf @@ -159,6 +160,27 @@ interface BsonFactory { @LowLevelApi fun readDocument(bytes: ByteArray): BsonDocument + /** + * Returns a [BsonDocument] that is tied to this factory, that represents the same data as [document]. + * + * This method is useful when you need to convert from a different [BsonDocument] implementation to the + * one returned by this factory. For example, if you have a [BsonDocument] from the Multiplatform driver and you + * want to convert it to one from the official driver. + * + * The returned document may be referentially identical to [document] if [document] was already produced by this factory. + * + * The returned document may share memory with [document]. + */ + @OptIn(LowLevelApi::class, DangerousMongoApi::class) + fun readDocument(document: BsonDocument): BsonDocument = + buildDocument { + for ((name, value) in document) { + write(name) { + pipe(value) + } + } + } + /** * Instantiates a new [BSON array][BsonArray]. * @@ -205,6 +227,42 @@ interface BsonFactory { @LowLevelApi fun readArray(bytes: ByteArray): BsonArray + /** + * Returns a [BsonArray] that is tied to this factory, that represents the same data as [array]. + * + * This method is useful when you need to convert from a different [BsonArray] implementation to the + * one returned by this factory. For example, if you have a [BsonArray] from the Multiplatform driver and you + * want to convert it to one from the official driver. + * + * The returned array may be referentially identical to [array] if [array] was already produced by this factory. + * + * The returned array may share memory with [array]. + */ + @OptIn(LowLevelApi::class, DangerousMongoApi::class) + fun readArray(array: BsonArray): BsonArray = + buildArray { + for (value in array) { + pipe(value) + } + } + + /** + * Returns a [BsonValue] that is tied to this factory, that represents the same data as [value]. + * + * This method is useful when you need to convert from a different [BsonValue] implementation to the + * one returned by this factory. For example, if you have a [BsonValue] from the Multiplatform driver and you + * want to convert it to one from the official driver. + * + * The returned value may be referentially identical to [value] if [value] was already produced by this factory. + * + * The returned value may share memory with [value]. + */ + @OptIn(DangerousMongoApi::class, LowLevelApi::class) + fun readValue(value: BsonValue): BsonValue = + buildArray { + pipe(value) + }[0]!! + } /**