-
Notifications
You must be signed in to change notification settings - Fork 6
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
Implement $addFields stage dsl #103
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c2cd5da
feat: add field stage dsl
username1103 4dc0664
feat: add field stage dsl repository test
username1103 3728b25
feat: use .. in path
username1103 b28e061
docs: add param docs
username1103 00d136a
feat: add @Id annotation check in toFieldName
username1103 2b9ecc1
feat: use @Id annotation
username1103 210e162
feat: ignore detekt return count to toFieldName
username1103 ca53729
feat: change _id to id property
username1103 c90357f
feat: check property name in toFieldName
username1103 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
94 changes: 94 additions & 0 deletions
94
...c/main/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/AddFieldsStageDsl.kt
This file contains 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,94 @@ | ||
package com.github.inflab.spring.data.mongodb.core.aggregation | ||
|
||
import com.github.inflab.spring.data.mongodb.core.aggregation.expression.AggregationExpressionDsl | ||
import com.github.inflab.spring.data.mongodb.core.annotation.AggregationMarker | ||
import com.github.inflab.spring.data.mongodb.core.extension.toDotPath | ||
import org.springframework.data.mongodb.core.aggregation.AddFieldsOperation | ||
import org.springframework.data.mongodb.core.aggregation.AggregationExpression | ||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* A Kotlin DSL to configure $addFields stage using idiomatic Kotlin code. | ||
* | ||
* @author username1103 | ||
* @since 1.0 | ||
* @see <a href="https://www.mongodb.com/docs/v7.0/reference/operator/aggregation/addFields">$addFields (aggregation)</a> | ||
*/ | ||
@AggregationMarker | ||
class AddFieldsStageDsl { | ||
private val builder = AddFieldsOperation.builder() | ||
|
||
/** | ||
* Adds new fields to documents with aggregation expression. | ||
* | ||
* @param configuration The configuration block where you can use DSL to define aggregation expression. | ||
*/ | ||
infix fun String.set(configuration: AggregationExpressionDsl.() -> AggregationExpression) { | ||
builder.addField(this).withValue(AggregationExpressionDsl().configuration()) | ||
} | ||
|
||
/** | ||
* Adds new fields to documents from a field. | ||
* | ||
* @param path The path of the field to contain value to be added. | ||
*/ | ||
infix fun String.set(path: KProperty<Any?>) { | ||
builder.addField(this).withValue("${'$'}${path.toDotPath()}") | ||
} | ||
|
||
/** | ||
* Adds new fields to documents with a value. | ||
* | ||
* @param value The value of the field to add. | ||
*/ | ||
infix fun String.set(value: Any?) { | ||
builder.addField(this).withValue(value) | ||
} | ||
|
||
/** | ||
* Adds new fields to documents from a field. | ||
* | ||
* @param fieldPath The path of the field to contain value to be added. | ||
*/ | ||
infix fun String.setByField(fieldPath: String) { | ||
builder.addField(this).withValue("$$fieldPath") | ||
} | ||
|
||
/** | ||
* Adds new fields to documents with aggregation expression. | ||
* | ||
* @param configuration The configuration block where you can use DSL to define aggregation expression. | ||
*/ | ||
infix fun KProperty<Any?>.set(configuration: AggregationExpressionDsl.() -> AggregationExpression) { | ||
builder.addField(this.toDotPath()).withValue(AggregationExpressionDsl().configuration()) | ||
} | ||
|
||
/** | ||
* Adds new fields to documents from a field. | ||
* | ||
* @param path The path of the field to contain value to be added. | ||
*/ | ||
infix fun KProperty<Any?>.set(path: KProperty<Any?>) { | ||
builder.addField(this.toDotPath()).withValue("${'$'}${path.toDotPath()}") | ||
} | ||
|
||
/** | ||
* Adds new fields to documents with a value. | ||
* | ||
* @param value The value of the field to add. | ||
*/ | ||
infix fun KProperty<Any?>.set(value: Any?) { | ||
builder.addField(this.toDotPath()).withValue(value) | ||
} | ||
|
||
/** | ||
* Adds new fields to documents from a field. | ||
* | ||
* @param fieldPath The path of the field to contain value to be added. | ||
*/ | ||
infix fun KProperty<Any?>.setByField(fieldPath: String) { | ||
builder.addField(this.toDotPath()).withValue("$$fieldPath") | ||
} | ||
|
||
internal fun get() = builder.build() | ||
} |
This file contains 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 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
204 changes: 204 additions & 0 deletions
204
...st/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/AddFieldsStageDslTest.kt
This file contains 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,204 @@ | ||
package com.github.inflab.spring.data.mongodb.core.aggregation | ||
|
||
import com.github.inflab.spring.data.mongodb.core.util.shouldBeJson | ||
import io.kotest.core.spec.style.FreeSpec | ||
|
||
internal class AddFieldsStageDslTest : FreeSpec({ | ||
|
||
fun addFields(configuration: AddFieldsStageDsl.() -> Unit): AddFieldsStageDsl = | ||
AddFieldsStageDsl().apply(configuration) | ||
|
||
"set" - { | ||
"should set field with path" { | ||
// given | ||
data class Test(val targetPath: Int) | ||
val stage = addFields { | ||
"a" set Test::targetPath | ||
} | ||
|
||
// when | ||
val result = stage.get() | ||
|
||
// then | ||
result.shouldBeJson( | ||
""" | ||
{ | ||
"${'$'}addFields": { | ||
"a": "${'$'}targetPath" | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
"should set field path with path" { | ||
// given | ||
data class Test(val sourcePath: Int, val targetPath: Int) | ||
val stage = addFields { | ||
Test::targetPath set Test::sourcePath | ||
} | ||
|
||
// when | ||
val result = stage.get() | ||
|
||
// then | ||
result.shouldBeJson( | ||
""" | ||
{ | ||
"${'$'}addFields": { | ||
"targetPath": "${'$'}sourcePath" | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
"should set field with aggregation expression" { | ||
// given | ||
data class Test(val targetPath: Int) | ||
val stage = addFields { | ||
"a" set { | ||
add { of(1) and 2 and Test::targetPath } | ||
} | ||
} | ||
|
||
// when | ||
val result = stage.get() | ||
|
||
// then | ||
result.shouldBeJson( | ||
""" | ||
{ | ||
"${'$'}addFields": { | ||
"a": { | ||
"${'$'}add": [ | ||
1, | ||
2, | ||
"${'$'}targetPath" | ||
] | ||
} | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
"should set field path with aggregation expression" { | ||
// given | ||
data class Test(val sourcePath: Int, val targetPath: Int) | ||
val stage = addFields { | ||
Test::targetPath set { | ||
add { of(1) and 2 and Test::sourcePath } | ||
} | ||
} | ||
|
||
// when | ||
val result = stage.get() | ||
|
||
// then | ||
result.shouldBeJson( | ||
""" | ||
{ | ||
"${'$'}addFields": { | ||
"targetPath": { | ||
"${'$'}add": [ | ||
1, | ||
2, | ||
"${'$'}sourcePath" | ||
] | ||
} | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
"should set field with value" { | ||
// given | ||
val stage = addFields { | ||
"a" set 1 | ||
} | ||
|
||
// when | ||
val result = stage.get() | ||
|
||
// then | ||
result.shouldBeJson( | ||
""" | ||
{ | ||
"${'$'}addFields": { | ||
"a": 1 | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
"should set field path with value" { | ||
// given | ||
data class Test(val targetPath: Int) | ||
val stage = addFields { | ||
Test::targetPath set 1 | ||
} | ||
|
||
// when | ||
val result = stage.get() | ||
|
||
// then | ||
result.shouldBeJson( | ||
""" | ||
{ | ||
"${'$'}addFields": { | ||
"targetPath": 1 | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
} | ||
|
||
"setByField" - { | ||
"should set field with value of other field" { | ||
// given | ||
val stage = addFields { | ||
"a" setByField "b" | ||
} | ||
|
||
// when | ||
val result = stage.get() | ||
|
||
// then | ||
result.shouldBeJson( | ||
""" | ||
{ | ||
"${'$'}addFields": { | ||
"a": "${'$'}b" | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
|
||
"should set field path with value of other field" { | ||
// given | ||
data class Test(val targetPath: Int) | ||
val stage = addFields { | ||
Test::targetPath setByField "b" | ||
} | ||
|
||
// when | ||
val result = stage.get() | ||
|
||
// then | ||
result.shouldBeJson( | ||
""" | ||
{ | ||
"${'$'}addFields": { | ||
"targetPath": "${'$'}b" | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
} | ||
}) |
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.
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.
There is
withValueOf
method for field path.But test case is failed if used it.
Bad luck 😢
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.
Yes. So use withValue 🥲