Skip to content
Open
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: 10 additions & 0 deletions common/src/main/java/dev/cel/common/values/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ CEL_VALUES_SOURCES = [
"ErrorValue.java",
"NullValue.java",
"OpaqueValue.java",
"OptimizedSelectable.java",
"OptionalValue.java",
"SelectField.java",
"SelectableValue.java",
"StructValue.java",
]
Expand Down Expand Up @@ -167,6 +169,7 @@ java_library(
":preadapted_list",
"//:auto_value",
"//common/annotations",
"//common/exceptions:attribute_not_found",
"//common/types",
"//common/types:type_providers",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -218,6 +221,7 @@ cel_android_library(
":preadapted_list_android",
"//:auto_value",
"//common/annotations",
"//common/exceptions:attribute_not_found",
"//common/types:type_providers_android",
"//common/types:types_android",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -317,6 +321,7 @@ java_library(
srcs = [
"ProtoLiteCelValueConverter.java",
"ProtoMessageLiteValue.java",
"RawProtoMessageLiteValue.java",
],
tags = [
],
Expand All @@ -325,6 +330,7 @@ java_library(
":values",
"//:auto_value",
"//common/annotations",
"//common/exceptions:attribute_not_found",
"//common/internal:cel_lite_descriptor_pool",
"//common/internal:well_known_proto",
"//common/types",
Expand All @@ -333,6 +339,7 @@ java_library(
"//protobuf:cel_lite_descriptor",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
"@maven_android//:com_google_protobuf_protobuf_javalite",
],
)
Expand All @@ -342,6 +349,7 @@ cel_android_library(
srcs = [
"ProtoLiteCelValueConverter.java",
"ProtoMessageLiteValue.java",
"RawProtoMessageLiteValue.java",
],
tags = [
],
Expand All @@ -350,6 +358,7 @@ cel_android_library(
":values_android",
"//:auto_value",
"//common/annotations",
"//common/exceptions:attribute_not_found",
"//common/internal:cel_lite_descriptor_pool_android",
"//common/internal:well_known_proto_android",
"//common/types:type_providers_android",
Expand All @@ -358,6 +367,7 @@ cel_android_library(
"//protobuf:cel_lite_descriptor",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
"@maven_android//:com_google_guava_guava",
"@maven_android//:com_google_protobuf_protobuf_javalite",
],
Expand Down
152 changes: 152 additions & 0 deletions common/src/main/java/dev/cel/common/values/OptimizedSelectable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright 2026 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.

package dev.cel.common.values;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.annotations.Internal;
import dev.cel.common.exceptions.CelAttributeNotFoundException;
import java.util.Map;
import java.util.Optional;
import org.jspecify.annotations.Nullable;

/**
* Represents a value capable of evaluating optimized field selection and presence testing for field
* selection optimization.
*
* <p>CEL Library Internals. Do Not Use.
*/
@Internal
@Immutable
public interface OptimizedSelectable {

/**
* Evaluates a multi-hop qualification chain across the provided fields.
*
* <p>Implementations may override this method to provide an allocation-free traversal over
* underlying schema or message descriptors. The default implementation iterates sequentially over
* {@link #selectField} for intermediate submessages and the leaf.
*/
default Object qualify(ImmutableList<SelectField> fields) {
Object current = this;
for (SelectField field : fields) {
if (current instanceof OptimizedSelectable) {
current = ((OptimizedSelectable) current).selectField(field);
} else if (current instanceof SelectableValue) {
Optional<Object> found =
SelectField.findField((SelectableValue<?>) current, field.fieldName());
if (found.isPresent()) {
current = found.get();
} else if (field.defaultValue() != null) {
current = field.defaultValue();
} else {
throw CelAttributeNotFoundException.forFieldResolution(field.fieldName());
}
} else if (current instanceof Map) {
Map<?, ?> map = (Map<?, ?>) current;
Object mapValue = map.get(field.fieldName());
if (mapValue != null) {
current = mapValue;
} else if (map.containsKey(field.fieldName())) {
current = NullValue.NULL_VALUE;
} else {
throw CelAttributeNotFoundException.forMissingMapKey(field.fieldName());
}
} else {
throw CelAttributeNotFoundException.forFieldResolution(field.fieldName());
}
}
return current;
}

/**
* Performs field selection for a single hop in an optimized selection chain.
*
* @param field The field hop descriptor containing field number, name, type code, and default
* value.
* @return The selected field value, or default value / empty submessage if absent.
*/
Object selectField(SelectField field);

/**
* Evaluates presence testing across the provided fields.
*
* <p>Implementations may override this method to provide an allocation-free traversal over
* underlying schema or message descriptors. The default implementation iterates sequentially over
* {@link #navigateField} for intermediate submessages and {@link #hasField} for the leaf.
*/
default boolean hasField(ImmutableList<SelectField> fields) {
if (fields.isEmpty()) {
return false;
}
int lastIndex = fields.size() - 1;
Object current = this;
for (int i = 0; i < lastIndex; i++) {
SelectField field = fields.get(i);
if (current instanceof OptimizedSelectable) {
current = ((OptimizedSelectable) current).navigateField(field);
if (current == null) {
return false;
}
} else if (current instanceof SelectableValue) {
Optional<Object> found =
SelectField.findField((SelectableValue<?>) current, field.fieldName());
if (!found.isPresent()) {
return false;
}
current = found.get();
} else if (current instanceof Map) {
Map<?, ?> map = (Map<?, ?>) current;
if (!map.containsKey(field.fieldName())) {
return false;
}
Object mapValue = map.get(field.fieldName());
current = (mapValue != null) ? mapValue : NullValue.NULL_VALUE;
} else {
throw CelAttributeNotFoundException.forFieldResolution(field.fieldName());
}
}
SelectField terminalField = fields.get(lastIndex);
if (current instanceof OptimizedSelectable) {
return ((OptimizedSelectable) current).hasField(terminalField);
} else if (current instanceof SelectableValue) {
return SelectField.findField((SelectableValue<?>) current, terminalField.fieldName())
.isPresent();
} else if (current instanceof Map) {
return ((Map<?, ?>) current).containsKey(terminalField.fieldName());
} else {
throw CelAttributeNotFoundException.forFieldResolution(terminalField.fieldName());
}
}

/**
* Evaluates presence testing for a single hop in an optimized selection chain.
*
* @param field The field hop descriptor.
* @return True if the field is present, false otherwise.
*/
boolean hasField(SelectField field);

/**
* Navigates into an intermediate submessage for presence testing.
*
* <p>Returns {@code null} instead of an {@link Optional} to avoid object allocation overhead on
* hot-path intermediate navigation hops.
*
* @param field The field hop descriptor.
* @return The submessage if present, or {@code null} if absent.
*/
@Nullable Object navigateField(SelectField field);
}
Loading
Loading