Skip to content

Commit

Permalink
♻️ Refactor serial name of ZeroAsByteSerializer type
Browse files Browse the repository at this point in the history
Gradle projects: types-internal and types-kotlinx-serialization.
References: #602 and #644.
  • Loading branch information
LVMVRQUXL committed Apr 15, 2024
1 parent 1c978b2 commit a731a5c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import kotlin.reflect.KClass
/** Returns the simple name of the type [T]. */
@InternalKotoolsTypesApi
public inline fun <reified T : Any> simpleNameOf(): String {
val kClass: KClass<T> = T::class
return kClass.simpleName
?: error("Getting simple name of '$kClass' shouldn't fail.")
val type: KClass<T> = T::class
return checkNotNull(type.simpleName) {
"Getting simple name of '$type' shouldn't return 'null'."
}
}
18 changes: 16 additions & 2 deletions subprojects/kotlinx-serialization/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet

plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.kotlinx.binary.compatibility.validator)
Expand All @@ -17,8 +19,20 @@ apiValidation.apiDumpDirectory = "src/api"

documentation.packages = layout.projectDirectory.file("packages.md").asFile

kotlin.sourceSets.all {
languageSettings.optIn("kotools.types.internal.InternalKotoolsTypesApi")
kotlin.sourceSets {
all {
languageSettings.optIn("kotools.types.internal.InternalKotoolsTypesApi")
}
val jvmAndNativeMain: KotlinSourceSet by creating {
commonMain.get()
.let(this::dependsOn)
}
jvmMain.get()
.dependsOn(jvmAndNativeMain)
val nativeMain: KotlinSourceSet by creating { dependsOn(jvmAndNativeMain) }
listOf(linuxMain, macosMain, macosArm64Main, windowsMain)
.map(NamedDomainObjectProvider<KotlinSourceSet>::get)
.forEach { it.dependsOn(nativeMain) }
}

publishing.publications.named<MavenPublication>("kotlinMultiplatform")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.kotools.types.kotlinx.serialization

@Suppress("unused")
internal expect inline fun <reified T : Any> serialNameOf(): String
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kotools.types.kotlinx.serialization

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.PrimitiveKind
Expand All @@ -8,18 +9,14 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotools.types.experimental.ExperimentalKotoolsTypesApi
import kotools.types.internal.simpleNameOf
import org.kotools.types.Zero

@OptIn(ExperimentalKotoolsTypesApi::class)
internal object ZeroAsByteSerializer : KSerializer<Zero> {
override val descriptor: SerialDescriptor
get() {
val className: String = simpleNameOf<Zero>()
return PrimitiveSerialDescriptor(
serialName = "org.kotools.types.$className",
kind = PrimitiveKind.BYTE
)
val serialName: String = serialNameOf<Zero>()
return PrimitiveSerialDescriptor(serialName, PrimitiveKind.BYTE)
}

override fun serialize(encoder: Encoder, value: Zero) {
Expand All @@ -35,8 +32,9 @@ internal object ZeroAsByteSerializer : KSerializer<Zero> {
throw SerializationException(message)
}

@OptIn(ExperimentalSerializationApi::class)
fun deserializationErrorMessage(decodedValue: Byte): String {
val typeName: String = simpleNameOf<Zero>()
return "Unable to deserialize '$typeName' from ${decodedValue}."
val serialName: String = this.descriptor.serialName
return "Unable to deserialize '$serialName' from ${decodedValue}."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import kotlinx.serialization.descriptors.SerialKind
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotools.types.experimental.ExperimentalKotoolsTypesApi
import kotools.types.internal.simpleNameOf
import org.kotools.types.Zero
import kotlin.random.Random
import kotlin.test.Test
Expand All @@ -23,8 +22,7 @@ class ZeroAsByteSerializerTest {
fun descriptor_serialName_should_be_the_qualified_name_of_Zero() {
val serializer: KSerializer<Zero> = ZeroAsByteSerializer
val actual: String = serializer.descriptor.serialName
val className: String = simpleNameOf<Zero>()
val expected = "org.kotools.types.$className"
val expected: String = serialNameOf<Zero>()
assertEquals(expected, actual)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.kotools.types.kotlinx.serialization

import kotools.types.internal.simpleNameOf

internal actual inline fun <reified T : Any> serialNameOf(): String =
simpleNameOf<T>()
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.kotools.types.kotlinx.serialization

import kotlin.reflect.KClass

internal actual inline fun <reified T : Any> serialNameOf(): String {
val type: KClass<T> = T::class
return checkNotNull(type.qualifiedName) {
"Getting qualified name of $type shouldn't return 'null'."
}
}

0 comments on commit a731a5c

Please sign in to comment.