# proto-file: ../../../proto/cel/expr/conformance/test/simple.proto # proto-message: cel.expr.conformance.test.SimpleTestFile name: "optionals" description: "Tests for optionals." section: { name: "optionals" test { name: "null" expr: "optional.of(null).hasValue()" value: { bool_value: true } } test { name: "null_non_zero_value" expr: "optional.ofNonZeroValue(null).hasValue()" value: { bool_value: false } } test { name: "none_or_none_or_value" expr: "optional.none().or(optional.none()).orValue(42)" value: { int64_value: 42 } } test { name: "none_optMap_hasValue" expr: "optional.none().optMap(y, y + 1).hasValue()" value: { bool_value: false } } test { name: "empty_map_optFlatMap_hasValue" expr: "{}.?key.optFlatMap(k, k.?subkey).hasValue()" value: { bool_value: false } } test { name: "map_empty_submap_optFlatMap_hasValue" expr: "{'key': {}}.?key.optFlatMap(k, k.?subkey).hasValue()" value: { bool_value: false } } test { name: "map_null_entry_hasValue" expr: "{'null_key': dyn(null)}.?null_key.hasValue()" value: { bool_value: true } } test { name: "map_null_entry_no_such_key" expr: "{'null_key': dyn(null)}.?null_key.invalid.hasValue()" eval_error: { errors: { message: "no such key" } } } test { name: "map_absent_key_absent_field_none" expr: "{true: dyn(0)}[?false].absent.hasValue()" value: { bool_value: false } } test { name: "map_present_key_invalid_field" expr: "{true: dyn(0)}[?true].absent.hasValue()" eval_error: { errors: { message: "no such key" } } } test { name: "map_undefined_entry_hasValue" expr: "{}.?null_key.invalid.hasValue()" value: { bool_value: false } } test { name: "map_submap_subkey_optFlatMap_value" expr: "{'key': {'subkey': 'subvalue'}}.?key.optFlatMap(k, k.?subkey).value()" value: { string_value: "subvalue" } } test { name: "map_submap_optFlatMap_value" expr: "{'key': {'subkey': ''}}.?key.optFlatMap(k, k.?subkey).value()" value: { string_value: "" } } test { name: "map_optindex_optFlatMap_optional_ofNonZeroValue_hasValue" expr: "{'key': {'subkey': ''}}.?key.optFlatMap(k, optional.ofNonZeroValue(k.subkey)).hasValue()" value: { bool_value: false } } test { name: "optional_of_optMap_value" expr: "optional.of(42).optMap(y, y + 1).value()" value: { int64_value: 43 } } test { name: "optional_ofNonZeroValue_or_optional_value" expr: "optional.ofNonZeroValue(42).or(optional.of(20)).value() == 42" value: { bool_value: true } } test { name: "ternary_optional_hasValue" expr: "(has({}.x) ? optional.of({}.x) : optional.none()).hasValue()" value: { bool_value: false } } test { name: "map_optindex_hasValue" expr: "{}.?x.hasValue()" value: { bool_value: false } } test { name: "has_map_optindex" expr: "has({}.?x.y)" value: { bool_value: false } } test { name: "has_map_optindex_field" expr: "has({'x': {'y': 'z'}}.?x.y)" value: { bool_value: true } } test { name: "type" expr: "type(optional.none()) == optional_type" value: { bool_value: true } } test { name: "optional_chaining_1" expr: "optional.ofNonZeroValue('').or(optional.of({'c': {'dashed-index': 'goodbye'}}.c['dashed-index'])).orValue('default value')" value: { string_value: "goodbye" } } test { name: "optional_chaining_2" expr: "{'c': {'dashed-index': 'goodbye'}}.c[?'dashed-index'].orValue('default value')" value: { string_value: "goodbye" } } test { name: "optional_chaining_3" expr: "{'c': {}}.c[?'missing-index'].orValue('default value')" value: { string_value: "default value" } } test { name: "optional_chaining_4" expr: "optional.of({'c': {'index': 'goodbye'}}).c.index.orValue('default value')" value: { string_value: "goodbye" } } test { name: "optional_chaining_5" expr: "optional.of({'c': {}}).c.missing.or(optional.none()[0]).orValue('default value')" value: { string_value: "default value" } } test { name: "optional_chaining_6" expr: "optional.of({'c': {}}).c.missing.or(optional.of(['list-value'])[0]).orValue('default value')" value: { string_value: "list-value" } } test { name: "optional_chaining_7" expr: "optional.of({'c': {'index': 'goodbye'}}).c['index'].orValue('default value')" value: { string_value: "goodbye" } } test { name: "optional_chaining_8" expr: "optional.of({'c': {}}).c['missing'].orValue('default value')" value: { string_value: "default value" } } test { name: "optional_chaining_9" expr: "has(optional.of({'c': {'entry': 'hello world'}}).c) && !has(optional.of({'c': {'entry': 'hello world'}}).c.missing)" value: { bool_value: true } } test { name: "optional_chaining_10" expr: "optional.ofNonZeroValue({'c': {'dashed-index': 'goodbye'}}.a.z).orValue({'c': {'dashed-index': 'goodbye'}}.c['dashed-index'])" eval_error: { errors: { message: "no such key" } } } test { name: "optional_chaining_11" expr: "{'c': {'dashed-index': 'goodbye'}}.?c.missing.or({'c': {'dashed-index': 'goodbye'}}.?c['dashed-index']).orValue('').size()" value: { int64_value: 7 } } test { name: "optional_chaining_12" expr: "{?'nested_map': optional.ofNonZeroValue({?'map': {'c': {'dashed-index': 'goodbye'}}.?c})}" value: { map_value: { entries { key: { string_value: "nested_map" } value: { map_value: { entries { key: { string_value: "map" } value: { map_value: { entries { key: { string_value: "dashed-index" } value: { string_value: "goodbye" } } } } } } } } } } } test { name: "optional_chaining_13" expr: "{?'nested_map': optional.ofNonZeroValue({?'map': {}.?c}), 'singleton': true}" value: { map_value: { entries { key: { string_value: "singleton" } value: { bool_value: true } } } } } test { name: "optional_chaining_14" expr: "[?{}.?c, ?optional.of(42), ?optional.none()]" value: { list_value: { values: { int64_value: 42 } } } } test { name: "optional_chaining_15" expr: "[?optional.ofNonZeroValue({'c': []}.?c.orValue(dyn({})))]" value: { list_value: {} } } test { name: "optional_chaining_16" expr: "optional.ofNonZeroValue({?'nested_map': optional.ofNonZeroValue({?'map': optional.of({}).?c})}).hasValue()" value: { bool_value: false } } test { name: "has_optional_ofNonZeroValue_struct_optional_ofNonZeroValue_map_optindex_field" container: "cel.expr.conformance.proto2" expr: "has(TestAllTypes{?single_double_wrapper: optional.ofNonZeroValue(0.0)}.single_double_wrapper)" value: { bool_value: false } } test { name: "optional_ofNonZeroValue_struct_optional_ofNonZeroValue_map_optindex_field" container: "cel.expr.conformance.proto2" expr: "optional.ofNonZeroValue(TestAllTypes{?single_double_wrapper: optional.ofNonZeroValue(0.0)}).hasValue()" value: { bool_value: false } } test { name: "struct_map_optindex_field" container: "cel.expr.conformance.proto2" expr: "TestAllTypes{?map_string_string: {'nested': {}}[?'nested']}.map_string_string" value: { map_value: {} } } test { name: "struct_optional_ofNonZeroValue_map_optindex_field" container: "cel.expr.conformance.proto2" expr: "TestAllTypes{?map_string_string: optional.ofNonZeroValue({'nested': {}}[?'nested'].orValue({}))}.map_string_string" value: { map_value: {} } } test { name: "struct_map_optindex_field_nested" container: "cel.expr.conformance.proto2" expr: "TestAllTypes{?map_string_string: {'nested': {'hello': 'world'}}[?'nested']}.map_string_string" value: { map_value: { entries { key: { string_value: "hello" } value: { string_value: "world" } } } } } test { name: "struct_list_optindex_field" container: "cel.expr.conformance.proto2" expr: "TestAllTypes{repeated_string: ['greetings', ?{'nested': {'hello': 'world'}}.nested.?hello]}.repeated_string" value: { list_value: { values { string_value: "greetings" } values { string_value: "world" } } } } test { name: "optional_empty_map_optindex_hasValue" expr: "optional.of({}).?c.hasValue()" value: { bool_value: false } } test { name: "empty_struct_optindex_hasValue" container: "cel.expr.conformance.proto2" expr: "TestAllTypes{}.?repeated_string.hasValue()" value: { bool_value: false } } test { name: "optional_empty_struct_optindex_hasValue" container: "cel.expr.conformance.proto2" expr: "optional.of(TestAllTypes{}).?repeated_string.hasValue()" value: { bool_value: false } } test { name: "optional_none_optselect_hasValue" expr: "optional.none().?repeated_string.hasValue()" value: { bool_value: false } } test { name: "struct_optindex_value" container: "cel.expr.conformance.proto2" expr: "TestAllTypes{repeated_string: ['foo']}.?repeated_string.value()" value: { list_value: { values { string_value: "foo" } } } } test { name: "optional_struct_optindex_value" container: "cel.expr.conformance.proto2" expr: "optional.of(TestAllTypes{repeated_string: ['foo']}).?repeated_string.value()" value: { list_value: { values { string_value: "foo" } } } } test { name: "optional_struct_optindex_index_value" container: "cel.expr.conformance.proto2" expr: "optional.of(TestAllTypes{repeated_string: ['foo']}).?repeated_string[0].value()" value: { string_value: "foo" } } test { name: "empty_list_optindex_hasValue" expr: "[][?0].hasValue()" value: { bool_value: false } } test { name: "optional_empty_list_optindex_hasValue" expr: "optional.of([])[?0].hasValue()" value: { bool_value: false } } test { name: "optional_none_optindex_hasValue" expr: "optional.none()[?0].hasValue()" value: { bool_value: false } } test { name: "list_optindex_value" expr: "['foo'][?0].value()" value: { string_value: "foo" } } test { name: "optional_list_optindex_value" expr: "optional.of(['foo'])[?0].value()" value: { string_value: "foo" } } test { name: "map_key_mixed_type_optindex_value" expr: "{true: 1, 2: 2, 5u: 3}[?true].value()" value: { int64_value: 1 } } test { name: "map_key_mixed_numbers_double_key_optindex_value" expr: "{1u: 1.0, 2: 2.0, 3u: 3.0}[?3.0].value()" value: { double_value: 3.0 } } test { name: "map_key_mixed_numbers_uint_key_optindex_value" expr: "{1u: 1.0, 2: 2.0, 3u: 3.0}[?2u].value()" value: { double_value: 2.0 } } test { name: "map_key_mixed_numbers_int_key_optindex_value" expr: "{1u: 1.0, 2: 2.0, 3u: 3.0}[?1].value()" value: { double_value: 1.0 } } test { name: "optional_eq_none_none" expr: "optional.none() == optional.none()" value: { bool_value: true } } test { name: "optional_eq_none_int" expr: "optional.none() == optional.of(1)" value: { bool_value: false } } test { name: "optional_eq_int_none" expr: "optional.of(1) == optional.none()" value: { bool_value: false } } test { name: "optional_eq_int_int" expr: "optional.of(1) == optional.of(1)" value: { bool_value: true } } test { name: "optional_ne_none_none" expr: "optional.none() != optional.none()" value: { bool_value: false } } test { name: "optional_ne_none_int" expr: "optional.none() != optional.of(1)" value: { bool_value: true } } test { name: "optional_ne_int_none" expr: "optional.of(1) != optional.none()" value: { bool_value: true } } test { name: "optional_ne_int_int" expr: "optional.of(1) != optional.of(1)" value: { bool_value: false } } test { name: "map_optional_has" expr: "has({'foo': optional.none()}.foo)" value: { bool_value: true } } test { name: "map_optional_select_has" expr: "has({'foo': optional.none()}.foo.bar)" value: { bool_value: false } } test { name: "map_optional_entry_has" expr: "has({?'foo': optional.none()}.foo)" value: { bool_value: false } } }