|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.common.values; |
| 16 | + |
| 17 | +import com.google.common.collect.ImmutableList; |
| 18 | +import com.google.errorprone.annotations.Immutable; |
| 19 | +import dev.cel.common.annotations.Internal; |
| 20 | +import dev.cel.common.exceptions.CelAttributeNotFoundException; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Optional; |
| 23 | +import org.jspecify.annotations.Nullable; |
| 24 | + |
| 25 | +/** |
| 26 | + * Represents a value capable of evaluating optimized field selection and presence testing for field |
| 27 | + * selection optimization. |
| 28 | + * |
| 29 | + * <p>CEL Library Internals. Do Not Use. |
| 30 | + */ |
| 31 | +@Internal |
| 32 | +@Immutable |
| 33 | +public interface OptimizedSelectable { |
| 34 | + |
| 35 | + /** |
| 36 | + * Evaluates a multi-hop qualification chain across the provided fields. |
| 37 | + * |
| 38 | + * <p>Implementations may override this method to provide an allocation-free traversal over |
| 39 | + * underlying schema or message descriptors. The default implementation iterates sequentially over |
| 40 | + * {@link #selectField} for intermediate submessages and the leaf. |
| 41 | + */ |
| 42 | + default Object qualify(ImmutableList<SelectField> fields) { |
| 43 | + Object current = this; |
| 44 | + for (SelectField field : fields) { |
| 45 | + if (current instanceof OptimizedSelectable) { |
| 46 | + current = ((OptimizedSelectable) current).selectField(field); |
| 47 | + } else if (current instanceof SelectableValue) { |
| 48 | + Optional<Object> found = |
| 49 | + SelectField.findField((SelectableValue<?>) current, field.fieldName()); |
| 50 | + if (found.isPresent()) { |
| 51 | + current = found.get(); |
| 52 | + } else if (field.defaultValue() != null) { |
| 53 | + current = field.defaultValue(); |
| 54 | + } else { |
| 55 | + throw CelAttributeNotFoundException.forFieldResolution(field.fieldName()); |
| 56 | + } |
| 57 | + } else if (current instanceof Map) { |
| 58 | + Map<?, ?> map = (Map<?, ?>) current; |
| 59 | + Object mapValue = map.get(field.fieldName()); |
| 60 | + if (mapValue != null) { |
| 61 | + current = mapValue; |
| 62 | + } else if (map.containsKey(field.fieldName())) { |
| 63 | + current = NullValue.NULL_VALUE; |
| 64 | + } else { |
| 65 | + throw CelAttributeNotFoundException.forMissingMapKey(field.fieldName()); |
| 66 | + } |
| 67 | + } else { |
| 68 | + throw CelAttributeNotFoundException.forFieldResolution(field.fieldName()); |
| 69 | + } |
| 70 | + } |
| 71 | + return current; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Performs field selection for a single hop in an optimized selection chain. |
| 76 | + * |
| 77 | + * @param field The field hop descriptor containing field number, name, type code, and default |
| 78 | + * value. |
| 79 | + * @return The selected field value, or default value / empty submessage if absent. |
| 80 | + */ |
| 81 | + Object selectField(SelectField field); |
| 82 | + |
| 83 | + /** |
| 84 | + * Evaluates presence testing across the provided fields. |
| 85 | + * |
| 86 | + * <p>Implementations may override this method to provide an allocation-free traversal over |
| 87 | + * underlying schema or message descriptors. The default implementation iterates sequentially over |
| 88 | + * {@link #navigateField} for intermediate submessages and {@link #hasField} for the leaf. |
| 89 | + */ |
| 90 | + default boolean hasField(ImmutableList<SelectField> fields) { |
| 91 | + if (fields.isEmpty()) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + int lastIndex = fields.size() - 1; |
| 95 | + Object current = this; |
| 96 | + for (int i = 0; i < lastIndex; i++) { |
| 97 | + SelectField field = fields.get(i); |
| 98 | + if (current instanceof OptimizedSelectable) { |
| 99 | + current = ((OptimizedSelectable) current).navigateField(field); |
| 100 | + if (current == null) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + } else if (current instanceof SelectableValue) { |
| 104 | + Optional<Object> found = |
| 105 | + SelectField.findField((SelectableValue<?>) current, field.fieldName()); |
| 106 | + if (!found.isPresent()) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + current = found.get(); |
| 110 | + } else if (current instanceof Map) { |
| 111 | + Map<?, ?> map = (Map<?, ?>) current; |
| 112 | + if (!map.containsKey(field.fieldName())) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + Object mapValue = map.get(field.fieldName()); |
| 116 | + current = (mapValue != null) ? mapValue : NullValue.NULL_VALUE; |
| 117 | + } else { |
| 118 | + throw CelAttributeNotFoundException.forFieldResolution(field.fieldName()); |
| 119 | + } |
| 120 | + } |
| 121 | + SelectField terminalField = fields.get(lastIndex); |
| 122 | + if (current instanceof OptimizedSelectable) { |
| 123 | + return ((OptimizedSelectable) current).hasField(terminalField); |
| 124 | + } else if (current instanceof SelectableValue) { |
| 125 | + return SelectField.findField((SelectableValue<?>) current, terminalField.fieldName()) |
| 126 | + .isPresent(); |
| 127 | + } else if (current instanceof Map) { |
| 128 | + return ((Map<?, ?>) current).containsKey(terminalField.fieldName()); |
| 129 | + } else { |
| 130 | + throw CelAttributeNotFoundException.forFieldResolution(terminalField.fieldName()); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Evaluates presence testing for a single hop in an optimized selection chain. |
| 136 | + * |
| 137 | + * @param field The field hop descriptor. |
| 138 | + * @return True if the field is present, false otherwise. |
| 139 | + */ |
| 140 | + boolean hasField(SelectField field); |
| 141 | + |
| 142 | + /** |
| 143 | + * Navigates into an intermediate submessage for presence testing. |
| 144 | + * |
| 145 | + * <p>Returns {@code null} instead of an {@link Optional} to avoid object allocation overhead on |
| 146 | + * hot-path intermediate navigation hops. |
| 147 | + * |
| 148 | + * @param field The field hop descriptor. |
| 149 | + * @return The submessage if present, or {@code null} if absent. |
| 150 | + */ |
| 151 | + @Nullable Object navigateField(SelectField field); |
| 152 | +} |