-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inspection for missing Optional default values (#797)
If a dev.kord.common.entity.optional.Optional is used in a @serializable class, it should always have a default value so that deserialization works if the value is missing in the JSON. Until now this was easy to forget which could lead to deserialization errors down the line. By introducing a KSP processor that looks at all primary constructor parameters of @serializable classes, there is now a static check executed at build time that verifies the default value is not forgotten in any place.
- Loading branch information
1 parent
98181ac
commit af9300c
Showing
11 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
ksp-processors/src/main/kotlin/inspection/OptionalDefaultInspectionProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package dev.kord.ksp.inspection | ||
|
||
import com.google.devtools.ksp.findActualType | ||
import com.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.* | ||
import dev.kord.ksp.getSymbolsWithAnnotation | ||
import dev.kord.ksp.isClassifierReference | ||
import kotlinx.serialization.Serializable | ||
|
||
/** [SymbolProcessorProvider] for [OptionalDefaultInspectionProcessor]. */ | ||
class OptionalDefaultInspectionProcessorProvider : SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = | ||
OptionalDefaultInspectionProcessor(environment.logger) | ||
} | ||
|
||
private val OPTIONAL_TYPES = | ||
listOf("Optional", "OptionalBoolean", "OptionalInt", "OptionalLong", "OptionalSnowflake") | ||
.map { "dev.kord.common.entity.optional.$it" } | ||
.toSet() | ||
|
||
/** | ||
* [SymbolProcessor] that verifies that every primary constructor parameter with `Optional` type of a [Serializable] | ||
* class has a default value. | ||
*/ | ||
private class OptionalDefaultInspectionProcessor(private val logger: KSPLogger) : SymbolProcessor { | ||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
resolver.getSymbolsWithAnnotation<Serializable>() | ||
.filterIsInstance<KSClassDeclaration>() | ||
.forEach { it.verifySerializableClassPrimaryConstructor() } | ||
|
||
return emptyList() // we never have to defer any symbols | ||
} | ||
|
||
private fun KSClassDeclaration.verifySerializableClassPrimaryConstructor() { | ||
primaryConstructor?.parameters?.forEach { parameter -> | ||
if (parameter.hasDefault) return@forEach | ||
|
||
val type = parameter.type | ||
if (type.element?.isClassifierReference == false) return@forEach | ||
|
||
val clazz = when (val declaration = type.resolve().declaration) { | ||
is KSTypeParameter -> return@forEach | ||
is KSClassDeclaration -> declaration | ||
is KSTypeAlias -> declaration.findActualType() | ||
else -> error("Unexpected KSDeclaration: $declaration") | ||
} | ||
if (clazz.qualifiedName?.asString() in OPTIONAL_TYPES) { | ||
logger.error("Missing default for parameter ${parameter.name?.asString()}.", symbol = parameter) | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dev.kord.ksp.inspection.OptionalDefaultInspectionProcessorProvider | ||
dev.kord.ksp.kordenum.KordEnumProcessorProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters