Skip to content

Commit

Permalink
fix int type, add long type, rename double type (#6041)
Browse files Browse the repository at this point in the history
Co-authored-by: David Motsonashvili <davidmotson@google.com>
  • Loading branch information
davidmotson and David Motsonashvili committed Jun 18, 2024
1 parent 0a8de63 commit 1aebbd3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions firebase-vertexai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Unreleased
* [changed] Breaking Change: renamed `Schema.int` to `Schema.long` and added a 32 bit int type
* [changed] Breaking Change: renamed `Schema.num` to `Schema.double`
* [fixed] Fixed an issue with decoding JSON literals (#6028).

# 16.0.0-beta01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ import org.json.JSONObject
* @see [defineFunction] for how to create an instance of this class.
*/
class NoParameterFunction
internal constructor(
name: String,
description: String,
val function: suspend () -> JSONObject,
) : FunctionDeclaration(name, description) {
internal constructor(name: String, description: String, val function: suspend () -> JSONObject) :
FunctionDeclaration(name, description) {
override fun getParameters() = listOf<Schema<Any>>()

suspend fun execute() = function()
Expand Down Expand Up @@ -185,15 +182,25 @@ class Schema<T>(
fun fromString(value: String?) = type.parse(value)

companion object {
/** Registers a schema for an integer number */
/** Registers a schema for a 32 bit integer number */
fun int(name: String, description: String) =
Schema<Long>(
Schema<Int>(
name = name,
description = description,
format = "int32",
type = FunctionType.INTEGER,
nullable = false,
)

/** Registers a schema for a 64 bit integer number */
fun long(name: String, description: String) =
Schema<Long>(
name = name,
description = description,
type = FunctionType.LONG,
nullable = false,
)

/** Registers a schema for a string */
fun str(name: String, description: String) =
Schema<String>(
Expand All @@ -213,6 +220,10 @@ class Schema<T>(
)

/** Registers a schema for a floating point number */
@Deprecated(
message = "this is being renamed to double",
replaceWith = ReplaceWith("double(name, description)"),
)
fun num(name: String, description: String) =
Schema<Double>(
name = name,
Expand All @@ -221,6 +232,15 @@ class Schema<T>(
nullable = false,
)

/** Registers a schema for a floating point number */
fun double(name: String, description: String) =
Schema<Double>(
name = name,
description = description,
type = FunctionType.NUMBER,
nullable = false,
)

/**
* Registers a schema for a complex object. In a function it will be returned as a [JSONObject]
*/
Expand All @@ -235,6 +255,7 @@ class Schema<T>(

/**
* Registers a schema for an array.
*
* @param items can be used to specify the type of the array
*/
fun arr(name: String, description: String, items: Schema<out Any>? = null) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import org.json.JSONObject
class FunctionType<T>(val name: String, val parse: (String?) -> T?) {
companion object {
val STRING = FunctionType<String>("STRING") { it }
val INTEGER = FunctionType<Long>("INTEGER") { it?.toLongOrNull() }
val INTEGER = FunctionType<Int>("INTEGER") { it?.toIntOrNull() }
val LONG = FunctionType<Long>("INTEGER") { it?.toLongOrNull() }
val NUMBER = FunctionType<Double>("NUMBER") { it?.toDoubleOrNull() }
val BOOLEAN = FunctionType<Boolean>("BOOLEAN") { it?.toBoolean() }
val ARRAY =
Expand Down

0 comments on commit 1aebbd3

Please sign in to comment.