|
| 1 | +// Copyright 2024 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.runtime; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import com.google.common.primitives.UnsignedLong; |
| 22 | +import com.google.errorprone.annotations.Immutable; |
| 23 | +import com.google.protobuf.ByteString; |
| 24 | +import com.google.protobuf.Duration; |
| 25 | +import com.google.protobuf.MessageOrBuilder; |
| 26 | +import com.google.protobuf.NullValue; |
| 27 | +import com.google.protobuf.Timestamp; |
| 28 | +import dev.cel.common.types.CelType; |
| 29 | +import dev.cel.common.types.ListType; |
| 30 | +import dev.cel.common.types.MapType; |
| 31 | +import dev.cel.common.types.OptionalType; |
| 32 | +import dev.cel.common.types.SimpleType; |
| 33 | +import dev.cel.common.types.StructType; |
| 34 | +import dev.cel.common.types.StructTypeReference; |
| 35 | +import dev.cel.common.types.TypeType; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Collection; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Optional; |
| 41 | + |
| 42 | +/** {@code CelTypeResolver} resolves {@link CelType} into a runtime type value. */ |
| 43 | +@Immutable |
| 44 | +final class CelTypeResolver { |
| 45 | + |
| 46 | + // Sentinel runtime value representing the special "type" ident. This ensures following to be |
| 47 | + // true: type == type(string) && type == type(type("foo")) |
| 48 | + private static final TypeType RUNTIME_TYPE_TYPE = TypeType.create(SimpleType.DYN); |
| 49 | + |
| 50 | + private static final ImmutableMap<Class<?>, TypeType> COMMON_TYPES = |
| 51 | + ImmutableMap.<Class<?>, TypeType>builder() |
| 52 | + .put(Boolean.class, TypeType.create(SimpleType.BOOL)) |
| 53 | + .put(Double.class, TypeType.create(SimpleType.DOUBLE)) |
| 54 | + .put(Long.class, TypeType.create(SimpleType.INT)) |
| 55 | + .put(UnsignedLong.class, TypeType.create(SimpleType.UINT)) |
| 56 | + .put(String.class, TypeType.create(SimpleType.STRING)) |
| 57 | + .put(NullValue.class, TypeType.create(SimpleType.NULL_TYPE)) |
| 58 | + .put(Duration.class, TypeType.create(SimpleType.DURATION)) |
| 59 | + .put(Timestamp.class, TypeType.create(SimpleType.TIMESTAMP)) |
| 60 | + .put(ArrayList.class, TypeType.create(ListType.create(SimpleType.DYN))) |
| 61 | + .put(ImmutableList.class, TypeType.create(ListType.create(SimpleType.DYN))) |
| 62 | + .put(HashMap.class, TypeType.create(MapType.create(SimpleType.DYN, SimpleType.DYN))) |
| 63 | + .put(ImmutableMap.class, TypeType.create(MapType.create(SimpleType.DYN, SimpleType.DYN))) |
| 64 | + .put(Optional.class, TypeType.create(OptionalType.create(SimpleType.DYN))) |
| 65 | + .buildOrThrow(); |
| 66 | + |
| 67 | + private static final ImmutableMap<Class<?>, TypeType> EXTENDABLE_TYPES = |
| 68 | + ImmutableMap.<Class<?>, TypeType>builder() |
| 69 | + .put(Collection.class, TypeType.create(ListType.create(SimpleType.DYN))) |
| 70 | + .put(ByteString.class, TypeType.create(SimpleType.BYTES)) |
| 71 | + .put(Map.class, TypeType.create(MapType.create(SimpleType.DYN, SimpleType.DYN))) |
| 72 | + .buildOrThrow(); |
| 73 | + |
| 74 | + /** Adapt the type-checked {@link CelType} into a runtime type value {@link TypeType}. */ |
| 75 | + static TypeType adaptType(CelType type) { |
| 76 | + checkNotNull(type); |
| 77 | + |
| 78 | + switch (type.kind()) { |
| 79 | + case TYPE: |
| 80 | + CelType typeOfType = ((TypeType) type).type(); |
| 81 | + switch (typeOfType.kind()) { |
| 82 | + case STRUCT: |
| 83 | + return TypeType.create(adaptStructType((StructType) typeOfType)); |
| 84 | + case TYPE: |
| 85 | + // Unwrap type(type(foo)) |
| 86 | + return adaptType(typeOfType); |
| 87 | + default: |
| 88 | + return (TypeType) type; |
| 89 | + } |
| 90 | + case UNSPECIFIED: |
| 91 | + throw new IllegalArgumentException("Unsupported CelType kind: " + type.kind()); |
| 92 | + default: |
| 93 | + return TypeType.create(type); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Resolve the CEL type of the {@code obj}, using the {@code checkedTypeValue} as hint for type |
| 99 | + * disambiguation. |
| 100 | + */ |
| 101 | + static TypeType resolveObjectType(Object obj, CelType checkedTypeValue) { |
| 102 | + checkNotNull(obj); |
| 103 | + checkNotNull(checkedTypeValue); |
| 104 | + if (obj instanceof TypeType) { |
| 105 | + return RUNTIME_TYPE_TYPE; |
| 106 | + } |
| 107 | + |
| 108 | + Class<?> currentClass = obj.getClass(); |
| 109 | + TypeType runtimeType = COMMON_TYPES.get(currentClass); |
| 110 | + if (runtimeType != null) { |
| 111 | + return runtimeType; |
| 112 | + } |
| 113 | + |
| 114 | + if (obj instanceof MessageOrBuilder) { |
| 115 | + MessageOrBuilder msg = (MessageOrBuilder) obj; |
| 116 | + // TODO: Replace with CelLiteDescriptor |
| 117 | + return TypeType.create(StructTypeReference.create(msg.getDescriptorForType().getFullName())); |
| 118 | + } |
| 119 | + |
| 120 | + while (currentClass != null) { |
| 121 | + runtimeType = EXTENDABLE_TYPES.get(currentClass); |
| 122 | + if (runtimeType != null) { |
| 123 | + return runtimeType; |
| 124 | + } |
| 125 | + |
| 126 | + // Check interfaces |
| 127 | + for (Class<?> interfaceClass : currentClass.getInterfaces()) { |
| 128 | + runtimeType = EXTENDABLE_TYPES.get(interfaceClass); |
| 129 | + if (runtimeType != null) { |
| 130 | + return runtimeType; |
| 131 | + } |
| 132 | + } |
| 133 | + currentClass = currentClass.getSuperclass(); |
| 134 | + } |
| 135 | + |
| 136 | + return TypeType.create(checkedTypeValue); |
| 137 | + } |
| 138 | + |
| 139 | + private static CelType adaptStructType(StructType typeOfType) { |
| 140 | + String structName = typeOfType.name(); |
| 141 | + CelType newTypeOfType; |
| 142 | + if (structName.equals(SimpleType.DURATION.name())) { |
| 143 | + newTypeOfType = SimpleType.DURATION; |
| 144 | + } else if (structName.equals(SimpleType.TIMESTAMP.name())) { |
| 145 | + newTypeOfType = SimpleType.TIMESTAMP; |
| 146 | + } else { |
| 147 | + // Coerces ProtoMessageTypeProvider to be a struct type reference for accurate |
| 148 | + // equality tests. |
| 149 | + // In the future, we can plumb ProtoMessageTypeProvider through the runtime to retain |
| 150 | + // ProtoMessageType here. |
| 151 | + newTypeOfType = StructTypeReference.create(typeOfType.name()); |
| 152 | + } |
| 153 | + return newTypeOfType; |
| 154 | + } |
| 155 | + |
| 156 | + private CelTypeResolver() {} |
| 157 | +} |
0 commit comments