Skip to content

Commit

Permalink
Add MutableAst
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 566741837
  • Loading branch information
l46kok authored and copybara-github committed Sep 19, 2023
1 parent e53e6ac commit 8cb805a
Show file tree
Hide file tree
Showing 14 changed files with 761 additions and 24 deletions.
125 changes: 120 additions & 5 deletions common/src/main/java/dev/cel/common/ast/CelExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,80 @@ public abstract static class Builder {

public abstract Builder setExprKind(ExprKind value);

public abstract ExprKind exprKind();

/**
* Gets the underlying constant expression.
*
* @throws UnsupportedOperationException if expression is not {@link Kind#CONSTANT}.
*/
public CelConstant constant() {
return exprKind().constant();
}

/**
* Gets the underlying identifier expression.
*
* @throws UnsupportedOperationException if expression is not {@link Kind#IDENT}.
*/
public CelIdent ident() {
return exprKind().ident();
}

/**
* Gets the underlying select expression.
*
* @throws UnsupportedOperationException if expression is not {@link Kind#SELECT}.
*/
public CelSelect select() {
return exprKind().select();
}

/**
* Gets the underlying call expression.
*
* @throws UnsupportedOperationException if expression is not {@link Kind#CALL}.
*/
public CelCall call() {
return exprKind().call();
}

/**
* Gets the underlying createList expression.
*
* @throws UnsupportedOperationException if expression is not {@link Kind#CREATE_LIST}.
*/
public CelCreateList createList() {
return exprKind().createList();
}

/**
* Gets the underlying createStruct expression.
*
* @throws UnsupportedOperationException if expression is not {@link Kind#CREATE_STRUCT}.
*/
public CelCreateStruct createStruct() {
return exprKind().createStruct();
}

/**
* Gets the underlying createMap expression.
*
* @throws UnsupportedOperationException if expression is not {@link Kind#createMap}.
*/
public CelCreateMap createMap() {
return exprKind().createMap();
}

/**
* Gets the underlying comprehension expression.
*
* @throws UnsupportedOperationException if expression is not {@link Kind#COMPREHENSION}.
*/
public CelComprehension comprehension() {
return exprKind().comprehension();
}

public Builder setConstant(CelConstant constant) {
return setExprKind(AutoOneOf_CelExpr_ExprKind.constant(constant));
}
Expand Down Expand Up @@ -373,6 +447,11 @@ public abstract static class CelSelect {
/** Builder for CelSelect. */
@AutoValue.Builder
public abstract static class Builder {
public abstract CelExpr operand();

public abstract String field();

public abstract boolean testOnly();

public abstract Builder setOperand(CelExpr value);

Expand Down Expand Up @@ -418,16 +497,18 @@ public abstract static class CelCall {
/** Builder for CelCall. */
@AutoValue.Builder
public abstract static class Builder {
List<CelExpr> mutableArgs = new ArrayList<>();
private List<CelExpr> mutableArgs = new ArrayList<>();

abstract ImmutableList<CelExpr> args();
public abstract ImmutableList<CelExpr> args();

public abstract Builder setTarget(CelExpr value);

public abstract Builder setTarget(Optional<CelExpr> value);

public abstract Builder setFunction(String value);

public abstract Optional<CelExpr> target();

// Not public. This only exists to make AutoValue.Builder work.
abstract Builder setArgs(ImmutableList<CelExpr> value);

Expand Down Expand Up @@ -501,16 +582,23 @@ public abstract static class CelCreateList {
/** Builder for CelCreateList. */
@AutoValue.Builder
public abstract static class Builder {
List<CelExpr> mutableElements = new ArrayList<>();
private List<CelExpr> mutableElements = new ArrayList<>();

// Not public. This only exists to make AutoValue.Builder work.
abstract ImmutableList<CelExpr> elements();

// Not public. This only exists to make AutoValue.Builder work.
abstract ImmutableList.Builder<Integer> optionalIndicesBuilder();

// Not public. This only exists to make AutoValue.Builder work.
@CanIgnoreReturnValue
abstract Builder setElements(ImmutableList<CelExpr> elements);

/** Returns an immutable copy of the current mutable elements present in the builder. */
public ImmutableList<CelExpr> getElements() {
return ImmutableList.copyOf(mutableElements);
}

@CanIgnoreReturnValue
public Builder setElement(int index, CelExpr element) {
checkNotNull(element);
Expand Down Expand Up @@ -586,8 +674,9 @@ public abstract static class CelCreateStruct {
/** Builder for CelCreateStruct. */
@AutoValue.Builder
public abstract static class Builder {
List<CelCreateStruct.Entry> mutableEntries = new ArrayList<>();
private List<CelCreateStruct.Entry> mutableEntries = new ArrayList<>();

// Not public. This only exists to make AutoValue.Builder work.
abstract ImmutableList<CelCreateStruct.Entry> entries();

@CanIgnoreReturnValue
Expand All @@ -597,6 +686,11 @@ public abstract static class Builder {
@CanIgnoreReturnValue
abstract Builder setEntries(ImmutableList<CelCreateStruct.Entry> entries);

/** Returns an immutable copy of the current mutable entries present in the builder. */
public ImmutableList<CelCreateStruct.Entry> getEntries() {
return ImmutableList.copyOf(mutableEntries);
}

@CanIgnoreReturnValue
public Builder setEntry(int index, CelCreateStruct.Entry entry) {
checkNotNull(entry);
Expand Down Expand Up @@ -669,6 +763,8 @@ public abstract static class Entry {
@AutoValue.Builder
public abstract static class Builder {

public abstract CelExpr value();

public abstract Builder setId(long value);

public abstract Builder setFieldKey(String value);
Expand Down Expand Up @@ -704,14 +800,20 @@ public abstract static class CelCreateMap {
@AutoValue.Builder
public abstract static class Builder {

List<CelCreateMap.Entry> mutableEntries = new ArrayList<>();
private List<CelCreateMap.Entry> mutableEntries = new ArrayList<>();

// Not public. This only exists to make AutoValue.Builder work.
abstract ImmutableList<CelCreateMap.Entry> entries();

// Not public. This only exists to make AutoValue.Builder work.
@CanIgnoreReturnValue
abstract Builder setEntries(ImmutableList<CelCreateMap.Entry> entries);

/** Returns an immutable copy of the current mutable entries present in the builder. */
public ImmutableList<CelCreateMap.Entry> getEntries() {
return ImmutableList.copyOf(mutableEntries);
}

@CanIgnoreReturnValue
public Builder setEntry(int index, CelCreateMap.Entry entry) {
checkNotNull(entry);
Expand Down Expand Up @@ -784,6 +886,10 @@ public abstract static class Entry {
@AutoValue.Builder
public abstract static class Builder {

public abstract CelExpr key();

public abstract CelExpr value();

public abstract CelCreateMap.Entry.Builder setId(long value);

public abstract CelCreateMap.Entry.Builder setKey(CelExpr value);
Expand Down Expand Up @@ -868,6 +974,15 @@ public abstract static class CelComprehension {
/** Builder for Comprehension. */
@AutoValue.Builder
public abstract static class Builder {
public abstract CelExpr iterRange();

public abstract CelExpr accuInit();

public abstract CelExpr loopCondition();

public abstract CelExpr loopStep();

public abstract CelExpr result();

public abstract Builder setIterVar(String value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
import java.util.HashMap;

/** Factory for populating expression IDs */
final class CelExprIdGeneratorFactory {
public final class CelExprIdGeneratorFactory {

/** MonotonicIdGenerator increments expression IDs from an initial seed value. */
static CelExprIdGenerator newMonotonicIdGenerator(long exprId) {
/**
* MonotonicIdGenerator increments expression IDs from an initial seed value.
*
* @param exprId Seed value. Must be non-negative. For example, if 1 is provided {@link
* CelExprIdGenerator#nextExprId} will return 2.
*/
public static CelExprIdGenerator newMonotonicIdGenerator(long exprId) {
return new MonotonicIdGenerator(exprId);
}

Expand Down
23 changes: 23 additions & 0 deletions common/src/test/java/dev/cel/common/ast/CelExprTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void celExprBuilder_setConstant() {
CelExpr celExpr = CelExpr.newBuilder().setConstant(celConstant).build();

assertThat(celExpr.constant()).isEqualTo(celConstant);
assertThat(celExpr.toBuilder().constant()).isEqualTo(celConstant);
}

@Test
Expand All @@ -119,6 +120,7 @@ public void celExprBuilder_setIdent() {
CelExpr celExpr = CelExpr.newBuilder().setIdent(celIdent).build();

assertThat(celExpr.ident()).isEqualTo(celIdent);
assertThat(celExpr.toBuilder().ident()).isEqualTo(celIdent);
}

@Test
Expand All @@ -131,6 +133,7 @@ public void celExprBuilder_setCall() {
CelExpr celExpr = CelExpr.newBuilder().setCall(celCall).build();

assertThat(celExpr.call()).isEqualTo(celCall);
assertThat(celExpr.toBuilder().call()).isEqualTo(celCall);
}

@Test
Expand All @@ -144,6 +147,8 @@ public void celExprBuilder_setCall_clearTarget() {
CelExpr.newBuilder().setCall(celCall.toBuilder().clearTarget().build()).build();

assertThat(celExpr.call()).isEqualTo(CelCall.newBuilder().setFunction("function").build());
assertThat(celExpr.toBuilder().call())
.isEqualTo(CelCall.newBuilder().setFunction("function").build());
}

@Test
Expand Down Expand Up @@ -182,6 +187,7 @@ public void celExprBuilder_setSelect() {

assertThat(celExpr.select().testOnly()).isFalse();
assertThat(celExpr.select()).isEqualTo(celSelect);
assertThat(celExpr.toBuilder().select()).isEqualTo(celSelect);
}

@Test
Expand All @@ -193,6 +199,7 @@ public void celExprBuilder_setCreateList() {
CelExpr celExpr = CelExpr.newBuilder().setCreateList(celCreateList).build();

assertThat(celExpr.createList()).isEqualTo(celCreateList);
assertThat(celExpr.toBuilder().createList()).isEqualTo(celCreateList);
}

@Test
Expand Down Expand Up @@ -236,6 +243,7 @@ public void celExprBuilder_setCreateStruct() {

assertThat(celExpr.createStruct().entries().get(0).optionalEntry()).isFalse();
assertThat(celExpr.createStruct()).isEqualTo(celCreateStruct);
assertThat(celExpr.toBuilder().createStruct()).isEqualTo(celCreateStruct);
}

@Test
Expand Down Expand Up @@ -309,37 +317,52 @@ public void celExprBuilder_setComprehension() {
CelExpr celExpr = CelExpr.newBuilder().setComprehension(celComprehension).build();

assertThat(celExpr.comprehension()).isEqualTo(celComprehension);
assertThat(celExpr.toBuilder().comprehension()).isEqualTo(celComprehension);
}

@Test
public void getUnderlyingExpression_unmatchedKind_throws(
@TestParameter BuilderExprKindTestCase testCase) {
if (!testCase.expectedExprKind.equals(Kind.NOT_SET)) {
assertThrows(UnsupportedOperationException.class, () -> testCase.expr.exprKind().notSet());
assertThrows(
UnsupportedOperationException.class, () -> testCase.expr.toBuilder().exprKind().notSet());
}
if (!testCase.expectedExprKind.equals(Kind.CONSTANT)) {
assertThrows(UnsupportedOperationException.class, testCase.expr::constant);
assertThrows(UnsupportedOperationException.class, () -> testCase.expr.toBuilder().constant());
}
if (!testCase.expectedExprKind.equals(Kind.IDENT)) {
assertThrows(UnsupportedOperationException.class, testCase.expr::ident);
assertThrows(UnsupportedOperationException.class, () -> testCase.expr.toBuilder().ident());
}
if (!testCase.expectedExprKind.equals(Kind.SELECT)) {
assertThrows(UnsupportedOperationException.class, testCase.expr::select);
assertThrows(UnsupportedOperationException.class, () -> testCase.expr.toBuilder().select());
}
if (!testCase.expectedExprKind.equals(Kind.CALL)) {
assertThrows(UnsupportedOperationException.class, testCase.expr::call);
assertThrows(UnsupportedOperationException.class, () -> testCase.expr.toBuilder().call());
}
if (!testCase.expectedExprKind.equals(Kind.CREATE_LIST)) {
assertThrows(UnsupportedOperationException.class, testCase.expr::createList);
assertThrows(
UnsupportedOperationException.class, () -> testCase.expr.toBuilder().createList());
}
if (!testCase.expectedExprKind.equals(Kind.CREATE_STRUCT)) {
assertThrows(UnsupportedOperationException.class, testCase.expr::createStruct);
assertThrows(
UnsupportedOperationException.class, () -> testCase.expr.toBuilder().createStruct());
}
if (!testCase.expectedExprKind.equals(Kind.CREATE_MAP)) {
assertThrows(UnsupportedOperationException.class, testCase.expr::createMap);
assertThrows(
UnsupportedOperationException.class, () -> testCase.expr.toBuilder().createMap());
}
if (!testCase.expectedExprKind.equals(Kind.COMPREHENSION)) {
assertThrows(UnsupportedOperationException.class, testCase.expr::comprehension);
assertThrows(
UnsupportedOperationException.class, () -> testCase.expr.toBuilder().comprehension());
}
}

Expand Down
12 changes: 12 additions & 0 deletions optimizer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ java_library(
exports = ["//optimizer/src/main/java/dev/cel/optimizer:ast_optimizer"],
)

java_library(
name = "optimization_exception",
exports = ["//optimizer/src/main/java/dev/cel/optimizer:optimization_exception"],
)

java_library(
name = "mutable_ast",
testonly = 1,
visibility = ["//optimizer/src/test/java/dev/cel/optimizer:__pkg__"],
exports = ["//optimizer/src/main/java/dev/cel/optimizer:mutable_ast"],
)

java_library(
name = "optimizer_impl",
testonly = 1,
Expand Down
Loading

0 comments on commit 8cb805a

Please sign in to comment.