|
| 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 dev.cel.common.annotations.Internal; |
| 19 | +import dev.cel.common.exceptions.CelAttributeNotFoundException; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +/** |
| 24 | + * Walks a sequence of {@link SelectField} selections, dispatching each field over {@link |
| 25 | + * OptimizedSelectable}, {@link SelectableValue}, or {@link Map}. |
| 26 | + * |
| 27 | + * <p>CEL Library Internals. Do Not Use. |
| 28 | + */ |
| 29 | +@Internal |
| 30 | +public final class OptimizedSelectTraversal { |
| 31 | + |
| 32 | + /** |
| 33 | + * Qualifies {@code target} through every field in {@code fields} and returns the terminal value. |
| 34 | + * |
| 35 | + * @param celValueConverter Converter for unadapted entries encountered in a root {@link Map}. |
| 36 | + */ |
| 37 | + public static Object qualify( |
| 38 | + Object target, ImmutableList<SelectField> fields, CelValueConverter celValueConverter) { |
| 39 | + Object current = target; |
| 40 | + for (int i = 0; i < fields.size(); i++) { |
| 41 | + current = qualifyField(current, fields.get(i), celValueConverter); |
| 42 | + } |
| 43 | + return current; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Presence tests the terminal field of {@code fields}, navigating through all preceding fields. |
| 48 | + * |
| 49 | + * <p>Absence of any intermediate field short-circuits to {@code false}. |
| 50 | + */ |
| 51 | + public static boolean hasField( |
| 52 | + Object target, ImmutableList<SelectField> fields, CelValueConverter celValueConverter) { |
| 53 | + if (fields.isEmpty()) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + Object current = target; |
| 57 | + int terminalIndex = fields.size() - 1; |
| 58 | + for (int i = 0; i < terminalIndex; i++) { |
| 59 | + Optional<Object> next = navigateField(current, fields.get(i), celValueConverter); |
| 60 | + if (!next.isPresent()) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + current = next.get(); |
| 64 | + } |
| 65 | + return hasTerminalField(current, fields.get(terminalIndex)); |
| 66 | + } |
| 67 | + |
| 68 | + // SelectableValue is only ever instantiated with String keys in the select path. |
| 69 | + @SuppressWarnings("unchecked") |
| 70 | + private static Object qualifyField( |
| 71 | + Object target, SelectField field, CelValueConverter celValueConverter) { |
| 72 | + if (target instanceof ErrorValue) { |
| 73 | + return target; |
| 74 | + } |
| 75 | + if (target instanceof OptimizedSelectable) { |
| 76 | + return ((OptimizedSelectable) target).selectByFieldNumber(field); |
| 77 | + } |
| 78 | + if (target instanceof SelectableValue) { |
| 79 | + SelectableValue<String> selectable = (SelectableValue<String>) target; |
| 80 | + if (field.defaultValue() != null) { |
| 81 | + return selectable |
| 82 | + .find(field.fieldName()) |
| 83 | + .map(Object.class::cast) |
| 84 | + .orElse(field.defaultValue()); |
| 85 | + } |
| 86 | + return selectable.select(field.fieldName()); |
| 87 | + } |
| 88 | + if (target instanceof Map) { |
| 89 | + return getMapEntry((Map<?, ?>) target, field.fieldName(), celValueConverter); |
| 90 | + } |
| 91 | + throw CelAttributeNotFoundException.forFieldResolution(field.fieldName()); |
| 92 | + } |
| 93 | + |
| 94 | + // SelectableValue is only ever instantiated with String keys in the select path. |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + private static Optional<Object> navigateField( |
| 97 | + Object target, SelectField field, CelValueConverter celValueConverter) { |
| 98 | + if (target instanceof ErrorValue) { |
| 99 | + return Optional.of(target); |
| 100 | + } |
| 101 | + if (target instanceof OptimizedSelectable) { |
| 102 | + return ((OptimizedSelectable) target).findByFieldNumber(field); |
| 103 | + } |
| 104 | + if (target instanceof SelectableValue) { |
| 105 | + return ((SelectableValue<String>) target).find(field.fieldName()).map(Object.class::cast); |
| 106 | + } |
| 107 | + if (target instanceof Map) { |
| 108 | + return findMapEntry((Map<?, ?>) target, field.fieldName(), celValueConverter); |
| 109 | + } |
| 110 | + throw CelAttributeNotFoundException.forFieldResolution(field.fieldName()); |
| 111 | + } |
| 112 | + |
| 113 | + // SelectableValue is only ever instantiated with String keys in the select path. |
| 114 | + @SuppressWarnings("unchecked") |
| 115 | + private static boolean hasTerminalField(Object target, SelectField field) { |
| 116 | + if (target instanceof ErrorValue) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + if (target instanceof OptimizedSelectable) { |
| 120 | + return ((OptimizedSelectable) target).hasFieldByNumber(field); |
| 121 | + } |
| 122 | + if (target instanceof SelectableValue) { |
| 123 | + return ((SelectableValue<String>) target).find(field.fieldName()).isPresent(); |
| 124 | + } |
| 125 | + if (target instanceof Map) { |
| 126 | + return ((Map<?, ?>) target).containsKey(field.fieldName()); |
| 127 | + } |
| 128 | + throw CelAttributeNotFoundException.forFieldResolution(field.fieldName()); |
| 129 | + } |
| 130 | + |
| 131 | + private static Object getMapEntry( |
| 132 | + Map<?, ?> map, String key, CelValueConverter celValueConverter) { |
| 133 | + return findMapEntry(map, key, celValueConverter) |
| 134 | + .orElseThrow(() -> CelAttributeNotFoundException.forMissingMapKey(key)); |
| 135 | + } |
| 136 | + |
| 137 | + private static Optional<Object> findMapEntry( |
| 138 | + Map<?, ?> map, String key, CelValueConverter celValueConverter) { |
| 139 | + Object mapValue = map.get(key); |
| 140 | + if (mapValue != null) { |
| 141 | + return Optional.of(toStepTarget(mapValue, celValueConverter)); |
| 142 | + } |
| 143 | + if (!map.containsKey(key)) { |
| 144 | + return Optional.empty(); |
| 145 | + } |
| 146 | + throw CelAttributeNotFoundException.of( |
| 147 | + String.format("Map value cannot be null for key: %s", key)); |
| 148 | + } |
| 149 | + |
| 150 | + static Object toStepTarget(Object value, CelValueConverter celValueConverter) { |
| 151 | + if (value instanceof Map) { |
| 152 | + return value; |
| 153 | + } |
| 154 | + return celValueConverter.toRuntimeValue(value); |
| 155 | + } |
| 156 | + |
| 157 | + private OptimizedSelectTraversal() {} |
| 158 | +} |
0 commit comments