Skip to content

Commit

Permalink
Adapt the function dispatch result to allow for automatic Any unpacking
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 647784646
  • Loading branch information
l46kok authored and copybara-github committed Jun 28, 2024
1 parent 0e2fd64 commit db83a47
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
9 changes: 5 additions & 4 deletions runtime/src/main/java/dev/cel/runtime/DefaultInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,11 @@ private IntermediateResult evalCall(ExecutionFrame frame, CelExpr expr, CelCall

Object[] argArray = Arrays.stream(argResults).map(IntermediateResult::value).toArray();

return IntermediateResult.create(
attr,
Object dispatchResult =
dispatcher.dispatch(
metadata, expr.id(), callExpr.function(), reference.overloadIds(), argArray));
metadata, expr.id(), callExpr.function(), reference.overloadIds(), argArray);
dispatchResult = typeProvider.adapt(dispatchResult);
return IntermediateResult.create(attr, dispatchResult);
}

private Optional<CelAttribute> maybeContainerIndexAttribute(
Expand Down Expand Up @@ -717,7 +718,7 @@ private IntermediateResult evalList(ExecutionFrame frame, CelExpr unusedExpr, Ce
Object value = evaluatedElement.value();
if (!optionalIndicesSet
.isEmpty() // Performance optimization to prevent autoboxing when there's no
// optionals.
// optionals.
&& optionalIndicesSet.contains(i)
&& !isUnknownValue(value)) {
Optional<?> optionalVal = (Optional<?>) value;
Expand Down
59 changes: 59 additions & 0 deletions runtime/src/test/resources/packUnpackAny.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {d=seconds: 100
} +> {any=type_url: "type.googleapis.com/google.protobuf.Duration"
Expand All @@ -25,6 +28,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {message=single_any {
type_url: "type.googleapis.com/google.protobuf.Duration"
Expand All @@ -45,6 +51,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {message=single_any {
type_url: "type.googleapis.com/google.protobuf.Duration"
Expand All @@ -64,6 +73,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {any=single_int64: 1
}
Expand All @@ -79,12 +91,38 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {any=type_url: "type.googleapis.com/google.protobuf.Int64Value"
value: "\b\001"
}
result: true

Source: list[0] == message
declare any {
value any
}
declare d {
value google.protobuf.Duration
}
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {list=[type_url: "type.googleapis.com/dev.cel.testing.testdata.proto3.TestAllTypes"
value: "\242\0062\n,type.googleapis.com/google.protobuf.Duration\022\002\bd"
], message=single_any {
type_url: "type.googleapis.com/google.protobuf.Duration"
value: "\bd"
}
}
result: true

Source: TestAllTypes{single_any: d}
declare any {
value any
Expand All @@ -95,6 +133,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {d=seconds: 100
}
Expand All @@ -114,6 +155,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {message=single_int64: -1
}
Expand All @@ -133,6 +177,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {message=single_uint64: 1
}
Expand All @@ -152,6 +199,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {}
result: single_any {
Expand All @@ -170,6 +220,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {}
result: single_any {
Expand All @@ -188,6 +241,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {}
result: single_any {
Expand All @@ -206,6 +262,9 @@ declare d {
declare message {
value dev.cel.testing.testdata.proto3.TestAllTypes
}
declare list {
value list(dyn)
}
=====>
bindings: {message=single_bytes: "happy"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ public void packUnpackAny() throws Exception {
declareVariable("any", CelTypes.ANY);
declareVariable("d", CelTypes.DURATION);
declareVariable("message", CelTypes.createMessage(TestAllTypes.getDescriptor().getFullName()));
declareVariable("list", CelTypes.createList(CelTypes.DYN));
Duration duration = Durations.fromSeconds(100);
Any any = Any.pack(duration);
TestAllTypes message = TestAllTypes.newBuilder().setSingleAny(any).build();
Expand All @@ -584,6 +585,10 @@ public void packUnpackAny() throws Exception {
runTest(Activation.of("any", TestAllTypes.newBuilder().setSingleInt64(1).build()));
source = "any == 1";
runTest(Activation.of("any", Any.pack(Int64Value.of(1))));
source = "list[0] == message";
runTest(
Activation.copyOf(
ImmutableMap.of("list", ImmutableList.of(Any.pack(message)), "message", message)));

// pack any
source = "TestAllTypes{single_any: d}";
Expand Down

0 comments on commit db83a47

Please sign in to comment.