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
10 changes: 9 additions & 1 deletion testing/src/main/java/dev/cel/testing/utils/ExprValueUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.TypeType;
import java.io.IOException;
Expand Down Expand Up @@ -144,6 +145,9 @@ public static ExprValue toExprValue(Object object, CelType type) throws Exceptio
*/
@SuppressWarnings("unchecked")
public static Value toValue(Object object, CelType type) throws Exception {
if (!(object instanceof Optional) && type instanceof OptionalType) {
return toValue(object, type.parameters().get(0));
}
if (object == null) {
object = NullValue.NULL_VALUE;
}
Expand Down Expand Up @@ -230,7 +234,11 @@ public static Value toValue(Object object, CelType type) throws Exception {
}

if (object instanceof Optional) {
return toValue(((Optional<?>) object).get(), type);
// TODO: Remove this once the ExprValue Native representation is added.
if (!((Optional<?>) object).isPresent()) {
return Value.getDefaultInstance();
}
return toValue(((Optional<?>) object).get(), type.parameters().get(0));
}

throw new IllegalArgumentException(
Expand Down
19 changes: 19 additions & 0 deletions testing/src/test/resources/policy/context_pb/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: "context_pb"
container: "cel.expr.conformance.proto3"
extensions:
- name: "strings"
version: "latest"
25 changes: 25 additions & 0 deletions testing/src/test/resources/policy/context_pb/policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: "context_pb"
rule:
match:
- condition: >
single_int32 > TestAllTypes{single_int64: 10}.single_int64
output: |
["invalid spec, got single_int32=" , single_int32 , ", wanted <= 10"].join()
- condition: >
standalone_enum == TestAllTypes.NestedEnum.BAR
output: |
"invalid spec, no nested enums may refer to BAR"
35 changes: 35 additions & 0 deletions testing/src/test/resources/policy/context_pb/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: "context_pb_cel_tests"
description: "Protobuf input tests"
sections:
- name: "valid"
description: "Valid context_expr"
tests:
- name: "good spec"
description: "good spec"
context_expr:
"TestAllTypes{single_int32: 10}"
output:
expr: "optional.none()"
- name: "invalid"
description: "Invalid context_expr"
tests:
- name: "bad spec"
description: "bad spec"
context_expr:
"TestAllTypes{single_int32: 11}"
output:
value: "invalid spec, got single_int32=11, wanted <= 10"
Loading