Skip to content

Commit a82e79e

Browse files
authored
Helper methods and extensions to improve kotlin interop. (#1478)
Adds a new package `org.mongodb.mongodb-driver-kotlin-extensions`. Its both, kotlin driver and bson kotlin implementation agnostic. Can be used with any combination of `bson-kotlin`, `bson-kotlinx`, `mongodb-driver-kotlin-sync` and `mongodb-driver-kotlin-coroutine`. Initial Filters extensions support, with both inflix and nested helpers eg: ``` import com.mongodb.kotlin.client.model.Filters.eq // infix Person::name.eq(person.name) // nested val bson = eq(Person::name, person.name) ``` Also adds path based support which works with vairous annotations on the data class: `@SerialName("_id")`, `@BsonId`, `@BsonProperty("_id")`: ``` (Restaurant::reviews / Review::score).path() == "reviews.rating" ``` JAVA-5308 JAVA-5484
1 parent 7e3108b commit a82e79e

File tree

11 files changed

+2626
-0
lines changed

11 files changed

+2626
-0
lines changed

Diff for: THIRD-PARTY-NOTICES

+23
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,26 @@ https://github.com/mongodb/mongo-java-driver.
161161
See the License for the specific language governing permissions and
162162
limitations under the License.
163163

164+
8) The following files (originally from https://github.com/Litote/kmongo):
165+
166+
Filters.kt
167+
Properties.kt
168+
KPropertyPath.kt
169+
FiltersTest.kt
170+
KPropertiesTest.kt
171+
172+
Copyright 2008-present MongoDB, Inc.
173+
Copyright (C) 2016/2022 Litote
174+
175+
Licensed under the Apache License, Version 2.0 (the "License");
176+
you may not use this file except in compliance with the License.
177+
You may obtain a copy of the License at
178+
179+
http://www.apache.org/licenses/LICENSE-2.0
180+
181+
Unless required by applicable law or agreed to in writing, software
182+
distributed under the License is distributed on an "AS IS" BASIS,
183+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
184+
See the License for the specific language governing permissions and
185+
limitations under the License.
186+

Diff for: config/spotbugs/exclude.xml

+6
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@
223223
<Method name="~.*validateAnnotations.*"/>
224224
<Bug pattern="BC_BAD_CAST_TO_ABSTRACT_COLLECTION"/>
225225
</Match>
226+
<Match>
227+
<!-- MongoDB status: "False Positive", SpotBugs rank: 17 -->
228+
<Class name="com.mongodb.kotlin.client.model.PropertiesKt$path$1"/>
229+
<Method name="~.*invoke.*"/>
230+
<Bug pattern="BC_BAD_CAST_TO_ABSTRACT_COLLECTION"/>
231+
</Match>
226232

227233
<!-- Spotbugs reports false positives for suspendable operations with default params
228234
see: https://github.com/Kotlin/kotlinx.coroutines/issues/3099

Diff for: driver-kotlin-extensions/build.gradle.kts

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
* http://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+
import io.gitlab.arturbosch.detekt.Detekt
17+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
18+
19+
plugins {
20+
id("org.jetbrains.kotlin.jvm")
21+
`java-library`
22+
23+
// Test based plugins
24+
id("com.diffplug.spotless")
25+
id("org.jetbrains.dokka")
26+
id("io.gitlab.arturbosch.detekt")
27+
}
28+
29+
repositories {
30+
mavenCentral()
31+
google()
32+
}
33+
34+
base.archivesName.set("mongodb-driver-kotlin-extensions")
35+
36+
description = "The MongoDB Kotlin Driver Extensions"
37+
38+
ext.set("pomName", "MongoDB Kotlin Driver Extensions")
39+
40+
dependencies {
41+
// Align versions of all Kotlin components
42+
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
43+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
44+
45+
api(project(path = ":driver-core", configuration = "default"))
46+
47+
testImplementation("org.jetbrains.kotlin:kotlin-reflect")
48+
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
49+
testImplementation("org.assertj:assertj-core:3.24.2")
50+
testImplementation("io.github.classgraph:classgraph:4.8.154")
51+
}
52+
53+
kotlin { explicitApi() }
54+
55+
tasks.withType<KotlinCompile> {
56+
kotlinOptions {
57+
jvmTarget = "1.8"
58+
freeCompilerArgs =
59+
listOf(
60+
// Adds OnlyInputTypes support
61+
"-Xallow-kotlin-package",
62+
)
63+
}
64+
}
65+
66+
// ===========================
67+
// Code Quality checks
68+
// ===========================
69+
val customLicenseHeader = "/^(?s)(?!.*@custom-license-header).*/"
70+
71+
spotless {
72+
kotlinGradle {
73+
ktfmt("0.39").dropboxStyle().configure { it.setMaxWidth(120) }
74+
trimTrailingWhitespace()
75+
indentWithSpaces()
76+
endWithNewline()
77+
licenseHeaderFile(rootProject.file("config/mongodb.license"), "(group|plugins|import|buildscript|rootProject)")
78+
}
79+
80+
kotlin {
81+
target("**/*.kt")
82+
ktfmt().dropboxStyle().configure { it.setMaxWidth(120) }
83+
trimTrailingWhitespace()
84+
indentWithSpaces()
85+
endWithNewline()
86+
87+
licenseHeaderFile(rootProject.file("config/mongodb.license"))
88+
.named("standard")
89+
.onlyIfContentMatches("^(?!Copyright .*? Litote).*\$")
90+
}
91+
92+
format("extraneous") {
93+
target("*.xml", "*.yml", "*.md")
94+
trimTrailingWhitespace()
95+
indentWithSpaces()
96+
endWithNewline()
97+
}
98+
}
99+
100+
tasks.named("check") { dependsOn("spotlessApply") }
101+
102+
detekt {
103+
allRules = true // fail build on any finding
104+
buildUponDefaultConfig = true // preconfigure defaults
105+
config = rootProject.files("config/detekt/detekt.yml") // point to your custom config defining rules to run,
106+
// overwriting default behavior
107+
baseline = rootProject.file("config/detekt/baseline.xml") // a way of suppressing issues before introducing detekt
108+
source = files(file("src/main/kotlin"), file("src/test/kotlin"))
109+
}
110+
111+
tasks.withType<Detekt>().configureEach {
112+
reports {
113+
html.required.set(true) // observe findings in your browser with structure and code snippets
114+
xml.required.set(true) // checkstyle like format mainly for integrations like Jenkins
115+
txt.required.set(false) // similar to the console output, contains issue signature to manually edit
116+
}
117+
}
118+
119+
spotbugs { showProgress.set(true) }
120+
121+
// ===========================
122+
// Test Configuration
123+
// ===========================
124+
125+
tasks.create("kotlinCheck") {
126+
description = "Runs all the kotlin checks"
127+
group = "verification"
128+
129+
dependsOn("clean", "check")
130+
tasks.findByName("check")?.mustRunAfter("clean")
131+
}
132+
133+
tasks.test { useJUnitPlatform() }
134+
135+
// ===========================
136+
// Dokka Configuration
137+
// ===========================
138+
val dokkaOutputDir = "${rootProject.buildDir}/docs/${base.archivesName.get()}"
139+
140+
tasks.dokkaHtml.configure {
141+
outputDirectory.set(file(dokkaOutputDir))
142+
moduleName.set(base.archivesName.get())
143+
}
144+
145+
val cleanDokka by tasks.register<Delete>("cleanDokka") { delete(dokkaOutputDir) }
146+
147+
project.parent?.tasks?.named("docs") {
148+
dependsOn(tasks.dokkaHtml)
149+
mustRunAfter(cleanDokka)
150+
}
151+
152+
tasks.javadocJar.configure {
153+
dependsOn(cleanDokka, tasks.dokkaHtml)
154+
archiveClassifier.set("javadoc")
155+
from(dokkaOutputDir)
156+
}
157+
158+
// ===========================
159+
// Sources publishing configuration
160+
// ===========================
161+
tasks.sourcesJar { from(project.sourceSets.main.map { it.kotlin }) }
162+
163+
afterEvaluate { tasks.jar { manifest { attributes["Automatic-Module-Name"] = "org.mongodb.driver.kotlin.core" } } }

0 commit comments

Comments
 (0)