|
| 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 static com.google.common.base.Preconditions.checkArgument; |
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | + |
| 20 | +import com.google.auto.value.AutoValue; |
| 21 | +import com.google.errorprone.annotations.Immutable; |
| 22 | +import dev.cel.common.annotations.Internal; |
| 23 | +import org.jspecify.annotations.Nullable; |
| 24 | + |
| 25 | +/** |
| 26 | + * Represents a single field selection in an optimized selection chain. |
| 27 | + * |
| 28 | + * <p>CEL Library Internals. Do Not Use. |
| 29 | + */ |
| 30 | +@Internal |
| 31 | +@AutoValue |
| 32 | +@AutoValue.CopyAnnotations |
| 33 | +@Immutable |
| 34 | +@SuppressWarnings("Immutable") // Default value is an immutable CEL literal or null |
| 35 | +public abstract class SelectField { |
| 36 | + |
| 37 | + private static final int MAX_FIELD_NUMBER = 536870911; |
| 38 | + |
| 39 | + /** CEL-specific type code used to encode CEL maps on the wire. */ |
| 40 | + public static final int CEL_MAP_TYPE_CODE = -1; |
| 41 | + |
| 42 | + /** Sentinel for a presence-test qualifier, whose 2-tuple carries no type code. */ |
| 43 | + public static final int NO_TYPE_CODE = 0; |
| 44 | + |
| 45 | + /** Protobuf field type code for {@code TYPE_MESSAGE} ({@code FieldDescriptorProto.Type}). */ |
| 46 | + public static final int MESSAGE_TYPE_CODE = 11; |
| 47 | + |
| 48 | + // Mirrors FieldDescriptorProto.Type. Not validated against a protobuf enum because the :values |
| 49 | + // target is deliberately protobuf-free; keep in sync with CelLiteDescriptor.FieldLiteDescriptor. |
| 50 | + private static final int MIN_PROTO_TYPE_CODE = 1; // TYPE_DOUBLE |
| 51 | + private static final int MAX_PROTO_TYPE_CODE = 18; // TYPE_SINT64 |
| 52 | + private static final int GROUP_PROTO_TYPE_CODE = 10; // Unsupported by CEL. |
| 53 | + |
| 54 | + /** Protobuf field number of this hop. */ |
| 55 | + public abstract int fieldNumber(); |
| 56 | + |
| 57 | + /** Protobuf field name or map key of this hop. */ |
| 58 | + public abstract String fieldName(); |
| 59 | + |
| 60 | + /** |
| 61 | + * Protobuf wire type code (1..18, except 10), {@link #CEL_MAP_TYPE_CODE}, or {@link |
| 62 | + * #NO_TYPE_CODE}. |
| 63 | + */ |
| 64 | + public abstract int typeCode(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Default value for this hop, or null if unspecified. When non-null, this must be an immutable |
| 68 | + * CEL literal value. |
| 69 | + */ |
| 70 | + public abstract @Nullable Object defaultValue(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates a presence-test qualifier hop. |
| 74 | + * |
| 75 | + * @param fieldNumber Protobuf field number. Takes {@code long} for compatibility with CEL's int64 |
| 76 | + * constant representations. |
| 77 | + * @param fieldName Protobuf field name. |
| 78 | + */ |
| 79 | + public static SelectField create(long fieldNumber, String fieldName) { |
| 80 | + checkArgument( |
| 81 | + fieldNumber >= 1 && fieldNumber <= MAX_FIELD_NUMBER, |
| 82 | + "Field number out of protobuf range: %s", |
| 83 | + fieldNumber); |
| 84 | + checkNotNull(fieldName); |
| 85 | + return new AutoValue_SelectField( |
| 86 | + (int) fieldNumber, fieldName, NO_TYPE_CODE, /* defaultValue= */ null); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Creates a fully-specified field selection hop with type code and optional default value. |
| 91 | + * |
| 92 | + * @param fieldNumber Protobuf field number. Takes {@code long} for compatibility with CEL's int64 |
| 93 | + * constant representations. |
| 94 | + * @param fieldName Protobuf field name. |
| 95 | + * @param typeCode Protobuf wire type code or {@link #CEL_MAP_TYPE_CODE}. Takes {@code long} for |
| 96 | + * compatibility with CEL's int64 constant representations. |
| 97 | + * @param defaultValue Default value for the field, or null if unspecified. |
| 98 | + */ |
| 99 | + public static SelectField create( |
| 100 | + long fieldNumber, String fieldName, long typeCode, @Nullable Object defaultValue) { |
| 101 | + checkArgument( |
| 102 | + fieldNumber >= 1 && fieldNumber <= MAX_FIELD_NUMBER, |
| 103 | + "Field number out of protobuf range: %s", |
| 104 | + fieldNumber); |
| 105 | + checkNotNull(fieldName); |
| 106 | + checkArgument(isSupportedTypeCode(typeCode), "Invalid protobuf type code: %s", typeCode); |
| 107 | + return new AutoValue_SelectField((int) fieldNumber, fieldName, (int) typeCode, defaultValue); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns whether {@code typeCode} is a protobuf field type code CEL supports, or the {@link |
| 112 | + * #CEL_MAP_TYPE_CODE} sentinel. |
| 113 | + */ |
| 114 | + public static boolean isSupportedTypeCode(long typeCode) { |
| 115 | + if (typeCode == CEL_MAP_TYPE_CODE) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + return typeCode >= MIN_PROTO_TYPE_CODE |
| 119 | + && typeCode <= MAX_PROTO_TYPE_CODE |
| 120 | + && typeCode != GROUP_PROTO_TYPE_CODE; |
| 121 | + } |
| 122 | + |
| 123 | + SelectField() {} |
| 124 | +} |
0 commit comments