-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Adding Kotlin extensions methods for projection. #1515
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e52559e
Adding Kotlin extensions methods for projection. Fixes JAVA-5603
nhachicha 1fddaaa
Fixing formatting
nhachicha d070cb1
Exclude false positive spotbug warring
nhachicha 0a97010
Formatting
nhachicha e5a16dc
PR feedback
nhachicha b8bba10
PR feedback
nhachicha 354c956
Added missing Spotbug excluding conf
nhachicha 09cf6ad
Update since annotations
rozza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 236 additions & 0 deletions
236
driver-kotlin-extensions/src/main/kotlin/com/mongodb/kotlin/client/model/Projections.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* Copyright (C) 2016/2022 Litote | ||
* | ||
* 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. | ||
* | ||
* @custom-license-header | ||
*/ | ||
package com.mongodb.kotlin.client.model | ||
|
||
import com.mongodb.annotations.Beta | ||
import com.mongodb.annotations.Reason | ||
import com.mongodb.client.model.Aggregates | ||
import com.mongodb.client.model.Projections | ||
import kotlin.reflect.KProperty | ||
import kotlin.reflect.KProperty1 | ||
import org.bson.conversions.Bson | ||
|
||
/** The projection of the property. This is used in an aggregation pipeline to reference a property from a path. */ | ||
public val <T> KProperty<T>.projection: String | ||
get() = path().projection | ||
|
||
/** The projection of the property. */ | ||
public val String.projection: String | ||
get() = "\$$this" | ||
|
||
/** In order to write `$p.p2` */ | ||
@JvmSynthetic public infix fun <T0, T1> KProperty1<T0, T1?>.projectionWith(p2: String): String = "$projection.$p2" | ||
|
||
/** | ||
* Creates a projection of a property whose value is computed from the given expression. Projection with an expression | ||
* can be used in the following contexts: | ||
* <ul> | ||
* <li>$project aggregation pipeline stage.</li> | ||
* <li>Starting from MongoDB 4.4, it's also accepted in various find-related methods within the {@code | ||
* MongoCollection}-based API where projection is supported, for example: <ul> | ||
* <li>{@code find()}</li> | ||
* <li>{@code findOneAndReplace()}</li> | ||
* <li>{@code findOneAndUpdate()}</li> | ||
* <li>{@code findOneAndDelete()}</li> | ||
* </ul> | ||
* | ||
* </li> </ul> | ||
* | ||
* @param expression the expression | ||
* @param <T> the expression type | ||
* @return the projection | ||
* @see #computedSearchMeta(String) | ||
* @see Aggregates#project(Bson) | ||
*/ | ||
@JvmSynthetic | ||
public infix fun <T> KProperty<T>.computedFrom(expression: Any): Bson = | ||
Projections.computed(path(), (expression as? KProperty<*>)?.projection ?: expression) | ||
|
||
/** | ||
* Creates a projection of a String whose value is computed from the given expression. Projection with an expression can | ||
* be used in the following contexts: | ||
* <ul> | ||
* <li>$project aggregation pipeline stage.</li> | ||
* <li>Starting from MongoDB 4.4, it's also accepted in various find-related methods within the {@code | ||
* MongoCollection}-based API where projection is supported, for example: <ul> | ||
* <li>{@code find()}</li> | ||
* <li>{@code findOneAndReplace()}</li> | ||
* <li>{@code findOneAndUpdate()}</li> | ||
* <li>{@code findOneAndDelete()}</li> | ||
* </ul> | ||
* | ||
* </li> </ul> | ||
* | ||
* @param expression the expression | ||
* @return the projection | ||
* @see #computedSearchMeta(String) | ||
* @see Aggregates#project(Bson) | ||
*/ | ||
@JvmSynthetic | ||
public infix fun String.computedFrom(expression: Any): Bson = | ||
@Suppress("UNCHECKED_CAST") Projections.computed(this, (expression as? KProperty<Any>)?.projection ?: expression) | ||
|
||
/** | ||
* Creates a projection that includes all of the given properties. | ||
* | ||
* @param properties the field names | ||
* @return the projection | ||
*/ | ||
public fun include(vararg properties: KProperty<*>): Bson = include(properties.asList()) | ||
|
||
/** | ||
* Creates a projection that includes all of the given properties. | ||
* | ||
* @param properties the field names | ||
* @return the projection | ||
*/ | ||
public fun include(properties: Iterable<KProperty<*>>): Bson = Projections.include(properties.map { it.path() }) | ||
|
||
/** | ||
* Creates a projection that excludes all of the given properties. | ||
* | ||
* @param properties the field names | ||
* @return the projection | ||
*/ | ||
public fun exclude(vararg properties: KProperty<*>): Bson = exclude(properties.asList()) | ||
|
||
/** | ||
* Creates a projection that excludes all of the given properties. | ||
* | ||
* @param properties the field names | ||
* @return the projection | ||
*/ | ||
public fun exclude(properties: Iterable<KProperty<*>>): Bson = Projections.exclude(properties.map { it.path() }) | ||
|
||
/** | ||
* Creates a projection that excludes the _id field. This suppresses the automatic inclusion of _id that is the default, | ||
* even when other fields are explicitly included. | ||
* | ||
* @return the projection | ||
*/ | ||
public fun excludeId(): Bson = Projections.excludeId() | ||
|
||
/** | ||
* Creates a projection that includes for the given property only the first element of an array that matches the query | ||
* filter. This is referred to as the positional $ operator. | ||
* | ||
* @return the projection @mongodb.driver.manual reference/operator/projection/positional/#projection Project the first | ||
* matching element ($ operator) | ||
*/ | ||
public val <T> KProperty<T>.elemMatchProj: Bson | ||
get() = Projections.elemMatch(path()) | ||
|
||
/** | ||
* Creates a projection that includes for the given property only the first element of the array value of that field | ||
* that matches the given query filter. | ||
* | ||
* @param filter the filter to apply | ||
* @return the projection @mongodb.driver.manual reference/operator/projection/elemMatch elemMatch | ||
*/ | ||
public infix fun <T> KProperty<T>.elemMatchProj(filter: Bson): Bson = Projections.elemMatch(path(), filter) | ||
|
||
/** | ||
* Creates a $meta projection for the given property | ||
* | ||
* @param metaFieldName the meta field name | ||
* @return the projection @mongodb.driver.manual reference/operator/aggregation/meta/ | ||
* @see #metaTextScore(String) | ||
* @see #metaSearchScore(String) | ||
* @see #metaVectorSearchScore(String) | ||
* @see #metaSearchHighlights(String) | ||
* @since 4.1 | ||
*/ | ||
public infix fun <T> KProperty<T>.meta(metaFieldName: String): Bson = Projections.meta(path(), metaFieldName) | ||
|
||
/** | ||
* Creates a textScore projection for the given property, for use with text queries. Calling this method is equivalent | ||
* to calling {@link #meta(String)} with {@code "textScore"} as the argument. | ||
* | ||
* @return the projection | ||
* @see Filters#text(String, TextSearchOptions) @mongodb.driver.manual | ||
* reference/operator/aggregation/meta/#text-score-metadata--meta---textscore- textScore | ||
*/ | ||
public fun <T> KProperty<T>.metaTextScore(): Bson = Projections.metaTextScore(path()) | ||
|
||
/** | ||
* Creates a searchScore projection for the given property, for use with {@link Aggregates#search(SearchOperator, | ||
* SearchOptions)} / {@link Aggregates#search(SearchCollector, SearchOptions)}. Calling this method is equivalent to | ||
* calling {@link #meta(String, String)} with {@code "searchScore"} as the argument. | ||
* | ||
* @return the projection @mongodb.atlas.manual atlas-search/scoring/ Scoring | ||
* @since 4.7 | ||
*/ | ||
public fun <T> KProperty<T>.metaSearchScore(): Bson = Projections.metaSearchScore(path()) | ||
|
||
/** | ||
* Creates a vectorSearchScore projection for the given property, for use with {@link | ||
* Aggregates#vectorSearch(FieldSearchPath, Iterable, String, long, VectorSearchOptions)} . Calling this method is | ||
* equivalent to calling {@link #meta(String, String)} with {@code "vectorSearchScore"} as the argument. | ||
* | ||
* @return the projection @mongodb.atlas.manual atlas-search/scoring/ Scoring @mongodb.server.release 6.0.10 | ||
* @since 4.11 | ||
*/ | ||
@Beta(Reason.SERVER) | ||
public fun <T> KProperty<T>.metaVectorSearchScore(): Bson = Projections.metaVectorSearchScore(path()) | ||
|
||
/** | ||
* Creates a searchHighlights projection for the given property, for use with {@link Aggregates#search(SearchOperator, | ||
* SearchOptions)} / {@link Aggregates#search(SearchCollector, SearchOptions)}. Calling this method is equivalent to | ||
* calling {@link #meta(String, String)} with {@code "searchHighlights"} as the argument. | ||
* | ||
* @return the projection | ||
* @see com.mongodb.client.model.search.SearchHighlight @mongodb.atlas.manual atlas-search/highlighting/ Highlighting | ||
* @since 4.7 | ||
*/ | ||
public fun <T> KProperty<T>.metaSearchHighlights(): Bson = Projections.metaSearchHighlights(path()) | ||
|
||
/** | ||
* Creates a projection to the given property of a slice of the array value of that field. | ||
* | ||
* @param limit the number of elements to project. | ||
* @return the projection @mongodb.driver.manual reference/operator/projection/slice Slice | ||
*/ | ||
public infix fun <T> KProperty<T>.slice(limit: Int): Bson = Projections.slice(path(), limit) | ||
|
||
/** | ||
* Creates a projection to the given property of a slice of the array value of that field. | ||
* | ||
* @param skip the number of elements to skip before applying the limit | ||
* @param limit the number of elements to project | ||
* @return the projection @mongodb.driver.manual reference/operator/projection/slice Slice | ||
*/ | ||
public fun <T> KProperty<T>.slice(skip: Int, limit: Int): Bson = Projections.slice(path(), skip, limit) | ||
|
||
/** | ||
* Creates a projection that combines the list of projections into a single one. If there are duplicate keys, the last | ||
* one takes precedence. | ||
* | ||
* @param projections the list of projections to combine | ||
* @return the combined projection | ||
*/ | ||
public fun fields(vararg projections: Bson): Bson = Projections.fields(*projections) | ||
|
||
/** | ||
* Creates a projection that combines the list of projections into a single one. If there are duplicate keys, the last | ||
* one takes precedence. | ||
* | ||
* @param projections the list of projections to combine | ||
* @return the combined projection @mongodb.driver.manual | ||
*/ | ||
public fun fields(projections: List<Bson>): Bson = Projections.fields(projections) | ||
nhachicha marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.