Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions common/src/main/java/dev/cel/common/ast/CelMutableExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package dev.cel.common.ast;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import dev.cel.common.ast.CelExpr.CelNotSet;
import dev.cel.common.ast.CelExpr.ExprKind;
Expand All @@ -34,6 +35,7 @@ public final class CelMutableExpr {
private ExprKind.Kind exprKind;
private CelNotSet notSet;
private CelConstant constant;
private CelMutableIdent ident;
private int hash = 0;

public long id() {
Expand All @@ -58,17 +60,58 @@ public CelConstant constant() {
return constant;
}

public CelMutableIdent ident() {
checkExprKind(Kind.IDENT);
return ident;
}

public void setConstant(CelConstant constant) {
this.exprKind = ExprKind.Kind.CONSTANT;
this.constant = constant;
this.constant = checkNotNull(constant);
}

public static CelMutableExpr ofConstant(CelConstant constant) {
return ofConstant(0L, constant);
public void setIdent(CelMutableIdent ident) {
this.exprKind = ExprKind.Kind.IDENT;
this.ident = checkNotNull(ident);
}

public static CelMutableExpr ofConstant(long id, CelConstant constant) {
return new CelMutableExpr(id, constant);
/** A mutable identifier expression. */
public static final class CelMutableIdent {
private String name = "";

public String name() {
return name;
}

public void setName(String name) {
this.name = checkNotNull(name);
}

public static CelMutableIdent create(String name) {
return new CelMutableIdent(name);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof CelMutableIdent) {
CelMutableIdent that = (CelMutableIdent) obj;
return this.name.equals(that.name);
}

return false;
}

@Override
public int hashCode() {
return name.hashCode();
}

private CelMutableIdent(String name) {
this.name = checkNotNull(name);
}
}

public static CelMutableExpr ofNotSet() {
Expand All @@ -79,11 +122,32 @@ public static CelMutableExpr ofNotSet(long id) {
return new CelMutableExpr(id);
}

public static CelMutableExpr ofConstant(CelConstant constant) {
return ofConstant(0L, constant);
}

public static CelMutableExpr ofConstant(long id, CelConstant constant) {
return new CelMutableExpr(id, constant);
}

public static CelMutableExpr ofIdent(String name) {
return ofIdent(0, name);
}

public static CelMutableExpr ofIdent(long id, String name) {
return new CelMutableExpr(id, CelMutableIdent.create(name));
}

private CelMutableExpr(long id, CelConstant mutableConstant) {
this.id = id;
setConstant(mutableConstant);
}

private CelMutableExpr(long id, CelMutableIdent mutableIdent) {
this.id = id;
setIdent(mutableIdent);
}

private CelMutableExpr(long id) {
this();
this.id = id;
Expand All @@ -101,6 +165,7 @@ private Object exprValue() {
case CONSTANT:
return constant();
case IDENT:
return ident();
case SELECT:
case CALL:
case CREATE_LIST:
Expand Down
39 changes: 37 additions & 2 deletions common/src/test/java/dev/cel/common/ast/CelMutableExprTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
import dev.cel.common.ast.CelMutableExpr.CelMutableIdent;
import org.junit.Test;
import org.junit.runner.RunWith;

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

@Test
public void ofIdent() {
CelMutableExpr mutableExpr = CelMutableExpr.ofIdent("x");

assertThat(mutableExpr.id()).isEqualTo(0L);
assertThat(mutableExpr.ident().name()).isEqualTo("x");
}

@Test
public void ofIdent_withId() {
CelMutableExpr mutableExpr = CelMutableExpr.ofIdent(1L, "x");

assertThat(mutableExpr.id()).isEqualTo(1L);
assertThat(mutableExpr.ident().name()).isEqualTo("x");
}

@Test
public void mutableIdent_setName() {
CelMutableIdent ident = CelMutableIdent.create("x");

ident.setName("y");

assertThat(ident.name()).isEqualTo("y");
}

@Test
public void setId_success() {
CelMutableExpr mutableExpr = CelMutableExpr.ofConstant(CelConstant.ofValue(5L));
Expand All @@ -77,13 +103,17 @@ public void equalityTest() {
.addEqualityGroup(
CelMutableExpr.ofConstant(5L, CelConstant.ofValue("hello")),
CelMutableExpr.ofConstant(5L, CelConstant.ofValue("hello")))
.addEqualityGroup(CelMutableExpr.ofIdent("x"))
.addEqualityGroup(CelMutableExpr.ofIdent(2L, "y"), CelMutableExpr.ofIdent(2L, "y"))
.testEquals();
}

@SuppressWarnings("Immutable") // Mutable by design
private enum MutableExprKindTestCase {
NOT_SET(CelMutableExpr.ofNotSet(1L)),
CONSTANT(CelMutableExpr.ofConstant(CelConstant.ofValue(2L)));
CONSTANT(CelMutableExpr.ofConstant(CelConstant.ofValue(2L))),
IDENT(CelMutableExpr.ofIdent("test")),
;

private final CelMutableExpr mutableExpr;

Expand All @@ -101,12 +131,17 @@ public void getExprValue_invalidKind_throws(@TestParameter MutableExprKindTestCa
if (!testCaseKind.equals(Kind.CONSTANT)) {
assertThrows(IllegalArgumentException.class, testCase.mutableExpr::constant);
}
if (!testCaseKind.equals(Kind.IDENT)) {
assertThrows(IllegalArgumentException.class, testCase.mutableExpr::ident);
}
}

@SuppressWarnings("Immutable") // Mutable by design
private enum HashCodeTestCase {
NOT_SET(CelMutableExpr.ofNotSet(1L), -722379961),
CONSTANT(CelMutableExpr.ofConstant(2L, CelConstant.ofValue("test")), -724279919);
CONSTANT(CelMutableExpr.ofConstant(2L, CelConstant.ofValue("test")), -724279919),
IDENT(CelMutableExpr.ofIdent("x"), -721379855),
;

private final CelMutableExpr mutableExpr;
private final int expectedHashCode;
Expand Down