-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09ede26
commit b571bbd
Showing
5 changed files
with
66 additions
and
57 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
45 changes: 45 additions & 0 deletions
45
ksp-processors/src/main/kotlin/generation/kordenum/Serializer.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,45 @@ | ||
package dev.kord.ksp.generation.kordenum | ||
|
||
import com.squareup.kotlinpoet.KModifier.OVERRIDE | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import dev.kord.ksp.addFunction | ||
import dev.kord.ksp.addObject | ||
import dev.kord.ksp.addParameter | ||
import dev.kord.ksp.generation.GenerationEntity.KordEnum | ||
import dev.kord.ksp.generation.GenerationEntity.KordEnum.ValueType | ||
import dev.kord.ksp.generation.GenerationEntity.KordEnum.ValueType.INT | ||
import dev.kord.ksp.generation.GenerationEntity.KordEnum.ValueType.STRING | ||
import dev.kord.ksp.generation.shared.GenerationContext | ||
import dev.kord.ksp.generation.shared.addSharedSerializerContent | ||
import dev.kord.ksp.generation.shared.nameWithSuppressedDeprecation | ||
import dev.kord.ksp.withControlFlow | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
private fun ValueType.toEncodingPostfix() = when (this) { | ||
INT -> "Int" | ||
STRING -> "String" | ||
} | ||
|
||
context(KordEnum, GenerationContext) | ||
internal fun TypeSpec.Builder.addSerializer() = addObject("Serializer") { | ||
addSharedSerializerContent(entityCN) | ||
val encodingPostfix = valueType.toEncodingPostfix() | ||
addFunction("serialize") { | ||
addModifiers(OVERRIDE) | ||
addParameter<Encoder>("encoder") | ||
addParameter("value", entityCN) | ||
addStatement("encoder.encode$encodingPostfix(value.$valueName)") | ||
} | ||
addFunction("deserialize") { | ||
addModifiers(OVERRIDE) | ||
returns(entityCN) | ||
addParameter<Decoder>("decoder") | ||
withControlFlow("return when·(val·$valueName·=·decoder.decode$encodingPostfix())") { | ||
for (entry in entriesDistinctByValue) { | ||
addStatement("$valueFormat·->·${entry.nameWithSuppressedDeprecation}", entry.value) | ||
} | ||
addStatement("else·->·Unknown($valueName)") | ||
} | ||
} | ||
} |
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