Skip to content

Commit 611017f

Browse files
l46kokcopybara-github
authored andcommitted
Allow google.protobuf.Timestamp and google.protobuf.Duration to be created for planner
PiperOrigin-RevId: 872117591
1 parent a50bbcf commit 611017f

5 files changed

Lines changed: 23 additions & 19 deletions

File tree

common/src/main/java/dev/cel/common/values/ProtoCelValueConverter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,15 @@ public Object fromProtoMessageFieldToCelValue(Message message, FieldDescriptor f
151151

152152
return toRuntimeValue(result);
153153
case UINT32:
154-
return UnsignedLong.valueOf((int) result);
154+
if (!fieldDescriptor.isRepeated()) {
155+
return UnsignedLong.valueOf((int) result);
156+
}
157+
break;
155158
case UINT64:
156-
return UnsignedLong.fromLongBits((long) result);
159+
if (!fieldDescriptor.isRepeated()) {
160+
return UnsignedLong.fromLongBits((long) result);
161+
}
162+
break;
157163
default:
158164
break;
159165
}

runtime/src/main/java/dev/cel/runtime/planner/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ java_library(
297297
deps = [
298298
":execution_frame",
299299
":planned_interpretable",
300-
"//common/types",
300+
"//common/types:type_providers",
301301
"//common/values",
302302
"//common/values:cel_value_provider",
303303
"//runtime:evaluation_exception",

runtime/src/main/java/dev/cel/runtime/planner/EvalCreateStruct.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package dev.cel.runtime.planner;
1616

1717
import com.google.errorprone.annotations.Immutable;
18-
import dev.cel.common.types.StructType;
18+
import dev.cel.common.types.CelType;
1919
import dev.cel.common.values.CelValueProvider;
2020
import dev.cel.common.values.StructValue;
2121
import dev.cel.runtime.CelEvaluationException;
@@ -28,7 +28,7 @@
2828
final class EvalCreateStruct extends PlannedInterpretable {
2929

3030
private final CelValueProvider valueProvider;
31-
private final StructType structType;
31+
private final CelType structType;
3232

3333
// Array contents are not mutated
3434
@SuppressWarnings("Immutable")
@@ -50,7 +50,8 @@ public Object eval(GlobalResolver resolver, ExecutionFrame frame) throws CelEval
5050
Object value =
5151
valueProvider
5252
.newValue(structType.name(), Collections.unmodifiableMap(fieldValues))
53-
.orElseThrow(() -> new IllegalArgumentException("Type name not found: " + structType));
53+
.orElseThrow(
54+
() -> new IllegalArgumentException("Type name not found: " + structType.name()));
5455

5556
if (value instanceof StructValue) {
5657
return ((StructValue) value).value();
@@ -62,7 +63,7 @@ public Object eval(GlobalResolver resolver, ExecutionFrame frame) throws CelEval
6263
static EvalCreateStruct create(
6364
long exprId,
6465
CelValueProvider valueProvider,
65-
StructType structType,
66+
CelType structType,
6667
String[] keys,
6768
PlannedInterpretable[] values) {
6869
return new EvalCreateStruct(exprId, valueProvider, structType, keys, values);
@@ -71,7 +72,7 @@ static EvalCreateStruct create(
7172
private EvalCreateStruct(
7273
long exprId,
7374
CelValueProvider valueProvider,
74-
StructType structType,
75+
CelType structType,
7576
String[] keys,
7677
PlannedInterpretable[] values) {
7778
super(exprId);

runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import dev.cel.common.types.CelType;
4242
import dev.cel.common.types.CelTypeProvider;
4343
import dev.cel.common.types.SimpleType;
44-
import dev.cel.common.types.StructType;
4544
import dev.cel.common.types.TypeType;
4645
import dev.cel.common.values.CelValueConverter;
4746
import dev.cel.common.values.CelValueProvider;
@@ -277,7 +276,7 @@ private PlannedInterpretable planCall(CelExpr expr, PlannerContext ctx) {
277276

278277
private PlannedInterpretable planCreateStruct(CelExpr celExpr, PlannerContext ctx) {
279278
CelStruct struct = celExpr.struct();
280-
StructType structType = resolveStructType(struct);
279+
CelType structType = resolveStructType(struct);
281280

282281
ImmutableList<Entry> entries = struct.entries();
283282
String[] keys = new String[entries.size()];
@@ -414,21 +413,25 @@ private ResolvedFunction resolveFunction(
414413
return ResolvedFunction.newBuilder().setFunctionName(functionName).setTarget(target).build();
415414
}
416415

417-
private StructType resolveStructType(CelStruct struct) {
416+
private CelType resolveStructType(CelStruct struct) {
418417
String messageName = struct.messageName();
419418
for (String typeName : container.resolveCandidateNames(messageName)) {
420419
CelType structType = typeProvider.findType(typeName).orElse(null);
421420
if (structType == null) {
422421
continue;
423422
}
424423

425-
if (!structType.kind().equals(CelKind.STRUCT)) {
424+
CelKind kind = structType.kind();
425+
426+
if (!kind.equals(CelKind.STRUCT)
427+
&& !kind.equals(CelKind.TIMESTAMP)
428+
&& !kind.equals(CelKind.DURATION)) {
426429
throw new IllegalArgumentException(
427430
String.format(
428431
"Expected struct type for %s, got %s", structType.name(), structType.kind()));
429432
}
430433

431-
return (StructType) structType;
434+
return structType;
432435
}
433436

434437
throw new IllegalArgumentException("Undefined type name: " + messageName);

runtime/src/test/java/dev/cel/runtime/PlannerInterpreterTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,4 @@ public void jsonFieldNames() throws Exception {
5151
// TODO: Support JSON field names for planner
5252
skipBaselineVerification();
5353
}
54-
55-
@Override
56-
public void wrappers() throws Exception {
57-
// TODO: Fix for planner
58-
skipBaselineVerification();
59-
}
6054
}

0 commit comments

Comments
 (0)