Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,45 @@ public void traditionalIndex_onOptionalList_returnsOptionalEmpty() throws Except
assertThat(result).isEqualTo(Optional.empty());
}

@Test
// LHS
@TestParameters("{expression: 'optx.or(optional.of(1))'}")
@TestParameters("{expression: 'optx.orValue(1)'}")
// RHS
@TestParameters("{expression: 'optional.none().or(optx)'}")
@TestParameters("{expression: 'optional.none().orValue(optx)'}")
public void optionalChainedFunctions_lhsIsUnknown_returnsUnknown(String expression)
throws Exception {
Cel cel =
newCelBuilder()
.addVar("optx", OptionalType.create(SimpleType.INT))
.addVar("x", SimpleType.INT)
.build();
CelAbstractSyntaxTree ast = cel.compile(expression).getAst();

Object result = cel.createProgram(ast).eval();

assertThat(InterpreterUtil.isUnknown(result)).isTrue();
}

@Test
// LHS
@TestParameters("{expression: 'optional.of(1/0).or(optional.of(1))'}")
@TestParameters("{expression: 'optional.of(1/0).orValue(1)'}")
// RHS
@TestParameters("{expression: 'optional.none().or(optional.of(1/0))'}")
@TestParameters("{expression: 'optional.none().orValue(1/0)'}")
public void optionalChainedFunctions_lhsIsError_returnsError(String expression) throws Exception {
Cel cel =
newCelBuilder()
.addVar("optx", OptionalType.create(SimpleType.INT))
.addVar("x", SimpleType.INT)
.build();
CelAbstractSyntaxTree ast = cel.compile(expression).getAst();

assertThrows(CelEvaluationException.class, () -> cel.createProgram(ast).eval());
}

@Test
public void traditionalIndex_onOptionalList_returnsOptionalValue() throws Exception {
Cel cel =
Expand Down
38 changes: 18 additions & 20 deletions 38 runtime/src/main/java/dev/cel/runtime/DefaultInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,30 +680,28 @@ private IntermediateResult evalType(ExecutionFrame frame, CelCall callExpr)

private IntermediateResult evalOptionalOr(ExecutionFrame frame, CelCall callExpr)
throws CelEvaluationException {
CelExpr lhsExpr = callExpr.target().get();
IntermediateResult lhsResult = evalInternal(frame, lhsExpr);
if (!(lhsResult.value() instanceof Optional)) {
throw CelEvaluationExceptionBuilder.newBuilder(
"expected optional value, found: %s", lhsResult.value())
.setErrorCode(CelErrorCode.INVALID_ARGUMENT)
.setMetadata(metadata, lhsExpr.id())
.build();
}

Optional<?> lhsOptionalValue = (Optional<?>) lhsResult.value();

if (lhsOptionalValue.isPresent()) {
// Short-circuit lhs if a value exists
return lhsResult;
}

return evalInternal(frame, callExpr.args().get(0));
return evalOptionalOrInternal(frame, callExpr, /* unwrapOptional= */ false);
}

private IntermediateResult evalOptionalOrValue(ExecutionFrame frame, CelCall callExpr)
throws CelEvaluationException {
CelExpr lhsExpr = callExpr.target().get();
return evalOptionalOrInternal(frame, callExpr, /* unwrapOptional= */ true);
}

private IntermediateResult evalOptionalOrInternal(
ExecutionFrame frame, CelCall callExpr, boolean unwrapOptional)
throws CelEvaluationException {
CelExpr lhsExpr =
callExpr
.target()
.orElseThrow(
() -> new IllegalStateException("Missing target for chained optional function"));
IntermediateResult lhsResult = evalInternal(frame, lhsExpr);

if (isUnknownValue(lhsResult.value())) {
return lhsResult;
}

if (!(lhsResult.value() instanceof Optional)) {
throw CelEvaluationExceptionBuilder.newBuilder(
"expected optional value, found: %s", lhsResult.value())
Expand All @@ -716,7 +714,7 @@ private IntermediateResult evalOptionalOrValue(ExecutionFrame frame, CelCall cal

if (lhsOptionalValue.isPresent()) {
// Short-circuit lhs if a value exists
return IntermediateResult.create(lhsOptionalValue.get());
return unwrapOptional ? IntermediateResult.create(lhsOptionalValue.get()) : lhsResult;
}

return evalInternal(frame, callExpr.args().get(0));
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.