Skip to content

Commit ece82cc

Browse files
l46kokcopybara-github
authored andcommitted
Enable evaluateCanonicalTypesToNativeValues by default
PiperOrigin-RevId: 808650209
1 parent c0dbe7e commit ece82cc

File tree

8 files changed

+21
-18
lines changed

8 files changed

+21
-18
lines changed

codelab/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,8 @@ public void validate_invalidTimestampLiteral_returnsError() throws Exception {
10651065
assertThat(validationResult.hasError()).isTrue();
10661066
assertThat(validationResult.getErrorString())
10671067
.isEqualTo(
1068-
"ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Failed to"
1069-
+ " parse timestamp: invalid timestamp \"bad\"\n"
1068+
"ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Text 'bad'"
1069+
+ " could not be parsed at index 0\n"
10701070
+ " | timestamp('bad')\n"
10711071
+ " | ..........^");
10721072
}

codelab/src/test/codelab/Exercise8Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public void validate_invalidTimestampLiteral_returnsError() throws Exception {
4343
assertThat(validationResult.hasError()).isTrue();
4444
assertThat(validationResult.getErrorString())
4545
.isEqualTo(
46-
"ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Failed to"
47-
+ " parse timestamp: invalid timestamp \"bad\"\n"
46+
"ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Text 'bad'"
47+
+ " could not be parsed at index 0\n"
4848
+ " | timestamp('bad')\n"
4949
+ " | ..........^");
5050
}

codelab/src/test/codelab/solutions/Exercise8Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public void validate_invalidTimestampLiteral_returnsError() throws Exception {
4343
assertThat(validationResult.hasError()).isTrue();
4444
assertThat(validationResult.getErrorString())
4545
.isEqualTo(
46-
"ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Failed to"
47-
+ " parse timestamp: invalid timestamp \"bad\"\n"
46+
"ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Text 'bad'"
47+
+ " could not be parsed at index 0\n"
4848
+ " | timestamp('bad')\n"
4949
+ " | ..........^");
5050
}

common/src/main/java/dev/cel/common/CelOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public static Builder current() {
184184
.enableUnsignedLongs(true)
185185
.enableRegexPartialMatch(true)
186186
.errorOnDuplicateMapKeys(true)
187+
.evaluateCanonicalTypesToNativeValues(true)
187188
.errorOnIntWrap(true)
188189
.resolveTypeDependencies(true)
189190
.disableCelStandardEquality(false);
@@ -465,7 +466,10 @@ public abstract static class Builder {
465466
* <li>Timestamp: {@code java.time.Instant} instead of {@code com.google.protobuf.Timestamp}.
466467
* <li>Duration: {@code java.time.Duration} instead of {@code com.google.protobuf.Duration}.
467468
* </ul>
469+
*
470+
* @deprecated Do not use. This flag is enabled by default and will be removed in the future.
468471
*/
472+
@Deprecated
469473
public abstract Builder evaluateCanonicalTypesToNativeValues(boolean value);
470474

471475
/**

validator/src/main/java/dev/cel/validator/validators/DurationLiteralValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package dev.cel.validator.validators;
1616

17-
import com.google.protobuf.Duration;
17+
import java.time.Duration;
1818

1919
/** DurationLiteralValidator ensures that duration literal arguments are valid. */
2020
public final class DurationLiteralValidator extends LiteralValidator {

validator/src/main/java/dev/cel/validator/validators/TimestampLiteralValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
package dev.cel.validator.validators;
1616

17-
import com.google.protobuf.Timestamp;
17+
import java.time.Instant;
1818

1919
/** TimestampLiteralValidator ensures that timestamp literal arguments are valid. */
2020
public final class TimestampLiteralValidator extends LiteralValidator {
2121
public static final TimestampLiteralValidator INSTANCE =
22-
new TimestampLiteralValidator("timestamp", Timestamp.class);
22+
new TimestampLiteralValidator("timestamp", Instant.class);
2323

2424
private TimestampLiteralValidator(String functionName, Class<?> expectedResultType) {
2525
super(functionName, expectedResultType);

validator/src/test/java/dev/cel/validator/validators/DurationLiteralValidatorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.Assert.assertThrows;
2121

2222
import com.google.common.collect.ImmutableMap;
23-
import com.google.protobuf.Duration;
2423
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2524
import com.google.testing.junit.testparameterinjector.TestParameters;
2625
import dev.cel.bundle.Cel;
@@ -35,6 +34,7 @@
3534
import dev.cel.validator.CelValidator;
3635
import dev.cel.validator.CelValidatorFactory;
3736
import java.text.ParseException;
37+
import java.time.Duration;
3838
import org.junit.Test;
3939
import org.junit.runner.RunWith;
4040

@@ -174,7 +174,7 @@ public void duration_unexpectedResultType_throws() throws Exception {
174174
assertThat(result.getAllIssues().get(0).toDisplayString(ast.getSource()))
175175
.isEqualTo(
176176
"ERROR: <input>:1:10: duration validation failed. Reason: Expected"
177-
+ " com.google.protobuf.Duration type but got java.lang.Integer instead\n"
177+
+ " java.time.Duration type but got java.lang.Integer instead\n"
178178
+ " | duration('1h')\n"
179179
+ " | .........^");
180180
}

validator/src/test/java/dev/cel/validator/validators/TimestampLiteralValidatorTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.Assert.assertThrows;
2121

2222
import com.google.common.collect.ImmutableMap;
23-
import com.google.protobuf.Timestamp;
2423
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2524
import com.google.testing.junit.testparameterinjector.TestParameters;
2625
import dev.cel.bundle.Cel;
@@ -36,6 +35,7 @@
3635
import dev.cel.validator.CelValidator;
3736
import dev.cel.validator.CelValidatorFactory;
3837
import java.text.ParseException;
38+
import java.time.Instant;
3939
import org.junit.Test;
4040
import org.junit.runner.RunWith;
4141

@@ -63,7 +63,7 @@ public void timestamp_validFormat(String source) throws Exception {
6363

6464
assertThat(result.hasError()).isFalse();
6565
assertThat(result.getAllIssues()).isEmpty();
66-
assertThat(CEL.createProgram(ast).eval()).isInstanceOf(Timestamp.class);
66+
assertThat(CEL.createProgram(ast).eval()).isInstanceOf(Instant.class);
6767
}
6868

6969
@Test
@@ -100,8 +100,7 @@ public void timestamp_withVariable_noOp() throws Exception {
100100
() -> CEL.createProgram(ast).eval(ImmutableMap.of("str_var", "bad")));
101101
assertThat(e)
102102
.hasMessageThat()
103-
.contains(
104-
"evaluation error at <input>:9: Failed to parse timestamp: invalid timestamp \"bad\"");
103+
.contains("evaluation error at <input>:9: Text 'bad' could not be parsed at index 0");
105104
}
106105

107106
@Test
@@ -153,8 +152,8 @@ public void timestamp_invalidFormat() throws Exception {
153152
assertThat(result.getAllIssues().get(0).getSeverity()).isEqualTo(Severity.ERROR);
154153
assertThat(result.getAllIssues().get(0).toDisplayString(ast.getSource()))
155154
.isEqualTo(
156-
"ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Failed to"
157-
+ " parse timestamp: invalid timestamp \"bad\"\n"
155+
"ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Text 'bad'"
156+
+ " could not be parsed at index 0\n"
158157
+ " | timestamp('bad')\n"
159158
+ " | ..........^");
160159
}
@@ -185,7 +184,7 @@ public void timestamp_unexpectedResultType_throws() throws Exception {
185184
assertThat(result.getAllIssues().get(0).toDisplayString(ast.getSource()))
186185
.isEqualTo(
187186
"ERROR: <input>:1:11: timestamp validation failed. Reason: Expected"
188-
+ " com.google.protobuf.Timestamp type but got java.lang.Integer instead\n"
187+
+ " java.time.Instant type but got java.lang.Integer instead\n"
189188
+ " | timestamp(0)\n"
190189
+ " | ..........^");
191190
}

0 commit comments

Comments
 (0)