Skip to content

Commit

Permalink
Add descriptions for tokens and known values.
Browse files Browse the repository at this point in the history
  • Loading branch information
christiandeange committed Nov 11, 2024
1 parent ff32d4f commit e260d72
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.withIndent
import org.gradle.configurationcache.extensions.capitalized
import sh.christian.ozone.api.generator.builder.EnumClass
import sh.christian.ozone.api.generator.builder.EnumEntry
import sh.christian.ozone.api.generator.builder.GeneratorContext
import sh.christian.ozone.api.generator.builder.LexiconDataClassesGenerator
import sh.christian.ozone.api.generator.builder.SealedRelationship
Expand All @@ -31,7 +33,7 @@ class LexiconClassFileCreator(
private val sealedRelationships = mutableListOf<SealedRelationship>()

fun createClassForLexicon(document: LexiconDocument) {
val enums = mutableMapOf<ClassName, MutableSet<String>>()
val enums = mutableMapOf<EnumClass, MutableSet<EnumEntry>>()

document.defs.forEach { (defKey, defValue) ->
val definitionName = if (defKey == "main") "" else defKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ private constructor(
val procedureName: String = document.id.substringAfterLast('.').removePrefix("defs")
val classPrefix: String = prefixOverride ?: procedureName.capitalized()

private val enums = mutableMapOf<ClassName, MutableSet<String>>()
private val enums = mutableMapOf<EnumClass, MutableSet<EnumEntry>>()
private val types = mutableMapOf<ClassName, TypeSpec>()
private val typeAliases = mutableMapOf<ClassName, TypeAliasSpec>()
private val sealedRelationships = mutableListOf<SealedRelationship>()

fun addEnum(
className: ClassName,
classDescription: String? = null,
enumName: String,
enumDescription: String? = null,
) {
enums.getOrPut(className) { mutableSetOf() } += enumName
enums.getOrPut(EnumClass(className, classDescription)) { mutableSetOf() } +=
EnumEntry(enumName, enumDescription)
}

fun addType(typeSpec: TypeSpec) {
Expand All @@ -57,7 +60,7 @@ private constructor(
)
}

fun enums(): Map<ClassName, Set<String>> {
fun enums(): Map<EnumClass, Set<EnumEntry>> {
return enums.mapValues { it.value.toSet() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class LexiconDataClassesGenerator(

val className = ClassName(context.authority, simpleClassName)
primitive.knownValues.forEach { enum ->
context.addEnum(className, enum)
context.addEnum(className, primitive.description, enum, null)
}
return className
}
Expand All @@ -194,7 +194,7 @@ class LexiconDataClassesGenerator(
) {
val className = ClassName(context.authority, context.classPrefix + "Token")
val enumName = context.document.id + "#" + context.definitionName
context.addEnum(className, enumName)
context.addEnum(className, null, enumName, token.description)
}

private fun generateTypes(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sh.christian.ozone.api.generator.builder

import com.squareup.kotlinpoet.ClassName

data class EnumClass(
val className: ClassName,
val description: String?,
)

data class EnumEntry(
val name: String,
val description: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -217,28 +217,26 @@ fun createObjectClass(
}

fun createOpenEnumClass(
className: ClassName,
values: Collection<String>,
enumClass: EnumClass,
entries: Collection<EnumEntry>,
): List<TypeSpec> {
val className = enumClass.className
val serializerClassName = className.peerClass(className.simpleName + "Serializer")
val serializerTypeSpec = TypeSpec.classBuilder(serializerClassName)
.addSuperinterface(
TypeNames.KSerializer.parameterizedBy(className),
CodeBlock.of("%M(%T::safeValueOf)".trimIndent(), stringEnumSerializer, className)
CodeBlock.of("%M(%T::safeValueOf)".trimIndent(), stringEnumSerializer, className)
)
.build()

val formattedNames = values.associateWith { value ->
value.substringAfterLast('#').toPascalCase()
}

val sealedClass = TypeSpec.classBuilder(className)
.addModifiers(KModifier.SEALED)
.addAnnotation(
AnnotationSpec.builder(TypeNames.Serializable)
.addMember("with = %T::class", serializerClassName)
.build()
)
.addDescription(enumClass.description)
.superclass(TypeNames.AtpEnum)
.primaryConstructor(
FunSpec.constructorBuilder()
Expand All @@ -254,20 +252,23 @@ fun createOpenEnumClass(
val safeValueOfControlFlow = CodeBlock.builder()
.beginControlFlow("return when (value)")

formattedNames.forEach { (value, formatted) ->
safeValueOfControlFlow.addStatement("%S -> %L", value, formatted)
entries.forEach { entry ->
val entryClassName = entry.name.substringAfterLast('#').toPascalCase()

safeValueOfControlFlow.addStatement("%S -> %L", entry.name, entryClassName)
sealedClass.addType(
TypeSpec.objectBuilder(formatted)
TypeSpec.objectBuilder(entryClassName)
.addModifiers(KModifier.DATA)
.addDescription(entry.description)
.superclass(className)
.addSuperclassConstructorParameter("%S", value)
.addSuperclassConstructorParameter("%S", entry.name)
.build()
)
}

// Avoid conflicting names by appending _ to the unknown name
val safeUnknownEntryName = "Unknown".let {
if (it in formattedNames.values) "_$it" else it
if (it in sealedClass.typeSpecs.map { type -> type.name }) "_$it" else it
}

sealedClass.addType(
Expand Down

0 comments on commit e260d72

Please sign in to comment.