From 33a7b6f6a51a9dfde6897a4f2e907c316011a068 Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Wed, 10 Apr 2024 15:14:19 -0700 Subject: [PATCH] Add CelMutableIdent PiperOrigin-RevId: 623619641 --- .../dev/cel/common/ast/CelMutableExpr.java | 75 +++++++++++++++++-- .../cel/common/ast/CelMutableExprTest.java | 39 +++++++++- 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/dev/cel/common/ast/CelMutableExpr.java b/common/src/main/java/dev/cel/common/ast/CelMutableExpr.java index 45e45852c..db3a6286a 100644 --- a/common/src/main/java/dev/cel/common/ast/CelMutableExpr.java +++ b/common/src/main/java/dev/cel/common/ast/CelMutableExpr.java @@ -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; @@ -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() { @@ -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() { @@ -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; @@ -101,6 +165,7 @@ private Object exprValue() { case CONSTANT: return constant(); case IDENT: + return ident(); case SELECT: case CALL: case CREATE_LIST: diff --git a/common/src/test/java/dev/cel/common/ast/CelMutableExprTest.java b/common/src/test/java/dev/cel/common/ast/CelMutableExprTest.java index 97304fd7d..3d44ac857 100644 --- a/common/src/test/java/dev/cel/common/ast/CelMutableExprTest.java +++ b/common/src/test/java/dev/cel/common/ast/CelMutableExprTest.java @@ -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; @@ -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)); @@ -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; @@ -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;