Skip to content

Commit e348a0a

Browse files
l46kokcopybara-github
authored andcommitted
Change string.split to return an immutable list. Add parsed-only evaluation coverage to CelStringExtensions
PiperOrigin-RevId: 900202732
1 parent 5667bd8 commit e348a0a

File tree

5 files changed

+238
-315
lines changed

5 files changed

+238
-315
lines changed

extensions/src/main/java/dev/cel/extensions/CelStringExtensions.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.common.base.Splitter;
2424
import com.google.common.collect.ImmutableList;
2525
import com.google.common.collect.ImmutableSet;
26-
import com.google.common.collect.Lists;
2726
import com.google.errorprone.annotations.Immutable;
2827
import dev.cel.checker.CelCheckerBuilder;
2928
import dev.cel.common.CelFunctionDecl;
@@ -37,7 +36,6 @@
3736
import dev.cel.runtime.CelFunctionBinding;
3837
import dev.cel.runtime.CelRuntimeBuilder;
3938
import dev.cel.runtime.CelRuntimeLibrary;
40-
import java.util.ArrayList;
4139
import java.util.List;
4240
import java.util.Set;
4341

@@ -475,7 +473,7 @@ private static String quote(String s) {
475473
sb.append('"');
476474
for (int i = 0; i < s.length(); ) {
477475
int codePoint = s.codePointAt(i);
478-
if (isMalformedUtf16(s, i, codePoint)) {
476+
if (isMalformedUtf16(s, i)) {
479477
sb.append('\uFFFD');
480478
i++;
481479
continue;
@@ -518,7 +516,7 @@ private static String quote(String s) {
518516
return sb.toString();
519517
}
520518

521-
private static boolean isMalformedUtf16(String s, int index, int codePoint) {
519+
private static boolean isMalformedUtf16(String s, int index) {
522520
char currentChar = s.charAt(index);
523521
if (Character.isLowSurrogate(currentChar)) {
524522
return true;
@@ -587,14 +585,14 @@ private static String reverse(String s) {
587585
return new StringBuilder(s).reverse().toString();
588586
}
589587

590-
private static List<String> split(String str, String separator) {
588+
private static ImmutableList<String> split(String str, String separator) {
591589
return split(str, separator, Integer.MAX_VALUE);
592590
}
593591

594592
/**
595593
* @param args Object array with indices of: [0: string], [1: separator], [2: limit]
596594
*/
597-
private static List<String> split(Object[] args) throws CelEvaluationException {
595+
private static ImmutableList<String> split(Object[] args) throws CelEvaluationException {
598596
long limitInLong = (Long) args[2];
599597
int limit;
600598
try {
@@ -609,16 +607,14 @@ private static List<String> split(Object[] args) throws CelEvaluationException {
609607
return split((String) args[0], (String) args[1], limit);
610608
}
611609

612-
/** Returns a **mutable** list of strings split on the separator */
613-
private static List<String> split(String str, String separator, int limit) {
610+
/** Returns an immutable list of strings split on the separator */
611+
private static ImmutableList<String> split(String str, String separator, int limit) {
614612
if (limit == 0) {
615-
return new ArrayList<>();
613+
return ImmutableList.of();
616614
}
617615

618616
if (limit == 1) {
619-
List<String> singleElementList = new ArrayList<>();
620-
singleElementList.add(str);
621-
return singleElementList;
617+
return ImmutableList.of(str);
622618
}
623619

624620
if (limit < 0) {
@@ -630,7 +626,7 @@ private static List<String> split(String str, String separator, int limit) {
630626
}
631627

632628
Iterable<String> splitString = Splitter.on(separator).limit(limit).split(str);
633-
return Lists.newArrayList(splitString);
629+
return ImmutableList.copyOf(splitString);
634630
}
635631

636632
/**
@@ -643,8 +639,8 @@ private static List<String> split(String str, String separator, int limit) {
643639
* <p>This exists because neither the built-in String.split nor Guava's splitter is able to deal
644640
* with separating single printable characters.
645641
*/
646-
private static List<String> explode(String str, int limit) {
647-
List<String> exploded = new ArrayList<>();
642+
private static ImmutableList<String> explode(String str, int limit) {
643+
ImmutableList.Builder<String> exploded = ImmutableList.builder();
648644
CelCodePointArray codePointArray = CelCodePointArray.fromString(str);
649645
if (limit > 0) {
650646
limit -= 1;
@@ -656,7 +652,7 @@ private static List<String> explode(String str, int limit) {
656652
if (codePointArray.length() > limit) {
657653
exploded.add(codePointArray.slice(limit, codePointArray.length()).toString());
658654
}
659-
return exploded;
655+
return exploded.build();
660656
}
661657

662658
private static Object substring(String s, long i) throws CelEvaluationException {

extensions/src/main/java/dev/cel/extensions/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ Examples:
522522

523523
### Split
524524

525-
Returns a mutable list of strings split from the input by the given separator. The
525+
Returns a list of strings split from the input by the given separator. The
526526
function accepts an optional argument specifying a limit on the number of
527527
substrings produced by the split.
528528

@@ -1069,4 +1069,4 @@ Examples:
10691069
{valueVar: indexVar}) // returns {1:0, 2:1, 3:2}
10701070

10711071
{'greeting': 'aloha', 'farewell': 'aloha'}
1072-
.transformMapEntry(k, v, {v: k}) // error, duplicate key
1072+
.transformMapEntry(k, v, {v: k}) // error, duplicate key

extensions/src/test/java/dev/cel/extensions/CelComprehensionsExtensionsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import dev.cel.common.CelFunctionDecl;
2929
import dev.cel.common.CelOptions;
3030
import dev.cel.common.CelValidationException;
31+
import dev.cel.common.CelValidationResult;
3132
import dev.cel.common.exceptions.CelAttributeNotFoundException;
3233
import dev.cel.common.exceptions.CelDivideByZeroException;
3334
import dev.cel.common.exceptions.CelIndexOutOfBoundsException;
@@ -312,8 +313,8 @@ public void unparseAST_twoVarComprehension(
312313
+ " err: 'no matching overload'}")
313314
public void twoVarComprehension_compilerErrors(String expr, String err) throws Exception {
314315
Assume.assumeFalse(isParseOnly);
315-
CelValidationException e =
316-
assertThrows(CelValidationException.class, () -> cel.compile(expr).getAst());
316+
CelValidationResult result = cel.compile(expr);
317+
CelValidationException e = assertThrows(CelValidationException.class, () -> result.getAst());
317318

318319
assertThat(e).hasMessageThat().contains(err);
319320
}

extensions/src/test/java/dev/cel/extensions/CelListsExtensionsTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import dev.cel.common.CelAbstractSyntaxTree;
2828
import dev.cel.common.CelContainer;
2929
import dev.cel.common.CelValidationException;
30+
import dev.cel.common.CelValidationResult;
3031
import dev.cel.common.types.SimpleType;
3132
import dev.cel.expr.conformance.test.SimpleTest;
3233
import dev.cel.parser.CelStandardMacro;
@@ -300,10 +301,8 @@ public void sortBy_success(String expression, String expected) throws Exception
300301
+ "expectedError: 'variable name must be a simple identifier'}")
301302
public void sortBy_throws_validationException(String expression, String expectedError)
302303
throws Exception {
303-
assertThat(
304-
assertThrows(
305-
CelValidationException.class,
306-
() -> cel.createProgram(cel.compile(expression).getAst()).eval()))
304+
CelValidationResult result = cel.compile(expression);
305+
assertThat(assertThrows(CelValidationException.class, () -> result.getAst()))
307306
.hasMessageThat()
308307
.contains(expectedError);
309308
}

0 commit comments

Comments
 (0)