Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
* <li>A primitive or boxed number or boolean, also in a primitive array other than {@code byte[]}
* (base64) or as a map key, accepts only its own JSON type or the same value quoted ({@code
* "3"}). An integer also accepts a whole number such as {@code 3.0}, and a char only one
* character of text.
* character of text. Inside a record or class, an {@code OptionalInt}, {@code OptionalLong}
* or {@code OptionalDouble} reads its value the same way, and null as empty.
* <li>An enum or boolean reads a number only where Jackson writes it as one.
* <li>A port of {@code Event} or of a {@code com.google.genai.types} type such as {@code
* Content}, or of a list, map or array of them, rejects unknown keys anywhere in its value.
Expand Down
68 changes: 63 additions & 5 deletions core/src/main/java/com/google/adk/workflow/PortScalars.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@
import java.io.IOException;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.function.Function;
import java.util.function.LongFunction;
import java.util.regex.Pattern;
import org.jspecify.annotations.Nullable;

/**
* Number, boolean and char readers, also for primitive arrays and map keys. Each accepts a value's
* own JSON type, the same value quoted, and for an integer type a whole number such as {@code
* "1e2"}; Jackson's coercion configs cannot express this. {@code BigInteger}, {@code BigDecimal},
* {@code Number}, {@code OptionalInt}, {@code OptionalLong}, {@code OptionalDouble} and the {@code
* Atomic} types keep Jackson's readers.
* Number, boolean and char readers, also for primitive arrays, map keys and {@code OptionalInt},
* {@code OptionalLong} and {@code OptionalDouble}. Each accepts a value's own JSON type, the same
* value quoted, and for an integer type a whole number such as {@code "1e2"}; Jackson's coercion
* configs cannot express this. {@code BigInteger}, {@code BigDecimal}, {@code Number} and the
* {@code Atomic} types keep Jackson's readers.
*/
final class PortScalars {

Expand Down Expand Up @@ -73,6 +76,20 @@ static SimpleModule module() {
addArray(module, double[].class);
addArray(module, float[].class);
addArray(module, boolean[].class);
addOptional(
module, OptionalInt.class, whole(n -> (int) n), OptionalInt::of, OptionalInt.empty());
addOptional(
module,
OptionalLong.class,
PortScalars::integerLiteral,
OptionalLong::of,
OptionalLong.empty());
addOptional(
module,
OptionalDouble.class,
PortScalars::doubleLiteral,
OptionalDouble::of,
OptionalDouble.empty());
return module;
}

Expand All @@ -87,6 +104,15 @@ private static <T> void add(
module.addKeyDeserializer(boxed, new ScalarKeyDeserializer(boxed, reader));
}

private static <T, E> void addOptional(
SimpleModule module,
Class<T> type,
Function<@Nullable Object, @Nullable E> reader,
Function<E, T> wrap,
T empty) {
module.addDeserializer(type, new PrimitiveOptionalDeserializer<>(type, reader, wrap, empty));
}

private static <A> void addArray(SimpleModule module, Class<A> type) {
module.addDeserializer(type, new ArrayDeserializer<>(type));
}
Expand Down Expand Up @@ -308,4 +334,36 @@ public A getEmptyValue(DeserializationContext ctxt) {
return type.cast(Array.newInstance(type.getComponentType(), 0));
}
}

/**
* Reads an {@code OptionalInt}, {@code OptionalLong} or {@code OptionalDouble}: null as empty.
*/
private static final class PrimitiveOptionalDeserializer<T, E> extends StdScalarDeserializer<T> {
private final Function<@Nullable Object, @Nullable E> reader;
private final Function<E, T> wrap;
private final T empty;

PrimitiveOptionalDeserializer(
Class<T> type,
Function<@Nullable Object, @Nullable E> reader,
Function<E, T> wrap,
T empty) {
super(type);
this.reader = reader;
this.wrap = wrap;
this.empty = empty;
}

@Override
@SuppressWarnings("unchecked") // handleUnexpectedToken returns a T or throws.
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
E value = reader.apply(scalarValue(p));
return value != null ? wrap.apply(value) : (T) ctxt.handleUnexpectedToken(handledType(), p);
}

@Override
public T getNullValue(DeserializationContext ctxt) {
return empty;
}
}
}
31 changes: 30 additions & 1 deletion core/src/test/java/com/google/adk/workflow/FunctionNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CancellationException;
Expand Down Expand Up @@ -5642,6 +5645,7 @@ record Reading(int count, double ratio, boolean flag) {}
record Tally(List<Integer> counts, Map<String, Boolean> flags) {}
record Blank(@JsonSetter(nulls = Nulls.AS_EMPTY) int n) {}
record Keyed(Map<Integer, String> byId) {}
record Optionals(OptionalInt n, OptionalLong l, OptionalDouble d) {}
ImmutableList<Case> cases =
ImmutableList.of(
new Case("3", int.class, "Integer:3"),
Expand Down Expand Up @@ -5720,7 +5724,32 @@ record Keyed(Map<Integer, String> byId) {}
ImmutableMap.of("byId", ImmutableMap.of("3.0", "a")),
Keyed.class,
"Keyed:Keyed[byId={3=a}]"),
new Case(ImmutableMap.of("byId", ImmutableMap.of("003", "a")), Keyed.class, "fails"));
new Case(ImmutableMap.of("byId", ImmutableMap.of("003", "a")), Keyed.class, "fails"),
// OptionalInt, OptionalLong and OptionalDouble read as int, long and double; null is
// empty.
new Case(
ImmutableMap.of("n", 2.0, "l", 2.0),
Optionals.class,
"Optionals:Optionals[n=OptionalInt[2], l=OptionalLong[2], d=OptionalDouble.empty]"),
new Case(
ImmutableMap.of("n", "3", "l", "1e2", "d", "2.5"),
Optionals.class,
"Optionals:Optionals[n=OptionalInt[3], l=OptionalLong[100],"
+ " d=OptionalDouble[2.5]]"),
new Case(ImmutableMap.of("n", 3_000_000_000L), Optionals.class, "fails"),
new Case(
ImmutableMap.of("l", 3_000_000_000L),
Optionals.class,
"Optionals:Optionals[n=OptionalInt.empty, l=OptionalLong[3000000000],"
+ " d=OptionalDouble.empty]"),
new Case(ImmutableMap.of("n", "003"), Optionals.class, "fails"),
new Case(ImmutableMap.of("l", "003"), Optionals.class, "fails"),
new Case(ImmutableMap.of("d", "NaN"), Optionals.class, "fails"),
new Case(
JsonNodeFactory.instance.objectNode().putNull("n"),
Optionals.class,
"Optionals:Optionals[n=OptionalInt.empty, l=OptionalLong.empty,"
+ " d=OptionalDouble.empty]"));

// Act
ImmutableMap<String, Object> actual = readAll(cases);
Expand Down
Loading