Skip to content

Commit

Permalink
Automatic custom exceptions registration using @MetaSerializable
Browse files Browse the repository at this point in the history
  • Loading branch information
rjaros committed Oct 5, 2022
1 parent 54b5630 commit ba26412
Showing 1 changed file with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit ba26412

Please sign in to comment.