Skip to content
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

Supply proper equals(), hashCode(), and toString() for SerialDescriptor() wrapper #2942

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,25 @@ public fun SerialDescriptor(serialName: String, original: SerialDescriptor): Ser
return WrappedSerialDescriptor(serialName, original)
}

internal class WrappedSerialDescriptor(override val serialName: String, original: SerialDescriptor) : SerialDescriptor by original
internal class WrappedSerialDescriptor(override val serialName: String, private val original: SerialDescriptor) :
SerialDescriptor by original {

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is WrappedSerialDescriptor) return false

return serialName == other.serialName && original == other.original
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok if we check serialName inside original?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting point. Ideally, we should not check it. However, given that equalsImpl requires lambda to check type parameters, and we do not have any way to get them here, it seems that we can't avoid this check.

At least, this should solve the case when the descriptor is identical, but object is created twice.

}

override fun hashCode(): Int {
var result = serialName.hashCode()
result = 31 * result + original.hashCode()
return result
}

override fun toString(): String = toStringImpl()

}

/**
* An unsafe alternative to [buildClassSerialDescriptor] that supports an arbitrary [SerialKind].
Expand Down Expand Up @@ -370,10 +388,5 @@ internal class SerialDescriptorImpl(

override fun hashCode(): Int = _hashCode

override fun toString(): String {
return (0 until elementsCount).joinToString(", ", prefix = "$serialName(", postfix = ")") {
getElementName(it) + ": " + getElementDescriptor(it).serialName
}
}
override fun toString(): String = toStringImpl()
}

Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ internal open class PluginGeneratedSerialDescriptor(

override fun hashCode(): Int = _hashCode

override fun toString(): String {
return (0 until elementsCount).joinToString(", ", "$serialName(", ")") { i ->
getElementName(i) + ": " + getElementDescriptor(i).serialName
}
}
override fun toString(): String = toStringImpl()
}

@OptIn(ExperimentalSerializationApi::class)
Expand Down Expand Up @@ -131,3 +127,7 @@ internal fun SerialDescriptor.hashCodeImpl(typeParams: Array<SerialDescriptor>):
result = 31 * result + kindHash
return result
}

internal fun SerialDescriptor.toStringImpl(): String = (0 until elementsCount).joinToString(", ", "$serialName(", ")") { i ->
getElementName(i) + ": " + getElementDescriptor(i).serialName
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ class WrappedSerialDescriptorTest {
}
assertFails { SerialDescriptor("kotlin.Int", Int.serializer().descriptor) }
}

@Test
fun testWrappedEqHc() {
val a = SerialDescriptor("SimpleTypeWrapper", SimpleType.serializer().descriptor)
val b = SerialDescriptor("SimpleTypeWrapper", SimpleType.serializer().descriptor)
assertEquals(a, b)
assertEquals(a.hashCode(), b.hashCode())
assertNotEquals(a, SimpleType.serializer().descriptor)
assertEquals(a.toString(), "SimpleTypeWrapper(int: kotlin.Int, float: kotlin.Float)")
}
}