diff --git a/driver-multiplatform-wire/src/commonTest/kotlin/OpMsgUtils.kt b/driver-multiplatform-wire/src/commonTest/kotlin/OpMsgUtils.kt new file mode 100644 index 00000000..6c6d7936 --- /dev/null +++ b/driver-multiplatform-wire/src/commonTest/kotlin/OpMsgUtils.kt @@ -0,0 +1,51 @@ +/* + * Copyright (c) 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. + * 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.multiplatform.wire + +import opensavvy.ktmongo.bson.BsonFieldWriter +import opensavvy.ktmongo.bson.multiplatform.BsonFactory +import opensavvy.ktmongo.dsl.LowLevelApi +import opensavvy.ktmongo.multiplatform.wire.Message.OpMsg + +/** + * Creates an [OpMsg] message with the given [body] and no [OpMsg.sequences]. + */ +@OptIn(LowLevelApi::class) +fun OpMsg(body: BsonFieldWriter.() -> Unit): OpMsg = + OpMsg( + MessageSection.Body( + eager( + BsonFactory().buildDocument(body) + ) + ), + sequences = emptySequence() + ) + +/** + * Creates a copy of this [OpMsg], concatenating a new sequence named [id] and composed of the given [documents]. + */ +@OptIn(LowLevelApi::class) +fun OpMsg.withSequence( + id: String, + vararg documents: BsonFieldWriter.() -> Unit, +): OpMsg = OpMsg( + body, + sequences + MessageSection.DocumentSequence( + id, + documents.map { eager(BsonFactory().buildDocument(it)) } + ) +) diff --git a/driver-multiplatform-wire/src/commonTest/kotlin/fake/FakeServerTest.kt b/driver-multiplatform-wire/src/commonTest/kotlin/fake/FakeServerTest.kt index b53820e9..4fb05486 100644 --- a/driver-multiplatform-wire/src/commonTest/kotlin/fake/FakeServerTest.kt +++ b/driver-multiplatform-wire/src/commonTest/kotlin/fake/FakeServerTest.kt @@ -18,11 +18,9 @@ package opensavvy.ktmongo.multiplatform.wire.fake -import opensavvy.ktmongo.bson.multiplatform.BsonFactory import opensavvy.ktmongo.dsl.LowLevelApi import opensavvy.ktmongo.multiplatform.wire.Message -import opensavvy.ktmongo.multiplatform.wire.MessageSection -import opensavvy.ktmongo.multiplatform.wire.eager +import opensavvy.ktmongo.multiplatform.wire.OpMsg import opensavvy.ktmongo.multiplatform.wire.fake.FakeServer.Companion.fakeServer import opensavvy.prepared.runner.testballoon.preparedSuite @@ -40,25 +38,13 @@ val FakeServerTest by preparedSuite { } test("Round-trip hello") { - val helloMessage = Message.OpMsg( - MessageSection.Body( - eager( - BsonFactory().buildDocument { - writeInt32("hello", 1) - } - ) - ) - ) + val helloMessage = OpMsg { + writeInt32("hello", 1) + } - val okMessage = Message.OpMsg( - MessageSection.Body( - eager( - BsonFactory().buildDocument { - writeDouble("ok", 1.0) - } - ) - ) - ) + val okMessage = OpMsg { + writeDouble("ok", 1.0) + } val server = fakeServer { expect(helloMessage)