Skip to content

Commit d80531c

Browse files
l46kokcopybara-github
authored andcommitted
Make CelAbstractSyntaxTree and its dependents an AutoValue class to allow for equality checks
PiperOrigin-RevId: 680437295
1 parent a9e8c82 commit d80531c

8 files changed

Lines changed: 146 additions & 155 deletions

File tree

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

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

1717
import dev.cel.expr.Type;
18+
import com.google.auto.value.AutoValue;
1819
import com.google.common.collect.ImmutableList;
1920
import com.google.common.collect.ImmutableMap;
2021
import com.google.errorprone.annotations.Immutable;
@@ -36,16 +37,17 @@
3637
* <p>Note: Use {@link CelProtoAbstractSyntaxTree} if you need access to the protobuf equivalent
3738
* ASTs, such as ParsedExpr and CheckedExpr from syntax.proto or checked.proto.
3839
*/
40+
@AutoValue
3941
@Immutable
40-
public final class CelAbstractSyntaxTree {
42+
public abstract class CelAbstractSyntaxTree {
4143

42-
private final CelSource celSource;
44+
abstract CelSource celSource();
4345

44-
private final CelExpr celExpr;
46+
abstract CelExpr celExpr();
4547

46-
private final ImmutableMap<Long, CelReference> references;
48+
abstract ImmutableMap<Long, CelReference> references();
4749

48-
private final ImmutableMap<Long, CelType> types;
50+
abstract ImmutableMap<Long, CelType> types();
4951

5052
/**
5153
* Constructs a new instance of CelAbstractSyntaxTree that represent a parsed expression.
@@ -54,7 +56,8 @@ public final class CelAbstractSyntaxTree {
5456
* validating or optimizing an AST.
5557
*/
5658
public static CelAbstractSyntaxTree newParsedAst(CelExpr celExpr, CelSource celSource) {
57-
return new CelAbstractSyntaxTree(celExpr, celSource);
59+
return new AutoValue_CelAbstractSyntaxTree(
60+
celSource, celExpr, ImmutableMap.of(), ImmutableMap.of());
5861
}
5962

6063
/**
@@ -69,32 +72,18 @@ public static CelAbstractSyntaxTree newCheckedAst(
6972
CelSource celSource,
7073
Map<Long, CelReference> references,
7174
Map<Long, CelType> types) {
72-
return new CelAbstractSyntaxTree(celExpr, celSource, references, types);
73-
}
74-
75-
private CelAbstractSyntaxTree(CelExpr celExpr, CelSource celSource) {
76-
this(celExpr, celSource, ImmutableMap.of(), ImmutableMap.of());
77-
}
78-
79-
private CelAbstractSyntaxTree(
80-
CelExpr celExpr,
81-
CelSource celSource,
82-
Map<Long, CelReference> references,
83-
Map<Long, CelType> types) {
84-
this.celExpr = celExpr;
85-
this.celSource = celSource;
86-
this.references = ImmutableMap.copyOf(references);
87-
this.types = ImmutableMap.copyOf(types);
75+
return new AutoValue_CelAbstractSyntaxTree(
76+
celSource, celExpr, ImmutableMap.copyOf(references), ImmutableMap.copyOf(types));
8877
}
8978

9079
/** Returns the underlying {@link CelExpr} representation of the abstract syntax tree. */
9180
public CelExpr getExpr() {
92-
return celExpr;
81+
return celExpr();
9382
}
9483

9584
/** Tests whether the underlying abstract syntax tree has been type checked or not. */
9685
public boolean isChecked() {
97-
return !types.isEmpty();
86+
return !types().isEmpty();
9887
}
9988

10089
/**
@@ -117,23 +106,23 @@ public Type getProtoResultType() {
117106
* Returns the {@link CelSource} that was used during construction of the abstract syntax tree.
118107
*/
119108
public CelSource getSource() {
120-
return celSource;
109+
return celSource();
121110
}
122111

123112
public Optional<CelType> getType(long exprId) {
124-
return Optional.ofNullable(types.get(exprId));
113+
return Optional.ofNullable(types().get(exprId));
125114
}
126115

127116
public ImmutableMap<Long, CelType> getTypeMap() {
128-
return types;
117+
return types();
129118
}
130119

131120
public Optional<CelReference> getReference(long exprId) {
132-
return Optional.ofNullable(references.get(exprId));
121+
return Optional.ofNullable(references().get(exprId));
133122
}
134123

135124
public ImmutableMap<Long, CelReference> getReferenceMap() {
136-
return references;
125+
return references();
137126
}
138127

139128
public CelReference getReferenceOrThrow(long exprId) {
@@ -142,12 +131,12 @@ public CelReference getReferenceOrThrow(long exprId) {
142131
}
143132

144133
Optional<CelConstant> findEnumValue(long exprId) {
145-
CelReference ref = references.get(exprId);
134+
CelReference ref = references().get(exprId);
146135
return ref != null ? ref.value() : Optional.empty();
147136
}
148137

149138
Optional<ImmutableList<String>> findOverloadIDs(long exprId) {
150-
CelReference ref = references.get(exprId);
139+
CelReference ref = references().get(exprId);
151140
return ref != null && !ref.value().isPresent()
152141
? Optional.of(ref.overloadIds())
153142
: Optional.empty();

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

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,34 @@
3636

3737
/** Represents the source content of an expression and related metadata. */
3838
@Immutable
39-
public final class CelSource implements Source {
40-
41-
private final CelCodePointArray codePoints;
42-
private final String description;
43-
private final ImmutableList<Integer> lineOffsets;
44-
private final ImmutableMap<Long, Integer> positions;
45-
private final ImmutableMap<Long, CelExpr> macroCalls;
46-
private final ImmutableSet<Extension> extensions;
47-
48-
private CelSource(Builder builder) {
49-
this.codePoints = checkNotNull(builder.codePoints);
50-
this.description = checkNotNull(builder.description);
51-
this.positions = checkNotNull(ImmutableMap.copyOf(builder.positions));
52-
this.lineOffsets = checkNotNull(ImmutableList.copyOf(builder.lineOffsets));
53-
this.macroCalls = checkNotNull(ImmutableMap.copyOf(builder.macroCalls));
54-
this.extensions = checkNotNull(builder.extensions.build());
55-
}
39+
@AutoValue
40+
public abstract class CelSource implements Source {
41+
42+
abstract CelCodePointArray codePoints();
43+
44+
abstract String description();
45+
46+
abstract ImmutableList<Integer> lineOffsets();
47+
48+
abstract ImmutableMap<Long, Integer> positions();
49+
50+
abstract ImmutableMap<Long, CelExpr> macroCalls();
51+
52+
abstract ImmutableSet<Extension> extensions();
5653

5754
@Override
5855
public CelCodePointArray getContent() {
59-
return codePoints;
56+
return codePoints();
6057
}
6158

6259
@Override
6360
public String getDescription() {
64-
return description;
61+
return description();
6562
}
6663

6764
@Override
6865
public ImmutableMap<Long, Integer> getPositionsMap() {
69-
return positions;
66+
return positions();
7067
}
7168

7269
/**
@@ -76,15 +73,15 @@ public ImmutableMap<Long, Integer> getPositionsMap() {
7673
* <p>NOTE: The indices point to the index just after the '\n' not the index of '\n' itself.
7774
*/
7875
public ImmutableList<Integer> getLineOffsets() {
79-
return lineOffsets;
76+
return lineOffsets();
8077
}
8178

8279
public ImmutableMap<Long, CelExpr> getMacroCalls() {
83-
return macroCalls;
80+
return macroCalls();
8481
}
8582

8683
public ImmutableSet<Extension> getExtensions() {
87-
return extensions;
84+
return extensions();
8885
}
8986

9087
/** See {@link #getLocationOffset(int, int)}. */
@@ -101,19 +98,19 @@ public Optional<Integer> getLocationOffset(CelSourceLocation location) {
10198
* @param column the column number starting from 0
10299
*/
103100
public Optional<Integer> getLocationOffset(int line, int column) {
104-
return getLocationOffsetImpl(lineOffsets, line, column);
101+
return getLocationOffsetImpl(lineOffsets(), line, column);
105102
}
106103

107104
/**
108105
* Get the line and column in the source expression text for the given code point {@code offset}.
109106
*/
110107
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
111-
return CelSourceHelper.getOffsetLocation(codePoints, offset);
108+
return CelSourceHelper.getOffsetLocation(codePoints(), offset);
112109
}
113110

114111
@Override
115112
public Optional<String> getSnippet(int line) {
116-
return CelSourceHelper.getSnippet(codePoints, line);
113+
return CelSourceHelper.getSnippet(codePoints(), line);
117114
}
118115

119116
/**
@@ -136,11 +133,11 @@ private static Optional<Integer> getLocationOffsetImpl(
136133
}
137134

138135
public Builder toBuilder() {
139-
return new Builder(codePoints, lineOffsets)
140-
.setDescription(description)
141-
.addPositionsMap(positions)
142-
.addAllExtensions(extensions)
143-
.addAllMacroCalls(macroCalls);
136+
return new Builder(codePoints(), lineOffsets())
137+
.setDescription(description())
138+
.addPositionsMap(positions())
139+
.addAllExtensions(extensions())
140+
.addAllMacroCalls(macroCalls());
144141
}
145142

146143
public static Builder newBuilder() {
@@ -236,12 +233,6 @@ public Builder addAllMacroCalls(Map<Long, CelExpr> macroCalls) {
236233
return this;
237234
}
238235

239-
@CanIgnoreReturnValue
240-
public Builder clearMacroCall(long exprId) {
241-
this.macroCalls.remove(exprId);
242-
return this;
243-
}
244-
245236
public ImmutableSet<Extension> getExtensions() {
246237
return extensions.build();
247238
}
@@ -308,7 +299,13 @@ public boolean containsMacroCalls(long exprId) {
308299

309300
@CheckReturnValue
310301
public CelSource build() {
311-
return new CelSource(this);
302+
return new AutoValue_CelSource(
303+
codePoints,
304+
description,
305+
ImmutableList.copyOf(lineOffsets),
306+
ImmutableMap.copyOf(positions),
307+
ImmutableMap.copyOf(macroCalls),
308+
extensions.build());
312309
}
313310
}
314311

@@ -369,7 +366,7 @@ public enum Component {
369366
/** Type checker. Checks that references in an AST are defined and types agree. */
370367
COMPONENT_TYPE_CHECKER,
371368
/** Runtime. Evaluates a parsed and optionally checked CEL AST against a context. */
372-
COMPONENT_RUNTIME;
369+
COMPONENT_RUNTIME
373370
}
374371

375372
@CheckReturnValue

common/src/main/java/dev/cel/common/internal/BasicCodePointArray.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919
import static com.google.common.base.Preconditions.checkPositionIndexes;
2020

21+
import com.google.auto.value.AutoValue;
2122
import com.google.common.annotations.VisibleForTesting;
2223
import com.google.common.collect.ImmutableList;
2324
import com.google.errorprone.annotations.Immutable;
@@ -32,50 +33,40 @@
3233
@Immutable
3334
@VisibleForTesting
3435
@Internal
35-
public final class BasicCodePointArray extends CelCodePointArray {
36+
@AutoValue
37+
@AutoValue.CopyAnnotations
38+
@SuppressWarnings("Immutable") // char[] is not exposed externally, thus cannot be mutated.
39+
public abstract class BasicCodePointArray extends CelCodePointArray {
3640

37-
@SuppressWarnings("Immutable")
38-
private final char[] codePoints;
41+
@SuppressWarnings("AutoValueImmutableFields")
42+
abstract char[] codePoints();
3943

40-
private final int offset;
41-
private final int size;
42-
private final ImmutableList<Integer> lineOffsets;
44+
abstract int offset();
4345

44-
BasicCodePointArray(char[] codePoints, int size, ImmutableList<Integer> lineOffsets) {
45-
this(codePoints, 0, lineOffsets, size);
46+
static BasicCodePointArray create(
47+
char[] codePoints, int size, ImmutableList<Integer> lineOffsets) {
48+
return create(codePoints, 0, lineOffsets, size);
4649
}
4750

48-
BasicCodePointArray(char[] codePoints, int offset, ImmutableList<Integer> lineOffsets, int size) {
49-
this.codePoints = checkNotNull(codePoints);
50-
this.offset = offset;
51-
this.size = size;
52-
this.lineOffsets = lineOffsets;
51+
static BasicCodePointArray create(
52+
char[] codePoints, int offset, ImmutableList<Integer> lineOffsets, int size) {
53+
return new AutoValue_BasicCodePointArray(size, checkNotNull(lineOffsets), codePoints, offset);
5354
}
5455

5556
@Override
5657
public BasicCodePointArray slice(int i, int j) {
5758
checkPositionIndexes(i, j, size());
58-
return new BasicCodePointArray(codePoints, offset + i, lineOffsets, j - i);
59+
return create(codePoints(), offset() + i, lineOffsets(), j - i);
5960
}
6061

6162
@Override
6263
public int get(int index) {
6364
checkElementIndex(index, size());
64-
return codePoints[offset + index] & 0xffff;
65+
return codePoints()[offset() + index] & 0xffff;
6566
}
6667

6768
@Override
68-
public int size() {
69-
return size;
70-
}
71-
72-
@Override
73-
public ImmutableList<Integer> lineOffsets() {
74-
return lineOffsets;
75-
}
76-
77-
@Override
78-
public String toString() {
79-
return new String(codePoints, offset, size);
69+
public final String toString() {
70+
return new String(codePoints(), offset(), size());
8071
}
8172
}

common/src/main/java/dev/cel/common/internal/CelCodePointArray.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ public static CelCodePointArray fromString(String text) {
101101
intArray[intIndex++] = codePoint;
102102
}
103103

104-
return new SupplementalCodePointArray(
104+
return SupplementalCodePointArray.create(
105105
intArray, intIndex, lineOffsetContext.buildLineOffsets());
106106
}
107107

108-
return new BasicCodePointArray(charArray, charIndex, lineOffsetContext.buildLineOffsets());
108+
return BasicCodePointArray.create(
109+
charArray, charIndex, lineOffsetContext.buildLineOffsets());
109110
}
110111
int[] intArray = new int[text.length()];
111112
int intIndex = 0;
@@ -120,11 +121,11 @@ public static CelCodePointArray fromString(String text) {
120121
intArray[intIndex++] = codePoint;
121122
}
122123

123-
return new SupplementalCodePointArray(
124+
return SupplementalCodePointArray.create(
124125
intArray, intIndex, lineOffsetContext.buildLineOffsets());
125126
}
126127

127-
return new Latin1CodePointArray(byteArray, byteIndex, lineOffsetContext.buildLineOffsets());
128+
return Latin1CodePointArray.create(byteArray, byteIndex, lineOffsetContext.buildLineOffsets());
128129
}
129130

130131
private static class LineOffsetContext {

0 commit comments

Comments
 (0)