Skip to content

Commit 673d33a

Browse files
2tsumo-hitorimp911de
authored andcommitted
Add Kotlin extension to accept Kotlin's Pair in BulkOperations.
Closes #4911 Original pull request: #4912 Signed-off-by: audghks1996 <[email protected]>
1 parent 5cb0fa8 commit 673d33a

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2017-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core
17+
18+
import org.springframework.data.mongodb.core.query.Query
19+
import org.springframework.data.mongodb.core.query.Update
20+
import org.springframework.data.mongodb.core.query.UpdateDefinition
21+
import org.springframework.data.util.Pair.of
22+
23+
/**
24+
* Extension for [BulkOperations.updateMulti] that converts a [kotlin.Pair] to [org.springframework.data.util.Pair].
25+
*
26+
* @author 2tsumo-hitori
27+
*/
28+
fun BulkOperations.updateMulti(kotlinPairs: List<Pair<Query, UpdateDefinition>>): BulkOperations =
29+
updateMulti(kotlinPairs.toSpringPairs())
30+
31+
/**
32+
* Extension for [BulkOperations.upsert] that converts a [kotlin.Pair] to [org.springframework.data.util.Pair].
33+
*
34+
* @author 2tsumo-hitori
35+
*/
36+
fun BulkOperations.upsert(kotlinPairs: List<Pair<Query, Update>>) : BulkOperations =
37+
upsert(kotlinPairs.toSpringPairs())
38+
39+
/**
40+
* Extension for [BulkOperations.updateOne] that converts a [kotlin.Pair] to [org.springframework.data.util.Pair].
41+
*
42+
* @author 2tsumo-hitori
43+
*/
44+
fun BulkOperations.updateOne(kotlinPairs: List<Pair<Query, UpdateDefinition>>): BulkOperations =
45+
updateOne(kotlinPairs.toSpringPairs())
46+
47+
private fun <A : Query, B : UpdateDefinition> List<Pair<A, B>>.toSpringPairs(): List<org.springframework.data.util.Pair<A, B>> {
48+
49+
return map { (first, second) -> of(first, second) }
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2017-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core
17+
18+
import io.mockk.mockk
19+
import io.mockk.verify
20+
import org.junit.Test
21+
import org.springframework.data.mongodb.core.query.Criteria
22+
import org.springframework.data.mongodb.core.query.Query
23+
import org.springframework.data.mongodb.core.query.Update
24+
import org.springframework.data.mongodb.core.query.UpdateDefinition
25+
import org.springframework.data.util.Pair.of
26+
27+
/**
28+
* @author 2tsumo-hitori
29+
*/
30+
class BulkOperationExtensionsTests {
31+
32+
private val bulkOperation = mockk<BulkOperations>(relaxed = true)
33+
34+
@Test // GH-4911
35+
fun `BulkOperation#updateMulti#updates() using kotlin#Pair should call its Java counterpart`() {
36+
val list : MutableList<Pair<Query, UpdateDefinition>> = mutableListOf()
37+
list.add(where("value", "v2") to set("value", "v3"))
38+
39+
bulkOperation.updateMulti(list)
40+
41+
val expected = list.map { (query, update) -> of(query, update) }
42+
verify { bulkOperation.updateMulti(expected) }
43+
}
44+
45+
@Test // GH-4911
46+
fun `BulkOperation#upsert#updates() using kotlin#Pair should call its Java counterpart`() {
47+
val list : MutableList<Pair<Query, Update>> = mutableListOf()
48+
list.add(where("value", "v2") to set("value", "v3"))
49+
50+
bulkOperation.upsert(list)
51+
52+
val expected = list.map { (query, update) -> of(query, update) }
53+
verify { bulkOperation.upsert(expected) }
54+
}
55+
56+
@Test // GH-4911
57+
fun `BulkOperation#updateOne#updates() using kotlin#Pair should call its Java counterpart`() {
58+
val list : MutableList<Pair<Query, UpdateDefinition>> = mutableListOf()
59+
list.add(where("value", "v2") to set("value", "v3"))
60+
61+
bulkOperation.updateOne(list)
62+
63+
val expected = list.map { (query, update) -> of(query, update) }
64+
verify { bulkOperation.updateOne(expected) }
65+
}
66+
67+
private fun where(field: String, value: String): Query {
68+
return Query().addCriteria(Criteria.where(field).`is`(value))
69+
}
70+
71+
private fun set(field: String, value: String): Update {
72+
return Update().set(field, value)
73+
}
74+
}

0 commit comments

Comments
 (0)