Skip to content

Commit 7fc351d

Browse files
Replace typeOf<T?>() with typeOf<T>().withNullability(true)
For platform types (i.e. String!), typeOf<T?>() still returns the platform type (i.e. String!). However, the nullable type is desired here, which withNullability(true) returns.
1 parent eec4825 commit 7fc351d

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

cosmic-binary-format/src/main/kotlin/xyz/xenondevs/cbf/Compound.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class Compound private constructor(
305305
@OptIn(UncheckedApi::class)
306306
@JvmName("entry1")
307307
inline fun <reified T : Any> entry(key: String, noinline defaultValue: () -> T? = { null }): MutableProvider<T?> =
308-
entry(typeOf<T?>(), key, defaultValue)
308+
entry(typeOf<T>().withNullability(true), key, defaultValue)
309309

310310
/**
311311
* Creates a [MutableProvider] of [type] that is linked to the value of this compound under [key].
@@ -355,7 +355,7 @@ class Compound private constructor(
355355
*/
356356
@OptIn(UncheckedApi::class)
357357
inline operator fun <reified T : Any> get(key: String): T? {
358-
return get(typeOf<T?>(), key)
358+
return get(typeOf<T>().withNullability(true), key)
359359
}
360360

361361
/**

cosmic-binary-format/src/main/kotlin/xyz/xenondevs/cbf/CosmicBinaryFormat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ object Cbf {
110110
* Registers a [BinarySerializer] that serializes, deserializes, and copies the exact type [T?][T].
111111
*/
112112
inline fun <reified T : Any> registerSerializer(serializer: BinarySerializer<T>) {
113-
val serializerType = typeOf<T?>()
113+
val serializerType = typeOf<T>().withNullability(true)
114114
val factory = object : BinarySerializerFactory {
115115
override fun create(type: KType): BinarySerializer<*>? =
116116
if (type.withNullability(true).equalsIgnorePlatformTypes(serializerType)) serializer else null
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package xyz.xenondevs.cbf
2+
3+
import org.junit.jupiter.api.Test
4+
import org.junit.jupiter.api.assertDoesNotThrow
5+
import xyz.xenondevs.cbf.io.ByteReader
6+
import xyz.xenondevs.cbf.io.ByteWriter
7+
import xyz.xenondevs.cbf.serializer.BinarySerializer
8+
import java.util.concurrent.atomic.AtomicReference
9+
10+
class PlatformTypesSerializerSelectionTest {
11+
12+
private class Foo1
13+
private class Foo2
14+
private class Foo3
15+
16+
@Test
17+
fun `test retrieving non-platform type serializer after registering as platform type`() {
18+
val serializer = platformTypeSerializer(AtomicReference(Foo1())) // BinarySerializer<Foo1!>
19+
Cbf.registerSerializer(serializer)
20+
21+
assertDoesNotThrow { Cbf.getSerializer<Foo1>() }
22+
}
23+
24+
@Test
25+
fun `test retrieving platform type serializer after registering as platform type`() {
26+
val ref = AtomicReference(Foo2())
27+
val serializer = platformTypeSerializer(ref) // BinarySerializer<Foo2!>
28+
Cbf.registerSerializer(serializer)
29+
30+
assertDoesNotThrow { Cbf.getSerializerLike(ref) }
31+
}
32+
33+
@Test
34+
fun `test retrieving platform type serializer after registering as non-platform type`() {
35+
val serializer = object : BinarySerializer<Foo3> {
36+
override fun read(reader: ByteReader) = throw UnsupportedOperationException()
37+
override fun write(obj: Foo3?, writer: ByteWriter) = throw UnsupportedOperationException()
38+
override fun copy(obj: Foo3?) = throw UnsupportedOperationException()
39+
}
40+
Cbf.registerSerializer(serializer)
41+
42+
assertDoesNotThrow { Cbf.getSerializerLike(AtomicReference(Foo3())) }
43+
}
44+
45+
private fun <T : Any> platformTypeSerializer(atomicRef: AtomicReference<T>): BinarySerializer<T> {
46+
return object : BinarySerializer<T> {
47+
override fun read(reader: ByteReader) = throw UnsupportedOperationException()
48+
override fun write(obj: T?, writer: ByteWriter) = throw UnsupportedOperationException()
49+
override fun copy(obj: T?) = throw UnsupportedOperationException()
50+
}
51+
}
52+
53+
private inline fun <reified T : Any> Cbf.getSerializerLike(atomicRef: AtomicReference<T>): BinarySerializer<T> {
54+
return Cbf.getSerializer<T>()
55+
}
56+
57+
}

0 commit comments

Comments
 (0)