Skip to content

Commit bcd3161

Browse files
l46kokcopybara-github
authored andcommitted
Add CelMutableIdent
PiperOrigin-RevId: 622277784
1 parent dec5452 commit bcd3161

File tree

2 files changed

+107
-7
lines changed

2 files changed

+107
-7
lines changed

common/src/main/java/dev/cel/common/ast/CelMutableExpr.java

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

1717
import static com.google.common.base.Preconditions.checkArgument;
18+
import static com.google.common.base.Preconditions.checkNotNull;
1819

1920
import dev.cel.common.ast.CelExpr.CelNotSet;
2021
import dev.cel.common.ast.CelExpr.ExprKind;
@@ -34,6 +35,7 @@ public final class CelMutableExpr {
3435
private ExprKind.Kind exprKind;
3536
private CelNotSet notSet;
3637
private CelConstant constant;
38+
private CelMutableIdent ident;
3739
private int hash = 0;
3840

3941
public long id() {
@@ -58,17 +60,58 @@ public CelConstant constant() {
5860
return constant;
5961
}
6062

63+
public CelMutableIdent ident() {
64+
checkExprKind(Kind.IDENT);
65+
return ident;
66+
}
67+
6168
public void setConstant(CelConstant constant) {
6269
this.exprKind = ExprKind.Kind.CONSTANT;
63-
this.constant = constant;
70+
this.constant = checkNotNull(constant);
6471
}
6572

66-
public static CelMutableExpr ofConstant(CelConstant constant) {
67-
return ofConstant(0L, constant);
73+
public void setIdent(CelMutableIdent ident) {
74+
this.exprKind = ExprKind.Kind.IDENT;
75+
this.ident = checkNotNull(ident);
6876
}
6977

70-
public static CelMutableExpr ofConstant(long id, CelConstant constant) {
71-
return new CelMutableExpr(id, constant);
78+
/** A mutable identifier expression. */
79+
public static final class CelMutableIdent {
80+
private String name = "";
81+
82+
public String name() {
83+
return name;
84+
}
85+
86+
public void setName(String name) {
87+
this.name = checkNotNull(name);
88+
}
89+
90+
public static CelMutableIdent create(String name) {
91+
return new CelMutableIdent(name);
92+
}
93+
94+
@Override
95+
public boolean equals(Object obj) {
96+
if (obj == this) {
97+
return true;
98+
}
99+
if (obj instanceof CelMutableIdent) {
100+
CelMutableIdent that = (CelMutableIdent) obj;
101+
return this.name.equals(that.name);
102+
}
103+
104+
return false;
105+
}
106+
107+
@Override
108+
public int hashCode() {
109+
return name.hashCode();
110+
}
111+
112+
private CelMutableIdent(String name) {
113+
this.name = checkNotNull(name);
114+
}
72115
}
73116

74117
public static CelMutableExpr ofNotSet() {
@@ -79,11 +122,32 @@ public static CelMutableExpr ofNotSet(long id) {
79122
return new CelMutableExpr(id);
80123
}
81124

125+
public static CelMutableExpr ofConstant(CelConstant constant) {
126+
return ofConstant(0L, constant);
127+
}
128+
129+
public static CelMutableExpr ofConstant(long id, CelConstant constant) {
130+
return new CelMutableExpr(id, constant);
131+
}
132+
133+
public static CelMutableExpr ofIdent(String name) {
134+
return ofIdent(0, name);
135+
}
136+
137+
public static CelMutableExpr ofIdent(long id, String name) {
138+
return new CelMutableExpr(id, CelMutableIdent.create(name));
139+
}
140+
82141
private CelMutableExpr(long id, CelConstant mutableConstant) {
83142
this.id = id;
84143
setConstant(mutableConstant);
85144
}
86145

146+
private CelMutableExpr(long id, CelMutableIdent mutableIdent) {
147+
this.id = id;
148+
setIdent(mutableIdent);
149+
}
150+
87151
private CelMutableExpr(long id) {
88152
this();
89153
this.id = id;
@@ -101,6 +165,7 @@ private Object exprValue() {
101165
case CONSTANT:
102166
return constant();
103167
case IDENT:
168+
return ident();
104169
case SELECT:
105170
case CALL:
106171
case CREATE_LIST:

common/src/test/java/dev/cel/common/ast/CelMutableExprTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.testing.junit.testparameterinjector.TestParameter;
2222
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2323
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
24+
import dev.cel.common.ast.CelMutableExpr.CelMutableIdent;
2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
2627

@@ -59,6 +60,31 @@ public void ofConstant_withId() {
5960
assertThat(mutableExpr.constant()).isEqualTo(CelConstant.ofValue(5L));
6061
}
6162

63+
@Test
64+
public void ofIdent() {
65+
CelMutableExpr mutableExpr = CelMutableExpr.ofIdent("x");
66+
67+
assertThat(mutableExpr.id()).isEqualTo(0L);
68+
assertThat(mutableExpr.ident().name()).isEqualTo("x");
69+
}
70+
71+
@Test
72+
public void ofIdent_withId() {
73+
CelMutableExpr mutableExpr = CelMutableExpr.ofIdent(1L, "x");
74+
75+
assertThat(mutableExpr.id()).isEqualTo(1L);
76+
assertThat(mutableExpr.ident().name()).isEqualTo("x");
77+
}
78+
79+
@Test
80+
public void mutableIdent_setName() {
81+
CelMutableIdent ident = CelMutableIdent.create("x");
82+
83+
ident.setName("y");
84+
85+
assertThat(ident.name()).isEqualTo("y");
86+
}
87+
6288
@Test
6389
public void setId_success() {
6490
CelMutableExpr mutableExpr = CelMutableExpr.ofConstant(CelConstant.ofValue(5L));
@@ -77,13 +103,17 @@ public void equalityTest() {
77103
.addEqualityGroup(
78104
CelMutableExpr.ofConstant(5L, CelConstant.ofValue("hello")),
79105
CelMutableExpr.ofConstant(5L, CelConstant.ofValue("hello")))
106+
.addEqualityGroup(CelMutableExpr.ofIdent("x"))
107+
.addEqualityGroup(CelMutableExpr.ofIdent(2L, "y"), CelMutableExpr.ofIdent(2L, "y"))
80108
.testEquals();
81109
}
82110

83111
@SuppressWarnings("Immutable") // Mutable by design
84112
private enum MutableExprKindTestCase {
85113
NOT_SET(CelMutableExpr.ofNotSet(1L)),
86-
CONSTANT(CelMutableExpr.ofConstant(CelConstant.ofValue(2L)));
114+
CONSTANT(CelMutableExpr.ofConstant(CelConstant.ofValue(2L))),
115+
IDENT(CelMutableExpr.ofIdent("test")),
116+
;
87117

88118
private final CelMutableExpr mutableExpr;
89119

@@ -101,12 +131,17 @@ public void getExprValue_invalidKind_throws(@TestParameter MutableExprKindTestCa
101131
if (!testCaseKind.equals(Kind.CONSTANT)) {
102132
assertThrows(IllegalArgumentException.class, testCase.mutableExpr::constant);
103133
}
134+
if (!testCaseKind.equals(Kind.IDENT)) {
135+
assertThrows(IllegalArgumentException.class, testCase.mutableExpr::ident);
136+
}
104137
}
105138

106139
@SuppressWarnings("Immutable") // Mutable by design
107140
private enum HashCodeTestCase {
108141
NOT_SET(CelMutableExpr.ofNotSet(1L), -722379961),
109-
CONSTANT(CelMutableExpr.ofConstant(2L, CelConstant.ofValue("test")), -724279919);
142+
CONSTANT(CelMutableExpr.ofConstant(2L, CelConstant.ofValue("test")), -724279919),
143+
IDENT(CelMutableExpr.ofIdent("x"), -721379855),
144+
;
110145

111146
private final CelMutableExpr mutableExpr;
112147
private final int expectedHashCode;

0 commit comments

Comments
 (0)