Skip to content

Commit

Permalink
build(generator): ast enum content for numbers
Browse files Browse the repository at this point in the history
Take into account cases, when enum value can be a number.
  • Loading branch information
SpaiR committed Aug 11, 2024
1 parent 588903f commit 7b8ca02
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions buildSrc/src/main/kotlin/tool/generator/api/ast_content.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ fun astEnumContent(markerAnnotation: CtAnnotation<*>): List<String> {

val file = markerAnnotation.getValueAsString(A_VALUE_FILE)!!
val qualType = markerAnnotation.getValueAsString(A_VALUE_QUAL_TYPE)!!
val sanitizeName = markerAnnotation.getValueAsString(A_VALUE_SANITIZE_NAME)?.takeIf(String::isNotEmpty) ?: qualType
val astRoot = AstParser.readFromResource(file)

val enums = astRoot.decls.filterDecls0 {
Expand All @@ -24,7 +23,7 @@ fun astEnumContent(markerAnnotation: CtAnnotation<*>): List<String> {

f.setModifiers<Nothing>(setOf(ModifierKind.PUBLIC, ModifierKind.STATIC, ModifierKind.FINAL))
f.setType<Nothing>(factory.createTypeParam("int"))
f.setSimpleName<Nothing>(e.name.replace(sanitizeName, ""))
f.setSimpleName<Nothing>(sanitizeName(markerAnnotation.getValueSanitizeName(qualType), e.name))
buildString {
if (e.docComment != null) {
appendLine(e.docComment)
Expand All @@ -49,6 +48,18 @@ fun astEnumContent(markerAnnotation: CtAnnotation<*>): List<String> {
return result
}

private fun CtAnnotation<*>.getValueSanitizeName(default: String): String {
return getValueAsString(A_VALUE_SANITIZE_NAME)?.takeIf(String::isNotEmpty) ?: default
}

private fun sanitizeName(aValueSanitizeName: String, name: String): String {
var result = name.replace(aValueSanitizeName, "").trim()
if (result.toIntOrNull() != null) {
result = "_$result"
}
return result
}

private fun sanitizeDocComment(doc: String): String {
return doc
.replace("*/", "* /")
Expand Down

0 comments on commit 7b8ca02

Please sign in to comment.