Skip to content

Commit bc978d9

Browse files
l46kokcopybara-github
authored andcommitted
Drop expr suffix from CelExpr static constructors
PiperOrigin-RevId: 625128541
1 parent cd407c7 commit bc978d9

64 files changed

Lines changed: 3742 additions & 2350 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bundle/src/test/java/dev/cel/bundle/CelImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public void compile_withOptionalTypes() throws Exception {
485485

486486
CelCreateList createList = ast.getExpr().createList();
487487
assertThat(createList.optionalIndices()).containsExactly(0);
488-
assertThat(createList.elements()).containsExactly(CelExpr.ofIdentExpr(2, "a"));
488+
assertThat(createList.elements()).containsExactly(CelExpr.ofIdent(2, "a"));
489489
}
490490

491491
@Test

common/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ java_library(
3939
exports = ["//common/src/main/java/dev/cel/common:error_codes"],
4040
)
4141

42+
java_library(
43+
name = "mutable_ast",
44+
exports = ["//common/src/main/java/dev/cel/common:mutable_ast"],
45+
)
46+
47+
java_library(
48+
name = "mutable_source",
49+
exports = ["//common/src/main/java/dev/cel/common:mutable_source"],
50+
)
51+
4252
java_library(
4353
name = "runtime_exception",
4454
visibility = ["//visibility:public"],

common/ast/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ java_library(
3434
)
3535

3636
java_library(
37-
name = "mutable_ast",
38-
exports = ["//common/src/main/java/dev/cel/common/ast:mutable_ast"],
37+
name = "mutable_expr",
38+
exports = ["//common/src/main/java/dev/cel/common/ast:mutable_expr"],
3939
)

common/navigation/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ package(
33
default_visibility = ["//visibility:public"],
44
)
55

6+
java_library(
7+
name = "common",
8+
exports = ["//common/src/main/java/dev/cel/common/navigation:common"],
9+
)
10+
611
java_library(
712
name = "navigation",
813
exports = ["//common/src/main/java/dev/cel/common/navigation"],
914
)
15+
16+
java_library(
17+
name = "mutable_navigation",
18+
exports = ["//common/src/main/java/dev/cel/common/navigation:mutable_navigation"],
19+
)

common/src/main/java/dev/cel/common/BUILD.bazel

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,31 @@ java_library(
134134
"//common/annotations",
135135
],
136136
)
137+
138+
java_library(
139+
name = "mutable_ast",
140+
srcs = ["CelMutableAst.java"],
141+
tags = [
142+
],
143+
deps = [
144+
":mutable_source",
145+
"//common",
146+
"//common/ast",
147+
"//common/ast:mutable_expr",
148+
"//common/types:type_providers",
149+
],
150+
)
151+
152+
java_library(
153+
name = "mutable_source",
154+
srcs = ["CelMutableSource.java"],
155+
tags = [
156+
],
157+
deps = [
158+
":common",
159+
"//:auto_value",
160+
"//common/ast:mutable_expr",
161+
"@maven//:com_google_errorprone_error_prone_annotations",
162+
"@maven//:com_google_guava_guava",
163+
],
164+
)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common;
16+
17+
import dev.cel.common.ast.CelMutableExpr;
18+
import dev.cel.common.ast.CelMutableExprConverter;
19+
import dev.cel.common.ast.CelReference;
20+
import dev.cel.common.types.CelType;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.Optional;
24+
25+
/**
26+
* An abstract representation of CEL Abstract Syntax tree that allows mutation in any of its
27+
* properties. This class is semantically the same as that of the immutable {@link
28+
* CelAbstractSyntaxTree}.
29+
*
30+
* <p>This should only be used within optimizers to augment an AST.
31+
*/
32+
public final class CelMutableAst {
33+
private final CelMutableExpr mutatedExpr;
34+
private final CelMutableSource source;
35+
private final Map<Long, CelReference> references;
36+
private final Map<Long, CelType> types;
37+
38+
/** Returns the underlying {@link CelMutableExpr} representation of the abstract syntax tree. */
39+
public CelMutableExpr expr() {
40+
return mutatedExpr;
41+
}
42+
43+
/**
44+
* Returns the {@link CelMutableSource} that was used during construction of the abstract syntax
45+
* tree.
46+
*/
47+
public CelMutableSource source() {
48+
return source;
49+
}
50+
51+
/**
52+
* Returns the resolved reference to a declaration at expression ID for a type-checked AST.
53+
*
54+
* @return Optional of {@link CelReference} or {@link Optional#empty} if the reference does not
55+
* exist at the ID.
56+
*/
57+
public Optional<CelReference> getReference(long exprId) {
58+
return Optional.ofNullable(references.get(exprId));
59+
}
60+
61+
/**
62+
* Returns the type of the expression node for a type-checked AST.
63+
*
64+
* @return Optional of {@link CelType} or {@link Optional#empty} if the type does not exist at the
65+
* ID.
66+
*/
67+
public Optional<CelType> getType(long exprId) {
68+
return Optional.ofNullable(types.get(exprId));
69+
}
70+
71+
/** Converts this mutable AST into a parsed {@link CelAbstractSyntaxTree}. */
72+
public CelAbstractSyntaxTree toParsedAst() {
73+
return CelAbstractSyntaxTree.newParsedAst(
74+
CelMutableExprConverter.fromMutableExpr(mutatedExpr), source.toCelSource());
75+
}
76+
77+
/**
78+
* Constructs an instance of {@link CelMutableAst} with the incoming {@link
79+
* CelAbstractSyntaxTree}.
80+
*/
81+
public static CelMutableAst fromCelAst(CelAbstractSyntaxTree ast) {
82+
return new CelMutableAst(
83+
CelMutableExprConverter.fromCelExpr(ast.getExpr()),
84+
CelMutableSource.fromCelSource(ast.getSource()),
85+
ast.getReferenceMap(),
86+
ast.getTypeMap());
87+
}
88+
89+
/**
90+
* Constructs an instance of {@link CelMutableAst} with the mutable expression and its source
91+
* builder.
92+
*/
93+
public static CelMutableAst of(CelMutableExpr mutableExpr, CelMutableSource mutableSource) {
94+
return new CelMutableAst(mutableExpr, mutableSource);
95+
}
96+
97+
private CelMutableAst(CelMutableExpr mutatedExpr, CelMutableSource mutableSource) {
98+
this(mutatedExpr, mutableSource, new HashMap<>(), new HashMap<>());
99+
}
100+
101+
private CelMutableAst(
102+
CelMutableExpr mutatedExpr,
103+
CelMutableSource mutableSource,
104+
Map<Long, CelReference> references,
105+
Map<Long, CelType> types) {
106+
this.mutatedExpr = mutatedExpr;
107+
this.source = mutableSource;
108+
this.references = new HashMap<>(references);
109+
this.types = new HashMap<>(types);
110+
}
111+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common;
16+
17+
import static com.google.common.base.Preconditions.checkNotNull;
18+
import static com.google.common.collect.ImmutableMap.toImmutableMap;
19+
20+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
21+
import dev.cel.common.CelSource.Extension;
22+
import dev.cel.common.ast.CelMutableExpr;
23+
import dev.cel.common.ast.CelMutableExprConverter;
24+
import java.util.Collection;
25+
import java.util.HashMap;
26+
import java.util.HashSet;
27+
import java.util.Map;
28+
import java.util.Map.Entry;
29+
import java.util.Set;
30+
import java.util.stream.Collectors;
31+
32+
/**
33+
* Represents the mutable portion of the {@link CelSource}. This is intended for the purposes of
34+
* augmenting an AST through CEL optimizers.
35+
*/
36+
public final class CelMutableSource {
37+
38+
final Map<Long, CelMutableExpr> macroCalls;
39+
final Set<Extension> extensions;
40+
41+
@CanIgnoreReturnValue
42+
public CelMutableSource addMacroCalls(long exprId, CelMutableExpr expr) {
43+
this.macroCalls.put(exprId, checkNotNull(CelMutableExpr.newInstance(expr)));
44+
return this;
45+
}
46+
47+
@CanIgnoreReturnValue
48+
public CelMutableSource addAllMacroCalls(Map<Long, CelMutableExpr> macroCalls) {
49+
this.macroCalls.putAll(macroCalls);
50+
return this;
51+
}
52+
53+
@CanIgnoreReturnValue
54+
public CelMutableSource addAllExtensions(Collection<? extends Extension> extensions) {
55+
checkNotNull(extensions);
56+
this.extensions.addAll(extensions);
57+
return this;
58+
}
59+
60+
@CanIgnoreReturnValue
61+
public CelMutableSource clearMacroCall(long exprId) {
62+
this.macroCalls.remove(exprId);
63+
return this;
64+
}
65+
66+
@CanIgnoreReturnValue
67+
public CelMutableSource clearMacroCalls() {
68+
this.macroCalls.clear();
69+
return this;
70+
}
71+
72+
public Map<Long, CelMutableExpr> getMacroCalls() {
73+
return macroCalls;
74+
}
75+
76+
public Set<Extension> getExtensions() {
77+
return extensions;
78+
}
79+
80+
public CelSource toCelSource() {
81+
return CelSource.newBuilder()
82+
.addAllExtensions(extensions)
83+
.addAllMacroCalls(
84+
macroCalls.entrySet().stream()
85+
.collect(
86+
toImmutableMap(
87+
Entry::getKey, v -> CelMutableExprConverter.fromMutableExpr(v.getValue()))))
88+
.build();
89+
}
90+
91+
public static CelMutableSource newInstance() {
92+
return new CelMutableSource(new HashMap<>(), new HashSet<>());
93+
}
94+
95+
public static CelMutableSource fromCelSource(CelSource source) {
96+
return new CelMutableSource(
97+
source.getMacroCalls().entrySet().stream()
98+
.collect(
99+
Collectors.toMap(
100+
Entry::getKey,
101+
v -> CelMutableExprConverter.fromCelExpr(v.getValue()),
102+
(prev, next) -> {
103+
throw new IllegalStateException(
104+
"Unexpected source collision at ID: " + prev.id());
105+
},
106+
HashMap::new)),
107+
source.getExtensions());
108+
}
109+
110+
CelMutableSource(Map<Long, CelMutableExpr> macroCalls, Set<Extension> extensions) {
111+
this.macroCalls = checkNotNull(macroCalls);
112+
this.extensions = checkNotNull(extensions);
113+
}
114+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ public Builder clearMacroCall(long exprId) {
279279
return this;
280280
}
281281

282+
public ImmutableSet<Extension> getExtensions() {
283+
return extensions.build();
284+
}
285+
282286
/**
283287
* Adds one or more {@link Extension}s to the source information. Extensions implement set
284288
* semantics and deduped if same ones are provided.

common/src/main/java/dev/cel/common/ast/BUILD.bazel

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ AST_SOURCES = [
1313
"CelExpr.java",
1414
"CelExprFormatter.java",
1515
"CelReference.java",
16+
"Expression.java",
1617
]
1718

1819
# keep sorted
@@ -32,7 +33,7 @@ EXPR_FACTORY_SOURCES = [
3233
]
3334

3435
# keep sorted
35-
MUTABLE_AST_SOURCES = [
36+
MUTABLE_EXPR_SOURCES = [
3637
"CelMutableExpr.java",
3738
"CelMutableExprConverter.java",
3839
]
@@ -117,8 +118,8 @@ java_library(
117118
)
118119

119120
java_library(
120-
name = "mutable_ast",
121-
srcs = MUTABLE_AST_SOURCES,
121+
name = "mutable_expr",
122+
srcs = MUTABLE_EXPR_SOURCES,
122123
tags = [
123124
],
124125
deps = [

0 commit comments

Comments
 (0)