Skip to content

Commit

Permalink
add 'kmongo.id.enabled' system property
Browse files Browse the repository at this point in the history
  • Loading branch information
lace00 committed Sep 1, 2021
1 parent 67d192b commit d8eecc3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,21 @@ internal object MongoIdUtil {
}

private val propertyIdCache: MutableMap<KClass<*>, IdPropertyWrapper>
by lazySoft { ConcurrentHashMap<KClass<*>, IdPropertyWrapper>() }
by lazySoft { ConcurrentHashMap<KClass<*>, IdPropertyWrapper>() }

fun findIdProperty(type: KClass<*>): KProperty1<*, *>? =
propertyIdCache.getOrPut(type) {
(getAnnotatedMongoIdProperty(type)
?: getIdProperty(type))
?: getIdProperty(type))
?.let { IdPropertyWrapper.IdProperty(it) }
?: NO_ID
?: NO_ID

}.property

private fun getIdProperty(type: KClass<*>): KProperty1<*, *>? =
try {
type.memberProperties.find { "_id" == it.name || "id" == it.name }
val idEnabled = System.getProperty("kmongo.id.enabled").toBoolean()
type.memberProperties.find { "_id" == it.name || (idEnabled && "id" == it.name) }
} catch (error: KotlinReflectionInternalError) {
//ignore
null
Expand All @@ -93,7 +94,7 @@ internal object MongoIdUtil {
} else {
type.memberProperties.find { p ->
p.javaField?.isAnnotationPresent(BsonId::class.java) == true
|| p.getter.javaMethod?.isAnnotationPresent(BsonId::class.java) == true
|| p.getter.javaMethod?.isAnnotationPresent(BsonId::class.java) == true
}
}
} catch (error: KotlinReflectionInternalError) {
Expand All @@ -104,11 +105,11 @@ internal object MongoIdUtil {
private fun findPrimaryConstructorParameter(type: KClass<*>): KParameter? =
try {
type.primaryConstructor?.parameters?.firstOrNull { it.findAnnotation<BsonId>() != null }
?: type.superclasses
.asSequence()
.map { findPrimaryConstructorParameter(it) }
.filterNotNull()
.firstOrNull()
?: type.superclasses
.asSequence()
.map { findPrimaryConstructorParameter(it) }
.filterNotNull()
.firstOrNull()
} catch (error: KotlinReflectionInternalError) {
//ignore
null
Expand All @@ -118,5 +119,4 @@ internal object MongoIdUtil {
idProperty.isAccessible = true
return idProperty.get(instance)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class UUIDTest
val bytes = mapper.writeValueAsBytes(testObject)

// Check the type of the binary representation of the UUID
assertEquals(if(uuidRepresentation == UuidRepresentation.STANDARD) 4 else 3, bytes[13].toInt())
assertEquals(if(uuidRepresentation == UuidRepresentation.STANDARD) 4 else 3, bytes[12].toInt())

val decodedObject: TestingObject = mapper.readValue(bytes)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class MongoIdUtilTest : KMongoRootTest() {

@Test
fun extractIdWithoutUnderscore() {
System.setProperty("kmongo.id.enabled", "true")
assertEquals("id", KMongoUtil.extractId(TestObjWithId("id"), TestObjWithId::class))
}

Expand Down

0 comments on commit d8eecc3

Please sign in to comment.