Skip to content

Commit c44efc8

Browse files
authored
feat: add Java-friendly convenience methods for JsonValue and tool response output (#9)
1 parent df75691 commit c44efc8

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

arcade-java-core/src/main/kotlin/dev/arcade/core/Values.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,42 @@ sealed class JsonValue : JsonField<Nothing>() {
274274

275275
fun <R : Any> convert(type: Class<R>): R? = JSON_MAPPER.convertValue(this, type)
276276

277+
// -------------------------------------------------------------------------
278+
// Start of manually added code
279+
// -------------------------------------------------------------------------
280+
281+
/**
282+
* Returns this value's map representation, or an empty map if this value is not a JSON object.
283+
*
284+
* Example usage:
285+
* ```java
286+
* Map<String, JsonValue> map = jsonValue.toMapOrEmpty();
287+
* ```
288+
*/
289+
fun toMapOrEmpty(): Map<String, JsonValue> =
290+
when (this) {
291+
is JsonObject -> values
292+
else -> emptyMap()
293+
}
294+
295+
/**
296+
* Returns this value's list representation, or an empty list if this value is not a JSON array.
297+
*
298+
* Example usage:
299+
* ```java
300+
* List<JsonValue> items = jsonValue.toListOrEmpty();
301+
* ```
302+
*/
303+
fun toListOrEmpty(): List<JsonValue> =
304+
when (this) {
305+
is JsonArray -> values
306+
else -> emptyList()
307+
}
308+
309+
// -------------------------------------------------------------------------
310+
// End of manually added code
311+
// -------------------------------------------------------------------------
312+
277313
/** Returns the result of calling the [visitor] method corresponding to this value's variant. */
278314
fun <R> accept(visitor: Visitor<R>): R =
279315
when (this) {

arcade-java-core/src/main/kotlin/dev/arcade/models/tools/ExecuteToolRequest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ private constructor(
216216
/** JSON input to the tool, if any */
217217
fun input(input: Input) = input(JsonField.of(input))
218218

219+
/** JSON input to the tool, if any */
220+
fun input(input: Map<String, Any?>) = input(Input.from(input))
221+
219222
/**
220223
* Sets [Builder.input] to an arbitrary JSON value.
221224
*
@@ -359,6 +362,11 @@ private constructor(
359362

360363
/** Returns a mutable builder for constructing an instance of [Input]. */
361364
@JvmStatic fun builder() = Builder()
365+
366+
/** Converts a Map of input objects to an [Input] Map<String, JsonValue>. */
367+
@JvmStatic
368+
fun from(input: Map<String, Any?>) =
369+
Input(input.mapValues { (_, value) -> JsonValue.from(value) })
362370
}
363371

364372
/** A builder for [Input]. */

arcade-java-core/src/main/kotlin/dev/arcade/models/tools/ExecuteToolResponse.kt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import com.fasterxml.jackson.annotation.JsonCreator
88
import com.fasterxml.jackson.annotation.JsonProperty
99
import dev.arcade.core.Enum
1010
import dev.arcade.core.ExcludeMissing
11+
import dev.arcade.core.JsonArray
1112
import dev.arcade.core.JsonField
1213
import dev.arcade.core.JsonMissing
14+
import dev.arcade.core.JsonObject
1315
import dev.arcade.core.JsonValue
1416
import dev.arcade.core.checkKnown
1517
import dev.arcade.core.checkRequired
@@ -467,6 +469,78 @@ private constructor(
467469
*/
468470
@JsonProperty("value") @ExcludeMissing fun _value(): JsonValue = value
469471

472+
// -------------------------------------------------------------------------
473+
// Start of manually added code
474+
// -------------------------------------------------------------------------
475+
476+
/**
477+
* Returns an [Optional] containing the output value as a `Map<String, JsonValue>`, or an
478+
* empty [Optional] if the value is not a JSON object.
479+
*
480+
* Example usage:
481+
* ```java
482+
* Map<String, JsonValue> result = response.output()
483+
* .flatMap(Output::valueAsObject)
484+
* .orElse(Map.of());
485+
* ```
486+
*/
487+
fun valueAsObject(): Optional<Map<String, JsonValue>> =
488+
when (value) {
489+
is JsonObject -> Optional.of(value.values)
490+
else -> Optional.empty()
491+
}
492+
493+
/**
494+
* Returns the output value as a `Map<String, JsonValue>`, or an empty map if the value is
495+
* not a JSON object.
496+
*
497+
* Example usage:
498+
* ```java
499+
* Map<String, JsonValue> result = output.valueAsObjectOrEmpty();
500+
* ```
501+
*/
502+
fun valueAsObjectOrEmpty(): Map<String, JsonValue> =
503+
when (value) {
504+
is JsonObject -> value.values
505+
else -> emptyMap()
506+
}
507+
508+
/**
509+
* Returns an [Optional] containing the output value as a `List<JsonValue>`, or an empty
510+
* [Optional] if the value is not a JSON array.
511+
*
512+
* Example usage:
513+
* ```java
514+
* List<JsonValue> items = response.output()
515+
* .flatMap(Output::valueAsArray)
516+
* .orElse(List.of());
517+
* ```
518+
*/
519+
fun valueAsArray(): Optional<List<JsonValue>> =
520+
when (value) {
521+
is JsonArray -> Optional.of(value.values)
522+
else -> Optional.empty()
523+
}
524+
525+
/**
526+
* Returns the output value as a `List<JsonValue>`, or an empty list if the value is not a
527+
* JSON array.
528+
*
529+
* Example usage:
530+
* ```java
531+
* List<JsonValue> items = output.valueAsArrayOrEmpty();
532+
* ```
533+
*/
534+
fun valueAsArrayOrEmpty(): List<JsonValue> =
535+
when (value) {
536+
is JsonArray -> value.values
537+
else -> emptyList()
538+
}
539+
540+
// -------------------------------------------------------------------------
541+
// End of manually added code
542+
// -------------------------------------------------------------------------
543+
470544
/**
471545
* Returns the raw JSON value of [authorization].
472546
*

0 commit comments

Comments
 (0)