Skip to content

Commit 8ffa303

Browse files
author
David Motsonashvili
committed
added JsonSchema type and altered schema generator to output it
1 parent 334f3a3 commit 8ffa303

File tree

4 files changed

+524
-13
lines changed

4 files changed

+524
-13
lines changed

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.squareup.kotlinpoet.CodeBlock
3434
import com.squareup.kotlinpoet.FileSpec
3535
import com.squareup.kotlinpoet.KModifier
3636
import com.squareup.kotlinpoet.ParameterizedTypeName
37+
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
3738
import com.squareup.kotlinpoet.PropertySpec
3839
import com.squareup.kotlinpoet.TypeSpec
3940
import com.squareup.kotlinpoet.ksp.toClassName
@@ -81,7 +82,7 @@ public class SchemaSymbolProcessor(
8182
classDeclaration.packageName.asString(),
8283
"${classDeclaration.simpleName.asString()}GeneratedSchema",
8384
)
84-
.addImport("com.google.firebase.ai.type", "Schema")
85+
.addImport("com.google.firebase.ai.type", "JsonSchema")
8586
.addType(
8687
TypeSpec.classBuilder("${classDeclaration.simpleName.asString()}GeneratedSchema")
8788
.addAnnotation(Generated::class)
@@ -90,7 +91,13 @@ public class SchemaSymbolProcessor(
9091
.addProperty(
9192
PropertySpec.builder(
9293
"SCHEMA",
93-
ClassName("com.google.firebase.ai.type", "Schema"),
94+
ClassName("com.google.firebase.ai.type", "JsonSchema")
95+
.parameterizedBy(
96+
ClassName(
97+
classDeclaration.packageName.asString(),
98+
classDeclaration.simpleName.asString()
99+
)
100+
),
94101
KModifier.PUBLIC,
95102
)
96103
.mutable(false)
@@ -132,32 +139,33 @@ public class SchemaSymbolProcessor(
132139
val minItems = getIntFromAnnotation(guideAnnotation, "minItems")
133140
val maxItems = getIntFromAnnotation(guideAnnotation, "maxItems")
134141
val format = getStringFromAnnotation(guideAnnotation, "format")
142+
val pattern = getStringFromAnnotation(guideAnnotation, "pattern")
135143
val builder = CodeBlock.builder()
136144
when (className.canonicalName) {
137145
"kotlin.Int" -> {
138-
builder.addStatement("Schema.integer(").indent()
146+
builder.addStatement("JsonSchema.integer(").indent()
139147
}
140148
"kotlin.Long" -> {
141-
builder.addStatement("Schema.long(").indent()
149+
builder.addStatement("JsonSchema.long(").indent()
142150
}
143151
"kotlin.Boolean" -> {
144-
builder.addStatement("Schema.boolean(").indent()
152+
builder.addStatement("JsonSchema.boolean(").indent()
145153
}
146154
"kotlin.Float" -> {
147-
builder.addStatement("Schema.float(").indent()
155+
builder.addStatement("JsonSchema.float(").indent()
148156
}
149157
"kotlin.Double" -> {
150-
builder.addStatement("Schema.double(").indent()
158+
builder.addStatement("JsonSchema.double(").indent()
151159
}
152160
"kotlin.String" -> {
153-
builder.addStatement("Schema.string(").indent()
161+
builder.addStatement("JsonSchema.string(").indent()
154162
}
155163
"kotlin.collections.List" -> {
156164
val listTypeParam = type.arguments.first().type!!.resolve()
157165
val listParamCodeBlock =
158166
generateCodeBlockForSchema(type = listTypeParam, parentType = type)
159167
builder
160-
.addStatement("Schema.array(")
168+
.addStatement("JsonSchema.array(")
161169
.indent()
162170
.addStatement("items = ")
163171
.add(listParamCodeBlock)
@@ -172,17 +180,19 @@ public class SchemaSymbolProcessor(
172180
.map { it.simpleName.asString() }
173181
.toList()
174182
builder
175-
.addStatement("Schema.enumeration(")
183+
.addStatement("JsonSchema.enumeration(")
176184
.indent()
185+
.addStatement("clazz = ${type.declaration.qualifiedName!!.asString()}::class.java,")
177186
.addStatement("values = listOf(")
178187
.indent()
179188
.addStatement(enumValues.joinToString { "\"$it\"" })
180189
.unindent()
181190
.addStatement("),")
182191
} else {
183192
builder
184-
.addStatement("Schema.obj(")
193+
.addStatement("JsonSchema.obj(")
185194
.indent()
195+
.addStatement("clazz = ${type.declaration.qualifiedName!!.asString()}::class.java,")
186196
.addStatement("properties = mapOf(")
187197
.indent()
188198
val properties =
@@ -229,9 +239,9 @@ public class SchemaSymbolProcessor(
229239
"${parentType?.toClassName()?.simpleName?.let { "$it." }}$name is not a List type, minItems and maxItems are not valid parameters to specify in @Guide"
230240
)
231241
}
232-
if (format != null && className.canonicalName != "kotlin.String") {
242+
if ((format != null || pattern != null) && className.canonicalName != "kotlin.String") {
233243
logger.warn(
234-
"${parentType?.toClassName()?.simpleName?.let { "$it." }}$name is not a String type, format is not a valid parameter to specify in @Guide"
244+
"${parentType?.toClassName()?.simpleName?.let { "$it." }}$name is not a String type, format and pattern are not a valid parameter to specify in @Guide"
235245
)
236246
}
237247
if (minimum != null) {
@@ -249,6 +259,9 @@ public class SchemaSymbolProcessor(
249259
if (format != null) {
250260
builder.addStatement("format = %S,", format)
251261
}
262+
if (pattern != null) {
263+
builder.addStatement("pattern = %S,", pattern)
264+
}
252265
builder.addStatement("nullable = %L)", className.isNullable).unindent()
253266
return builder.build()
254267
}

firebase-ai/src/main/kotlin/com/google/firebase/ai/annotations/Guide.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ public annotation class Guide(
2525
public val minItems: Int = -1,
2626
public val maxItems: Int = -1,
2727
public val format: String = "",
28+
public val pattern: String = "",
2829
)

0 commit comments

Comments
 (0)