in smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/EnumGenerator.kt [108:159]
fun render() {
writer.renderDocumentation(shape)
writer.renderAnnotations(shape)
// NOTE: The smithy spec only allows string shapes to apply to a string shape at the moment
writer.withBlock("sealed class ${symbol.name} {", "}") {
write("\nabstract val value: #Q\n", KotlinTypes.String)
val sortedDefinitions = enumTrait
.values
.sortedBy { it.name.orElse(it.value) }
sortedDefinitions.forEach {
generateSealedClassVariant(it)
write("")
}
if (generatedNames.contains("SdkUnknown")) throw CodegenException("generating SdkUnknown would cause duplicate variant for enum shape: $shape")
// generate the unknown which will always be last
writer.withBlock("data class SdkUnknown(override val value: #Q) : #Q() {", "}", KotlinTypes.String, symbol) {
renderToStringOverride()
}
write("")
// generate the fromValue() static method
withBlock("companion object {", "}") {
writer.dokka("Convert a raw value to one of the sealed variants or [SdkUnknown]")
openBlock("fun fromValue(str: #Q): #Q = when(str) {", KotlinTypes.String, symbol)
.call {
sortedDefinitions.forEach { definition ->
val variantName = getVariantName(definition)
write("\"${definition.value}\" -> $variantName")
}
}
.write("else -> SdkUnknown(str)")
.closeBlock("}")
.write("")
writer.dokka("Get a list of all possible variants")
openBlock("fun values(): #Q<#Q> = listOf(", KotlinTypes.Collections.List, symbol)
.call {
sortedDefinitions.forEachIndexed { idx, definition ->
val variantName = getVariantName(definition)
val suffix = if (idx < sortedDefinitions.size - 1) "," else ""
write("${variantName}$suffix")
}
}
.closeBlock(")")
}
}
}