Skip to content

wip: Schema.fromEnum() #6968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions firebase-ai/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,9 @@ package com.google.firebase.ai.type {
method public static com.google.firebase.ai.type.Schema enumeration(java.util.List<java.lang.String> values);
method public static com.google.firebase.ai.type.Schema enumeration(java.util.List<java.lang.String> values, String? description = null);
method public static com.google.firebase.ai.type.Schema enumeration(java.util.List<java.lang.String> values, String? description = null, boolean nullable = false);
method public static <E extends java.lang.Enum<E>> com.google.firebase.ai.type.Schema fromEnum(Class<E> enumClass);
method public static <E extends java.lang.Enum<E>> com.google.firebase.ai.type.Schema fromEnum(Class<E> enumClass, String? description = null);
method public static <E extends java.lang.Enum<E>> com.google.firebase.ai.type.Schema fromEnum(Class<E> enumClass, String? description = null, boolean nullable = false);
method public String? getDescription();
method public java.util.List<java.lang.String>? getEnum();
method public String? getFormat();
Expand Down Expand Up @@ -823,6 +826,10 @@ package com.google.firebase.ai.type {
method public com.google.firebase.ai.type.Schema enumeration(java.util.List<java.lang.String> values);
method public com.google.firebase.ai.type.Schema enumeration(java.util.List<java.lang.String> values, String? description = null);
method public com.google.firebase.ai.type.Schema enumeration(java.util.List<java.lang.String> values, String? description = null, boolean nullable = false);
method public <E extends java.lang.Enum<E>> com.google.firebase.ai.type.Schema fromEnum(Class<E> enumClass);
method public <E extends java.lang.Enum<E>> com.google.firebase.ai.type.Schema fromEnum(Class<E> enumClass, String? description = null);
method public <E extends java.lang.Enum<E>> com.google.firebase.ai.type.Schema fromEnum(Class<E> enumClass, String? description = null, boolean nullable = false);
method public inline <reified E extends java.lang.Enum<E>> com.google.firebase.ai.type.Schema fromEnum(String? description = null, boolean nullable = false);
method public com.google.firebase.ai.type.Schema numDouble();
method public com.google.firebase.ai.type.Schema numDouble(String? description = null);
method public com.google.firebase.ai.type.Schema numDouble(String? description = null, boolean nullable = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.firebase.ai.type

import java.util.EnumSet
import kotlinx.serialization.Serializable

public abstract class StringFormat private constructor(internal val value: String) {
Expand Down Expand Up @@ -179,7 +180,11 @@ internal constructor(
): Schema {
if (!properties.keys.containsAll(optionalProperties)) {
throw IllegalArgumentException(
"All optional properties must be present in properties. Missing: ${optionalProperties.minus(properties.keys)}"
"All optional properties must be present in properties. Missing: ${
optionalProperties.minus(
properties.keys
)
}"
)
}
return Schema(
Expand Down Expand Up @@ -239,6 +244,49 @@ internal constructor(
enum = values,
type = "STRING",
)

/**
* Returns a [Schema] for the given Kotlin Enum.
*
* For example, the cardinal directions can be represented as:
*
* ```
* enum class CardinalDirection { NORTH, EAST, SOUTH, WEST }
*
* Schema.fromEnum<CardinalDirection>()
* ```
*
* @param description The description of what the parameter should contain or represent
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
*/
@JvmOverloads
public inline fun <reified E : Enum<E>> fromEnum(
description: String? = null,
nullable: Boolean = false
): Schema = enumeration(enumValues<E>().map { it.toString() }, description, nullable)

/**
* Returns a [Schema] for the given Java Enum.
*
* For example, the cardinal directions can be represented as:
*
* ```
* enum CardinalDirection { NORTH, EAST, SOUTH, WEST }
*
* Schema.fromEnum(CardinalDirection.class);
* ```
*
* @param enumClass The Enum's Java class.
* @param description The description of what the parameter should contain or represent
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
*/
@JvmStatic
@JvmOverloads
public fun <E : Enum<E>> fromEnum(
enumClass: Class<E>,
description: String? = null,
nullable: Boolean = false
): Schema = enumeration(EnumSet.allOf(enumClass).map { it.name }, description, nullable)
}

internal fun toInternal(): Internal =
Expand All @@ -252,6 +300,7 @@ internal constructor(
required,
items?.toInternal(),
)

@Serializable
internal data class Internal(
val type: String,
Expand Down
76 changes: 76 additions & 0 deletions firebase-ai/src/test/java/com/google/firebase/ai/SchemaTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,80 @@ internal class SchemaTests {

Json.encodeToString(schemaDeclaration.toInternal()).shouldEqualJson(expectedJson)
}

enum class TestEnum {
RED,
GREEN,
BLUE
}

enum class TestEnumWithValues(val someValue: String) {
RED("FF0000"),
GREEN("00FF00"),
BLUE("0000FF")
}

@Test
fun `basic Kotlin enum class`() {
val schema = Schema.fromEnum<TestEnum>()
val expectedJson =
"""
{
"type": "STRING",
"format": "enum",
"enum": ["RED", "GREEN", "BLUE"]
}
"""
.trimIndent()

Json.encodeToString(schema.toInternal()).shouldEqualJson(expectedJson)
}

@Test
fun `basic Java enum`() {
val schema = Schema.fromEnum(TestEnum::class.java)
val expectedJson =
"""
{
"type": "STRING",
"format": "enum",
"enum": ["RED", "GREEN", "BLUE"]
}
"""
.trimIndent()

Json.encodeToString(schema.toInternal()).shouldEqualJson(expectedJson)
}

@Test
fun `Kotlin enum with values`() {
val schema = Schema.fromEnum<TestEnumWithValues>()
val expectedJson =
"""
{
"type": "STRING",
"format": "enum",
"enum": ["RED", "GREEN", "BLUE"]
}
"""
.trimIndent()

Json.encodeToString(schema.toInternal()).shouldEqualJson(expectedJson)
}

@Test
fun `Java enum with values`() {
val schema = Schema.fromEnum(TestEnumWithValues::class.java)
val expectedJson =
"""
{
"type": "STRING",
"format": "enum",
"enum": ["RED", "GREEN", "BLUE"]
}
"""
.trimIndent()

Json.encodeToString(schema.toInternal()).shouldEqualJson(expectedJson)
}
}