-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support authorizedCollections
option for listCollections
helpers
#1270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
d91efbb
c646b12
537e9ab
9a7dbe7
b1d8b57
0ac0567
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* 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 com.mongodb.kotlin.client.coroutine.syncadapter | ||
|
||
import com.mongodb.client.ListCollectionNamesIterable as JListCollectionNamesIterable | ||
import com.mongodb.kotlin.client.coroutine.ListCollectionNamesFlow | ||
import java.util.concurrent.TimeUnit | ||
import org.bson.BsonValue | ||
import org.bson.conversions.Bson | ||
|
||
data class SyncListCollectionNamesIterable(val wrapped: ListCollectionNamesFlow) : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is based on |
||
JListCollectionNamesIterable, SyncMongoIterable<String>(wrapped) { | ||
|
||
override fun batchSize(batchSize: Int): SyncListCollectionNamesIterable = apply { wrapped.batchSize(batchSize) } | ||
|
||
override fun maxTime(maxTime: Long, timeUnit: TimeUnit): SyncListCollectionNamesIterable = apply { | ||
wrapped.maxTime(maxTime, timeUnit) | ||
} | ||
|
||
override fun filter(filter: Bson?): SyncListCollectionNamesIterable = apply { wrapped.filter(filter) } | ||
|
||
override fun comment(comment: String?): SyncListCollectionNamesIterable = apply { wrapped.comment(comment) } | ||
|
||
override fun comment(comment: BsonValue?): SyncListCollectionNamesIterable = apply { wrapped.comment(comment) } | ||
|
||
override fun authorizedCollections(authorizedCollections: Boolean): SyncListCollectionNamesIterable = apply { | ||
wrapped.authorizedCollections(authorizedCollections) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* 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 com.mongodb.kotlin.client.coroutine | ||
|
||
import com.mongodb.reactivestreams.client.ListCollectionNamesPublisher | ||
import java.util.concurrent.TimeUnit | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.FlowCollector | ||
import kotlinx.coroutines.reactive.asFlow | ||
import org.bson.BsonValue | ||
import org.bson.conversions.Bson | ||
|
||
/** | ||
* Flow for listing collection names. | ||
* | ||
* @see [List collections](https://www.mongodb.com/docs/manual/reference/command/listCollections/) | ||
* @since 5.0 | ||
*/ | ||
public class ListCollectionNamesFlow(private val wrapped: ListCollectionNamesPublisher) : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is based on |
||
Flow<String> by wrapped.asFlow() { | ||
/** | ||
* Sets the maximum execution time on the server for this operation. | ||
* | ||
* @param maxTime the max time | ||
* @param timeUnit the time unit, defaults to Milliseconds | ||
* @return this | ||
* @see [Max Time](https://www.mongodb.com/docs/manual/reference/operator/meta/maxTimeMS/) | ||
*/ | ||
public fun maxTime(maxTime: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS): ListCollectionNamesFlow = apply { | ||
wrapped.maxTime(maxTime, timeUnit) | ||
} | ||
|
||
/** | ||
* Sets the number of documents to return per batch. | ||
* | ||
* @param batchSize the batch size | ||
* @return this | ||
* @see [Batch Size](https://www.mongodb.com/docs/manual/reference/method/cursor.batchSize/#cursor.batchSize) | ||
*/ | ||
public fun batchSize(batchSize: Int): ListCollectionNamesFlow = apply { wrapped.batchSize(batchSize) } | ||
|
||
/** | ||
* Sets the query filter to apply to the returned database names. | ||
* | ||
* @param filter the filter, which may be null. | ||
* @return this | ||
*/ | ||
public fun filter(filter: Bson?): ListCollectionNamesFlow = apply { wrapped.filter(filter) } | ||
|
||
/** | ||
* Sets the comment for this operation. A null value means no comment is set. | ||
* | ||
* @param comment the comment | ||
* @return this | ||
*/ | ||
public fun comment(comment: String?): ListCollectionNamesFlow = apply { wrapped.comment(comment) } | ||
|
||
/** | ||
* Sets the comment for this operation. A null value means no comment is set. | ||
* | ||
* @param comment the comment | ||
* @return this | ||
*/ | ||
public fun comment(comment: BsonValue?): ListCollectionNamesFlow = apply { wrapped.comment(comment) } | ||
|
||
/** | ||
* Sets the `authorizedCollections` field of the `listCollections` command. | ||
* | ||
* @param authorizedCollections If `true`, allows executing the `listCollections` command, which has the `nameOnly` | ||
* field set to `true`, without having the | ||
* [`listCollections` privilege](https://docs.mongodb.com/manual/reference/privilege-actions/#mongodb-authaction-listCollections) | ||
* on the database resource. | ||
* @return `this`. | ||
*/ | ||
public fun authorizedCollections(authorizedCollections: Boolean): ListCollectionNamesFlow = apply { | ||
wrapped.authorizedCollections(authorizedCollections) | ||
} | ||
|
||
public override suspend fun collect(collector: FlowCollector<String>): Unit = wrapped.asFlow().collect(collector) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, add a unit test for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
The added
ListCollectionsOperationTest
tests the public methodListCollectionsOperation.execute
instead of testing the (currently private) methodgetCommand
. This way it covers more ofListCollectionsOperation
. Since mocking is needed for this, I introducedMongoMockito.mock
(see its documentation for the detailed explanation and links to demo tests). I demonstrated it to @vbabanin and @rozza, and my understanding was that they liked this improvement overMockito.mock
.