-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Ensure custom KProperty include the name in the hashcode #1710
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ package com.mongodb.kotlin.client.property | |
|
||
import com.mongodb.annotations.Sealed | ||
import com.mongodb.kotlin.client.model.path | ||
import java.util.Objects | ||
import kotlin.reflect.KParameter | ||
import kotlin.reflect.KProperty1 | ||
import kotlin.reflect.KType | ||
|
@@ -84,6 +85,15 @@ public open class KPropertyPath<T, R>( | |
override fun callBy(args: Map<KParameter, Any?>): R = unSupportedOperation() | ||
override fun get(receiver: T): R = unSupportedOperation() | ||
override fun getDelegate(receiver: T): Any? = unSupportedOperation() | ||
override fun hashCode(): Int = Objects.hash(previous, property, name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good idea to provide the return if (this is KPropertyPath<*, T>) {
this.name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not used for the direct path, but the hashcode for CustomProperty relies on the hashcode. |
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
other as KPropertyPath<*, *> | ||
return Objects.equals(previous, other.previous) && | ||
Objects.equals(property, other.property) && | ||
Objects.equals(name, other.name) | ||
} | ||
|
||
public companion object { | ||
|
||
|
@@ -121,6 +131,13 @@ public open class KPropertyPath<T, R>( | |
override fun get(receiver: T): R = unSupportedOperation() | ||
override fun getDelegate(receiver: T): Any? = unSupportedOperation() | ||
override fun invoke(p1: T): R = unSupportedOperation() | ||
override fun hashCode(): Int = Objects.hash(previous, name) | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
other as CustomProperty<*, *> | ||
return Objects.equals(previous, other.previous) && Objects.equals(name, other.name) | ||
} | ||
} | ||
|
||
/** Provides "fake" property with custom name. */ | ||
|
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.
Good catch!
Can't we just refactor the map to
then use the hashCode
pathCache.computeIfAbsent(this.hashCode())
not sure why you concatenate the class name with the hash?
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.
I suppose I followed the previous logic for the key.
Hashcodes alone don't provide enough protection from collisions:
In this case they appear to - I tested up to 2 million for naming collisions but I still have some concern that it could also cause an issue. What do you think?
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.
The hashcode is calculated for the
KProperty1
so it will only match if you're using the sameKProperty1