Skip to content

Commit 334f3a3

Browse files
author
David Motsonashvili
committed
Add support for enum schema
1 parent 8624f43 commit 334f3a3

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

firebase-ai-ksp-processor/src/main/kotlin/com/google/firebase/ai/ksp/SchemaSymbolProcessor.kt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.google.devtools.ksp.processing.Dependencies
2222
import com.google.devtools.ksp.processing.KSPLogger
2323
import com.google.devtools.ksp.processing.Resolver
2424
import com.google.devtools.ksp.processing.SymbolProcessor
25+
import com.google.devtools.ksp.symbol.ClassKind
2526
import com.google.devtools.ksp.symbol.KSAnnotated
2627
import com.google.devtools.ksp.symbol.KSAnnotation
2728
import com.google.devtools.ksp.symbol.KSClassDeclaration
@@ -151,17 +152,33 @@ public class SchemaSymbolProcessor(
151152
"kotlin.String" -> {
152153
builder.addStatement("Schema.string(").indent()
153154
}
155+
"kotlin.collections.List" -> {
156+
val listTypeParam = type.arguments.first().type!!.resolve()
157+
val listParamCodeBlock =
158+
generateCodeBlockForSchema(type = listTypeParam, parentType = type)
159+
builder
160+
.addStatement("Schema.array(")
161+
.indent()
162+
.addStatement("items = ")
163+
.add(listParamCodeBlock)
164+
.addStatement(",")
165+
}
154166
else -> {
155-
if (className.canonicalName == "kotlin.collections.List") {
156-
val listTypeParam = type.arguments.first().type!!.resolve()
157-
val listParamCodeBlock =
158-
generateCodeBlockForSchema(type = listTypeParam, parentType = type)
167+
if ((type.declaration as? KSClassDeclaration)?.classKind == ClassKind.ENUM_CLASS) {
168+
val enumValues =
169+
(type.declaration as KSClassDeclaration)
170+
.declarations
171+
.filterIsInstance(KSClassDeclaration::class.java)
172+
.map { it.simpleName.asString() }
173+
.toList()
159174
builder
160-
.addStatement("Schema.array(")
175+
.addStatement("Schema.enumeration(")
176+
.indent()
177+
.addStatement("values = listOf(")
161178
.indent()
162-
.addStatement("items = ")
163-
.add(listParamCodeBlock)
164-
.addStatement(",")
179+
.addStatement(enumValues.joinToString { "\"$it\"" })
180+
.unindent()
181+
.addStatement("),")
165182
} else {
166183
builder
167184
.addStatement("Schema.obj(")

0 commit comments

Comments
 (0)