From ba264127847a9eb5f236d4c8f95a56d270677b8f Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 5 Oct 2022 13:47:52 +0200 Subject: [PATCH] Automatic custom exceptions registration using @MetaSerializable --- .../main/kotlin/io/kvision/ksp/KVProcessor.kt | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/kvision-tools/kvision-ksp-processor/src/main/kotlin/io/kvision/ksp/KVProcessor.kt b/kvision-tools/kvision-ksp-processor/src/main/kotlin/io/kvision/ksp/KVProcessor.kt index 88911d2e2b0..2c312d55077 100644 --- a/kvision-tools/kvision-ksp-processor/src/main/kotlin/io/kvision/ksp/KVProcessor.kt +++ b/kvision-tools/kvision-ksp-processor/src/main/kotlin/io/kvision/ksp/KVProcessor.kt @@ -128,16 +128,14 @@ class KVProcessor( exceptions.add(ExceptionNameDetails(packageName, className)) classDeclaration.containingFile }.toList().toTypedArray() - if (exceptions.isNotEmpty()) { - codeGenerator.createNewFile( - Dependencies(true, *depsExceptions), - "io.kvision.remote", - "GeneratedKVServiceExceptions" - ).writer().use { - when (codeGenerator.generatedFile.first().toString().sourceSetBelow("ksp")) { - "commonMain" -> { - it.write(generateCommonCodeExceptions(exceptions)) - } + codeGenerator.createNewFile( + Dependencies(true, *depsExceptions), + "io.kvision.remote", + "GeneratedKVServiceExceptions" + ).writer().use { + when (codeGenerator.generatedFile.first().toString().sourceSetBelow("ksp")) { + "commonMain" -> { + it.write(generateCommonCodeExceptions(exceptions)) } } } @@ -351,22 +349,26 @@ class KVProcessor( appendLine("import kotlinx.serialization.modules.polymorphic") appendLine("import kotlinx.serialization.modules.subclass") appendLine() - appendLine("private var registered = false") - appendLine() - appendLine("fun registerKVisionServiceExceptions() {") - appendLine(" if (!registered) {") - appendLine(" RemoteSerialization.customConfiguration = Json {") - appendLine(" serializersModule = SerializersModule {") - appendLine(" polymorphic(AbstractServiceException::class) {") - services.forEach { - appendLine(" subclass(${it.packageName}.${it.className}::class)") + if (services.isNotEmpty()) { + appendLine("private var registered = false") + appendLine() + appendLine("fun registerKVisionServiceExceptions() {") + appendLine(" if (!registered) {") + appendLine(" RemoteSerialization.customConfiguration = Json {") + appendLine(" serializersModule = SerializersModule {") + appendLine(" polymorphic(AbstractServiceException::class) {") + services.forEach { + appendLine(" subclass(${it.packageName}.${it.className}::class)") + } + appendLine(" }") + appendLine(" }") + appendLine(" }") + appendLine(" registered = true") + appendLine(" }") + appendLine("}") + } else { + appendLine("fun registerKVisionServiceExceptions() {}") } - appendLine(" }") - appendLine(" }") - appendLine(" }") - appendLine(" registered = true") - appendLine(" }") - appendLine("}") appendLine() }.toString() }