Initial commit

This commit is contained in:
2025-07-30 15:45:43 +08:00
commit b1b205b9b4
25 changed files with 1850 additions and 0 deletions

94
core/build.gradle.kts Normal file
View File

@@ -0,0 +1,94 @@
import java.util.*
plugins {
alias(libs.plugins.dokka)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.maven.publish)
}
repositories {
mavenCentral()
}
dependencies {
implementation(libs.mongo.bson)
}
kotlin {
jvmToolchain(11)
}
val javadocJar by tasks.register<Jar>("dokkaJavadocJar") {
dependsOn(tasks.dokkaJavadoc)
from(tasks.dokkaJavadoc.flatMap { it.outputDirectory })
archiveClassifier.set("javadoc")
}
val sourcesJar by tasks.register<Jar>("sourcesJar") {
from(sourceSets.main.get().allSource)
archiveClassifier.set("sources")
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = rootProject.group.toString()
artifactId = "bson-core"
version = rootProject.version.toString()
from(components["java"])
artifact(javadocJar)
artifact(sourcesJar)
}
}
repositories {
val secretFile = rootProject.file("secret.properties")
if (secretFile.exists()) {
val secret = Properties()
secretFile.inputStream().also { secret.load(it) }
val mavenUrl = secret["neuon.maven.url"] as? String
val mavenUsername = secret["neuon.maven.username"] as? String
val mavenPassword = secret["neuon.maven.password"] as? String
if (mavenUrl != null && mavenUsername != null && mavenPassword != null) {
maven {
requireNotNull(mavenUrl) {
"neuon.maven.url is missing from secret.properties"
}
requireNotNull(mavenUsername) {
"neuon.maven.username is missing from secret.properties"
}
requireNotNull(mavenPassword) {
"neuon.maven.password is missing from secret.properties"
}
name = "neuon"
url = uri(mavenUrl)
credentials(PasswordCredentials::class) {
username = mavenUsername
password = mavenPassword
}
}
} else {
val list = listOfNotNull(
"neuon.maven.url".takeIf { mavenUrl == null },
"neuon.maven.username".takeIf { mavenUsername == null },
"neuon.maven.password".takeIf { mavenPassword == null },
)
val message = buildString {
append("Missing Maven publication in secret.properties: ")
append(list.joinToString())
append(". Ignore this if you don't plan on publishing this library")
}
logger.error(message)
}
} else {
logger.error(
buildString {
append("Missing secret.properties. Ignore this if you don't plan on publishing this library")
},
)
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* MIT License
*
* Copyright (c) 2025 Neuon AI
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package ai.neuon.utility.bson
import org.bson.BsonArray
import org.bson.BsonBoolean
import org.bson.BsonDocument
import org.bson.BsonDouble
import org.bson.BsonInt32
import org.bson.BsonInt64
import org.bson.BsonObjectId
import org.bson.BsonString
import org.bson.types.ObjectId
inline fun buildBsonArray(builder: BsonArray.() -> Unit): BsonArray {
return BsonArray().apply(builder)
}
fun BsonArray.add(value: ObjectId) {
add(BsonObjectId(value))
}
fun BsonArray.add(value: String) {
add(BsonString(value))
}
fun BsonArray.add(value: Int) {
add(BsonInt32(value))
}
fun BsonArray.add(value: Long) {
add(BsonInt64(value))
}
fun BsonArray.add(value: Float) {
add(BsonDouble(value.toDouble()))
}
fun BsonArray.add(value: Double) {
add(BsonDouble(value))
}
fun BsonArray.add(value: Boolean) {
add(BsonBoolean(value))
}
inline fun BsonArray.addBsonDocument(builder: BsonDocument.() -> Unit) {
add(buildBsonDocument(builder))
}

View File

@@ -0,0 +1,154 @@
/*
* MIT License
*
* Copyright (c) 2025 Neuon AI
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package ai.neuon.utility.bson
import org.bson.BsonArray
import org.bson.BsonBoolean
import org.bson.BsonDateTime
import org.bson.BsonDocument
import org.bson.BsonDouble
import org.bson.BsonInt32
import org.bson.BsonInt64
import org.bson.BsonInvalidOperationException
import org.bson.BsonNumber
import org.bson.BsonObjectId
import org.bson.BsonString
import org.bson.BsonValue
import org.bson.types.ObjectId
fun BsonDocument.getStringOrNull(key: String): BsonString? {
return try {
getString(key)
} catch (_: BsonInvalidOperationException) {
null
}
}
fun BsonDocument.getNumberOrNull(key: String): BsonNumber? {
return try {
getNumber(key)
} catch (_: BsonInvalidOperationException) {
null
}
}
fun BsonDocument.getDateTimeOrNull(key: String): BsonDateTime? {
return try {
getDateTime(key)
} catch (_: BsonInvalidOperationException) {
null
}
}
fun BsonDocument.getDoubleOrNull(key: String): BsonDouble? {
return try {
getDouble(key)
} catch (_: BsonInvalidOperationException) {
null
}
}
fun BsonDocument.getArrayOrNull(key: String): BsonArray? {
return try {
getArray(key)
} catch (_: BsonInvalidOperationException) {
null
}
}
fun BsonDocument.getDocumentOrNull(key: String): BsonDocument? {
return try {
getDocument(key)
} catch (_: BsonInvalidOperationException) {
null
}
}
fun BsonDocument.getObjectIdOrNull(key: String): BsonObjectId? {
return try {
getObjectId(key)
} catch (_: BsonInvalidOperationException) {
null
}
}
fun BsonDocument.getBooleanOrNull(key: String): BsonBoolean? {
return try {
getBoolean(key)
} catch (_: BsonInvalidOperationException) {
null
}
}
fun BsonDocument.getOrNull(key: String): BsonValue? {
return if (containsKey(key)) get(key) else null
}
fun BsonDocument.getNumberAsDoubleOrNull(key: String): Double? {
return try {
val value = getOrNull(key) ?: return null
when (value) {
is BsonDouble -> value.value
is BsonInt32 -> value.value.toDouble()
is BsonInt64 -> value.value.toDouble()
else -> null
}
} catch (_: Exception) {
null
}
}
inline fun buildBsonDocument(builder: BsonDocument.() -> Unit): BsonDocument {
return BsonDocument().apply(builder)
}
inline fun BsonDocument.putBsonDocument(key: String, builder: BsonDocument.() -> Unit): Unit {
put(key = key, value = buildBsonDocument(builder))
}
fun BsonDocument.putBsonArray(key: String, array: BsonArray) {
put(key = key, value = array)
}
inline fun BsonDocument.putBsonArray(key: String, builder: BsonArray.() -> Unit): Unit {
put(key = key, value = buildBsonArray(builder))
}
fun BsonDocument.put(key: String, value: String) {
put(key = key, BsonString(value))
}
fun BsonDocument.put(key: String, value: Int) {
put(key = key, BsonInt32(value))
}
fun BsonDocument.put(key: String, value: Long) {
put(key = key, BsonInt64(value))
}
fun BsonDocument.put(key: String, value: Float) {
put(key = key, BsonDouble(value.toDouble()))
}
fun BsonDocument.put(key: String, value: Double) {
put(key = key, BsonDouble(value))
}
fun BsonDocument.put(key: String, value: Boolean) {
put(key = key, BsonBoolean(value))
}
fun BsonDocument.put(key: String, value: ObjectId) {
put(key = key, BsonObjectId(value))
}