From 832b763ea8c9cf7c6c8fa3bb25c5fd84834745a0 Mon Sep 17 00:00:00 2001 From: Ivan “CLOVIS” Canet Date: Mon, 18 May 2026 21:21:54 +0000 Subject: [PATCH] feat(bson): Allow re-encoding a BsonXXX type into another factory --- bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt | 9 +++++++++ bson-official/src/commonMain/kotlin/BsonFactory.kt | 11 ++++++++++- bson-official/src/jvmMain/kotlin/BsonFactory.jvm.kt | 12 ++++++++++++ bson/src/commonMain/kotlin/BsonFactory.kt | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 4 file(s) changed, 90 insertion(s)(+), 2 deletion(s)(-) diff --git a/bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt b/bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt --- a/bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt +++ b/bson-multiplatform/src/commonMain/kotlin/BsonFactory.kt @@ -204,6 +204,9 @@ 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 { @@ -225,6 +228,12 @@ @LowLevelApi 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 diff --git a/bson-official/src/commonMain/kotlin/BsonFactory.kt b/bson-official/src/commonMain/kotlin/BsonFactory.kt --- 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. @@ -39,6 +39,9 @@ override fun readDocument(bytes: ByteArray): BsonDocument @LowLevelApi + override fun readDocument(document: opensavvy.ktmongo.bson.BsonDocument): BsonDocument + + @LowLevelApi override fun buildArray(block: BsonValueWriter.() -> Unit): BsonArray @LowLevelApi @@ -46,4 +49,10 @@ @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 --- a/bson-official/src/jvmMain/kotlin/BsonFactory.jvm.kt +++ b/bson-official/src/jvmMain/kotlin/BsonFactory.jvm.kt @@ -179,6 +179,10 @@ } @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,11 +212,19 @@ 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) diff --git a/bson/src/commonMain/kotlin/BsonFactory.kt b/bson/src/commonMain/kotlin/BsonFactory.kt --- 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 @@ -160,6 +161,27 @@ 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]. * * ### Example @@ -204,6 +226,42 @@ */ @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]!! } -- tangled.sh