Skip to content

Commit 9e26ff0

Browse files
CEL Dev Teamcopybara-github
CEL Dev Team
authored andcommitted
Porting range() to Java Stack
PiperOrigin-RevId: 753777198
1 parent a3f0064 commit 9e26ff0

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ public enum Function {
5454
SimpleType.INT)),
5555
CelFunctionBinding.from("list_flatten", Collection.class, list -> flatten(list, 1)),
5656
CelFunctionBinding.from(
57-
"list_flatten_list_int", Collection.class, Long.class, CelListsExtensions::flatten));
57+
"list_flatten_list_int", Collection.class, Long.class, CelListsExtensions::flatten)),
58+
RANGE(
59+
CelFunctionDecl.newFunctionDeclaration(
60+
"lists.range",
61+
CelOverloadDecl.newGlobalOverload(
62+
"lists_range",
63+
"Returns a list of integers from 0 to n-1.",
64+
ListType.create(SimpleType.INT),
65+
SimpleType.INT)),
66+
CelFunctionBinding.from("lists_range", Long.class, CelListsExtensions::genRange));
5867

5968
private final CelFunctionDecl functionDecl;
6069
private final ImmutableSet<CelFunctionBinding> functionBindings;
@@ -104,4 +113,12 @@ private static ImmutableList<Object> flatten(Collection<Object> list, long depth
104113

105114
return builder.build();
106115
}
116+
117+
public static ImmutableList<Long> genRange(long end) {
118+
ImmutableList.Builder<Long> builder = ImmutableList.builder();
119+
for (long i = 0; i < end; i++) {
120+
builder.add(i);
121+
}
122+
return builder.build();
123+
}
107124
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,19 @@ dyn([1,2,3]).flatten() // [1,2,3]
688688

689689
This will be addressed once we add the appropriate capabilities in the
690690
type-checker to handle type-reductions, or union types.
691+
692+
### Range
693+
694+
Given integer size n returns a list of integers from 0 to n-1. If size <= 0
695+
then return empty list.
696+
697+
```
698+
lists.range(int) -> list(int)
699+
```
700+
701+
Examples:
702+
703+
```
704+
lists.range(5) -> [0, 1, 2, 3, 4]
705+
lists.range(0) -> []
706+
```

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public void getAllFunctionNames() {
177177
"sets.intersects",
178178
"base64.decode",
179179
"base64.encode",
180-
"flatten");
180+
"flatten",
181+
"lists.range");
181182
}
182183
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ public void flattenSingleLevel_listIsSingleLevel_throws(String expression) {
8989
// due to the lack of full-fledged dynamic dispatch.
9090
assertThrows(CelValidationException.class, () -> CEL.compile(expression).getAst());
9191
}
92+
93+
@Test
94+
@TestParameters("{expression: 'lists.range(9) == [0,1,2,3,4,5,6,7,8]'}")
95+
@TestParameters("{expression: 'lists.range(0) == []'}")
96+
@TestParameters("{expression: 'lists.range(-1) == []'}")
97+
public void range_success(String expression) throws Exception {
98+
boolean result = (boolean) CEL.createProgram(CEL.compile(expression).getAst()).eval();
99+
100+
assertThat(result).isTrue();
101+
}
92102
}

0 commit comments

Comments
 (0)