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
104 changes: 104 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 @@ -171,6 +171,10 @@ public int hashCode() {
return name.hashCode();
}

private CelMutableIdent deepCopy() {
return new CelMutableIdent(name);
}

private CelMutableIdent(String name) {
this.name = checkNotNull(name);
}
Expand Down Expand Up @@ -206,6 +210,10 @@ public void setTestOnly(boolean testOnly) {
this.testOnly = testOnly;
}

private CelMutableSelect deepCopy() {
return create(newInstance(operand()), field, testOnly);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
Expand Down Expand Up @@ -321,6 +329,13 @@ public int hashCode() {
return h;
}

private CelMutableCall deepCopy() {
List<CelMutableExpr> copiedArgs = deepCopyList(args);
return target().isPresent()
? create(newInstance(target.get()), function, copiedArgs)
: create(function, copiedArgs);
}

public static CelMutableCall create(String function, CelMutableExpr... args) {
return create(function, Arrays.asList(checkNotNull(args)));
}
Expand Down Expand Up @@ -399,6 +414,10 @@ public int hashCode() {
return h;
}

private CelMutableCreateList deepCopy() {
return create(deepCopyList(elements), optionalIndices);
}

public static CelMutableCreateList create(CelMutableExpr... elements) {
return create(Arrays.asList(checkNotNull(elements)));
}
Expand Down Expand Up @@ -489,6 +508,10 @@ public void setOptionalEntry(boolean optionalEntry) {
this.optionalEntry = optionalEntry;
}

private Entry deepCopy() {
return create(id, fieldKey, newInstance(value), optionalEntry);
}

public static Entry create(long id, String fieldKey, CelMutableExpr value) {
return create(id, fieldKey, value, false);
}
Expand Down Expand Up @@ -557,6 +580,15 @@ public int hashCode() {
return h;
}

private CelMutableCreateStruct deepCopy() {
ArrayList<CelMutableCreateStruct.Entry> copiedEntries = new ArrayList<>();
for (CelMutableCreateStruct.Entry entry : entries) {
copiedEntries.add(entry.deepCopy());
}

return create(messageName, copiedEntries);
}

public static CelMutableCreateStruct create(
String messageName, List<CelMutableCreateStruct.Entry> entries) {
return new CelMutableCreateStruct(messageName, entries);
Expand Down Expand Up @@ -657,6 +689,10 @@ public int hashCode() {
return h;
}

private Entry deepCopy() {
return create(id, newInstance(key), newInstance(value), optionalEntry);
}

public static Entry create(CelMutableExpr key, CelMutableExpr value) {
return create(0, key, value, false);
}
Expand Down Expand Up @@ -698,6 +734,15 @@ public int hashCode() {
return h;
}

private CelMutableCreateMap deepCopy() {
ArrayList<CelMutableCreateMap.Entry> copiedEntries = new ArrayList<>();
for (CelMutableCreateMap.Entry entry : entries) {
copiedEntries.add(entry.deepCopy());
}

return create(copiedEntries);
}

public static CelMutableCreateMap create(List<CelMutableCreateMap.Entry> entries) {
return new CelMutableCreateMap(new ArrayList<>(entries));
}
Expand Down Expand Up @@ -818,6 +863,17 @@ public int hashCode() {
return h;
}

private CelMutableComprehension deepCopy() {
return create(
iterVar,
newInstance(iterRange),
accuVar,
newInstance(accuInit),
newInstance(loopCondition),
newInstance(loopStep),
newInstance(result));
}

public static CelMutableComprehension create(
String iterVar,
CelMutableExpr iterRange,
Expand Down Expand Up @@ -917,6 +973,11 @@ public static CelMutableExpr ofComprehension(
return new CelMutableExpr(id, mutableComprehension);
}

/** Constructs a deep copy of the mutable expression. */
public static CelMutableExpr newInstance(CelMutableExpr other) {
return new CelMutableExpr(other);
}

private CelMutableExpr(long id, CelConstant mutableConstant) {
this.id = id;
setConstant(mutableConstant);
Expand Down Expand Up @@ -967,6 +1028,40 @@ private CelMutableExpr() {
this.exprKind = ExprKind.Kind.NOT_SET;
}

private CelMutableExpr(CelMutableExpr other) {
checkNotNull(other);
this.id = other.id;
this.exprKind = other.exprKind;
switch (other.getKind()) {
case CONSTANT:
this.exprValue = other.exprValue; // Constant is immutable.
break;
case IDENT:
this.exprValue = other.ident().deepCopy();
break;
case SELECT:
this.exprValue = other.select().deepCopy();
break;
case CALL:
this.exprValue = other.call().deepCopy();
break;
case CREATE_LIST:
this.exprValue = other.createList().deepCopy();
break;
case CREATE_STRUCT:
this.exprValue = other.createStruct().deepCopy();
break;
case CREATE_MAP:
this.exprValue = other.createMap().deepCopy();
break;
case COMPREHENSION:
this.exprValue = other.comprehension().deepCopy();
break;
default:
throw new IllegalStateException("Unexpected expr kind: " + this.exprKind);
}
}

private Object exprValue() {
switch (this.exprKind) {
case NOT_SET:
Expand All @@ -992,6 +1087,15 @@ private Object exprValue() {
throw new IllegalStateException("Unexpected expr kind: " + this.exprKind);
}

private static List<CelMutableExpr> deepCopyList(List<CelMutableExpr> elements) {
ArrayList<CelMutableExpr> copiedArgs = new ArrayList<>();
for (CelMutableExpr arg : elements) {
copiedArgs.add(newInstance(arg));
}

return copiedArgs;
}

private void checkExprKind(ExprKind.Kind exprKind) {
checkArgument(this.exprKind.equals(exprKind), "Invalid ExprKind: %s", exprKind);
}
Expand Down
Loading