Skip to content

Commit

Permalink
Add CelMutableCall
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 622330817
  • Loading branch information
l46kok authored and copybara-github committed Apr 10, 2024
1 parent 33a7b6f commit 1d0da3f
Show file tree
Hide file tree
Showing 2 changed files with 428 additions and 0 deletions.
230 changes: 230 additions & 0 deletions common/src/main/java/dev/cel/common/ast/CelMutableExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import dev.cel.common.ast.CelExpr.CelNotSet;
import dev.cel.common.ast.CelExpr.ExprKind;
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
* An abstract representation of a common expression that allows mutation in any of its properties.
Expand All @@ -36,6 +41,8 @@ public final class CelMutableExpr {
private CelNotSet notSet;
private CelConstant constant;
private CelMutableIdent ident;
private CelMutableSelect select;
private CelMutableCall call;
private int hash = 0;

public long id() {
Expand Down Expand Up @@ -65,6 +72,16 @@ public CelMutableIdent ident() {
return ident;
}

public CelMutableSelect select() {
checkExprKind(Kind.SELECT);
return select;
}

public CelMutableCall call() {
checkExprKind(Kind.CALL);
return call;
}

public void setConstant(CelConstant constant) {
this.exprKind = ExprKind.Kind.CONSTANT;
this.constant = checkNotNull(constant);
Expand All @@ -75,6 +92,16 @@ public void setIdent(CelMutableIdent ident) {
this.ident = checkNotNull(ident);
}

public void setSelect(CelMutableSelect select) {
this.exprKind = ExprKind.Kind.SELECT;
this.select = checkNotNull(select);
}

public void setCall(CelMutableCall call) {
this.exprKind = ExprKind.Kind.CALL;
this.call = checkNotNull(call);
}

/** A mutable identifier expression. */
public static final class CelMutableIdent {
private String name = "";
Expand Down Expand Up @@ -114,6 +141,181 @@ private CelMutableIdent(String name) {
}
}

/** A mutable field selection expression. e.g. `request.auth`. */
public static final class CelMutableSelect {
private CelMutableExpr operand;
private String field = "";
private boolean testOnly;

public CelMutableExpr operand() {
return operand;
}

public void setOperand(CelMutableExpr operand) {
this.operand = checkNotNull(operand);
}

public String field() {
return field;
}

public void setField(String field) {
this.field = checkNotNull(field);
}

public boolean testOnly() {
return testOnly;
}

public void setTestOnly(boolean testOnly) {
this.testOnly = testOnly;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof CelMutableSelect) {
CelMutableSelect that = (CelMutableSelect) obj;
return this.operand.equals(that.operand())
&& this.field.equals(that.field())
&& this.testOnly == that.testOnly();
}
return false;
}

@Override
public int hashCode() {
int h = 1;
h *= 1000003;
h ^= operand.hashCode();
h *= 1000003;
h ^= field.hashCode();
h *= 1000003;
h ^= testOnly ? 1231 : 1237;
return h;
}

public static CelMutableSelect create(CelMutableExpr operand, String field) {
return new CelMutableSelect(operand, field, false);
}

public static CelMutableSelect create(CelMutableExpr operand, String field, boolean testOnly) {
return new CelMutableSelect(operand, field, testOnly);
}

private CelMutableSelect(CelMutableExpr operand, String field, boolean testOnly) {
this.operand = checkNotNull(operand);
this.field = checkNotNull(field);
this.testOnly = testOnly;
}
}

/** A mutable call expression, including calls to predefined functions and operators. */
public static final class CelMutableCall {
private Optional<CelMutableExpr> target;
private String function;
private List<CelMutableExpr> args;

public Optional<CelMutableExpr> target() {
return target;
}

public void setTarget(CelMutableExpr target) {
this.target = Optional.of(target);
}

public String function() {
return function;
}

public void setFunction(String function) {
this.function = checkNotNull(function);
}

public List<CelMutableExpr> args() {
return args;
}

public void clearArgs() {
args.clear();
}

public void addArgs(CelMutableExpr... exprs) {
addArgs(Arrays.asList(checkNotNull(exprs)));
}

public void addArgs(Iterable<CelMutableExpr> exprs) {
exprs.forEach(e -> args.add(checkNotNull(e)));
}

public void setArgs(Collection<CelMutableExpr> exprs) {
this.args = new ArrayList<>(checkNotNull(exprs));
}

public void setArg(int index, CelMutableExpr arg) {
checkArgument(index >= 0 && index < args.size());
args.set(index, checkNotNull(arg));
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof CelMutableCall) {
CelMutableCall that = (CelMutableCall) obj;
return this.target.equals(that.target())
&& this.function.equals(that.function())
&& this.args.equals(that.args());
}

return false;
}

@Override
public int hashCode() {
int h = 1;
h *= 1000003;
h ^= target.hashCode();
h *= 1000003;
h ^= function.hashCode();
h *= 1000003;
h ^= args.hashCode();
return h;
}

public static CelMutableCall create(String function, CelMutableExpr... args) {
return create(function, Arrays.asList(checkNotNull(args)));
}

public static CelMutableCall create(String function, List<CelMutableExpr> args) {
return new CelMutableCall(function, args);
}

public static CelMutableCall create(
CelMutableExpr target, String function, CelMutableExpr... args) {
return create(target, function, Arrays.asList(checkNotNull(args)));
}

public static CelMutableCall create(
CelMutableExpr target, String function, List<CelMutableExpr> args) {
return new CelMutableCall(target, function, args);
}

private CelMutableCall(String function, List<CelMutableExpr> args) {
this.target = Optional.empty();
this.function = checkNotNull(function);
this.args = new ArrayList<>(checkNotNull(args));
}

private CelMutableCall(CelMutableExpr target, String function, List<CelMutableExpr> args) {
this(function, args);
this.target = Optional.of(target);
}
}

public static CelMutableExpr ofNotSet() {
return ofNotSet(0L);
}
Expand All @@ -138,6 +340,22 @@ public static CelMutableExpr ofIdent(long id, String name) {
return new CelMutableExpr(id, CelMutableIdent.create(name));
}

public static CelMutableExpr ofSelect(CelMutableSelect mutableSelect) {
return ofSelect(0, mutableSelect);
}

public static CelMutableExpr ofSelect(long id, CelMutableSelect mutableSelect) {
return new CelMutableExpr(id, mutableSelect);
}

public static CelMutableExpr ofCall(CelMutableCall mutableCall) {
return ofCall(0, mutableCall);
}

public static CelMutableExpr ofCall(long id, CelMutableCall mutableCall) {
return new CelMutableExpr(id, mutableCall);
}

private CelMutableExpr(long id, CelConstant mutableConstant) {
this.id = id;
setConstant(mutableConstant);
Expand All @@ -148,6 +366,16 @@ private CelMutableExpr(long id, CelMutableIdent mutableIdent) {
setIdent(mutableIdent);
}

private CelMutableExpr(long id, CelMutableSelect mutableSelect) {
this.id = id;
setSelect(mutableSelect);
}

private CelMutableExpr(long id, CelMutableCall mutableCall) {
this.id = id;
setCall(mutableCall);
}

private CelMutableExpr(long id) {
this();
this.id = id;
Expand All @@ -167,7 +395,9 @@ private Object exprValue() {
case IDENT:
return ident();
case SELECT:
return select();
case CALL:
return call();
case CREATE_LIST:
case CREATE_STRUCT:
case CREATE_MAP:
Expand Down
Loading

0 comments on commit 1d0da3f

Please sign in to comment.