|
| 1 | +// Copyright 2025 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.types; |
| 16 | + |
| 17 | +import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableCollection; |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import com.google.common.collect.ImmutableSet; |
| 22 | +import dev.cel.protobuf.CelLiteDescriptor; |
| 23 | +import dev.cel.protobuf.CelLiteDescriptor.FieldLiteDescriptor; |
| 24 | +import dev.cel.protobuf.CelLiteDescriptor.MessageLiteDescriptor; |
| 25 | +import java.util.Map.Entry; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.function.Function; |
| 29 | + |
| 30 | +/** TODO: Add */ |
| 31 | +public final class ProtoMessageLiteTypeProvider implements CelTypeProvider { |
| 32 | + private static final ImmutableMap<FieldLiteDescriptor.Type, CelType> PROTO_TYPE_TO_CEL_TYPE = |
| 33 | + ImmutableMap.<FieldLiteDescriptor.Type, CelType>builder() |
| 34 | + .put(FieldLiteDescriptor.Type.DOUBLE, SimpleType.DOUBLE) |
| 35 | + .put(FieldLiteDescriptor.Type.FLOAT, SimpleType.DOUBLE) |
| 36 | + .put(FieldLiteDescriptor.Type.INT64, SimpleType.INT) |
| 37 | + .put(FieldLiteDescriptor.Type.INT32, SimpleType.INT) |
| 38 | + .put(FieldLiteDescriptor.Type.SFIXED32, SimpleType.INT) |
| 39 | + .put(FieldLiteDescriptor.Type.SFIXED64, SimpleType.INT) |
| 40 | + .put(FieldLiteDescriptor.Type.SINT32, SimpleType.INT) |
| 41 | + .put(FieldLiteDescriptor.Type.SINT64, SimpleType.INT) |
| 42 | + .put(FieldLiteDescriptor.Type.BOOL, SimpleType.BOOL) |
| 43 | + .put(FieldLiteDescriptor.Type.STRING, SimpleType.STRING) |
| 44 | + .put(FieldLiteDescriptor.Type.BYTES, SimpleType.BYTES) |
| 45 | + .put(FieldLiteDescriptor.Type.FIXED32, SimpleType.UINT) |
| 46 | + .put(FieldLiteDescriptor.Type.FIXED64, SimpleType.UINT) |
| 47 | + .put(FieldLiteDescriptor.Type.UINT32, SimpleType.UINT) |
| 48 | + .put(FieldLiteDescriptor.Type.UINT64, SimpleType.UINT) |
| 49 | + .buildOrThrow(); |
| 50 | + |
| 51 | + private final ImmutableMap<String, CelType> allTypes; |
| 52 | + |
| 53 | + @Override |
| 54 | + public ImmutableCollection<CelType> types() { |
| 55 | + return allTypes.values(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Optional<CelType> findType(String typeName) { |
| 60 | + return Optional.empty(); |
| 61 | + } |
| 62 | + |
| 63 | + public static ProtoMessageLiteTypeProvider newInstance(CelLiteDescriptor... celLiteDescriptors) { |
| 64 | + return newInstance(ImmutableSet.copyOf(celLiteDescriptors)); |
| 65 | + } |
| 66 | + |
| 67 | + public static ProtoMessageLiteTypeProvider newInstance( |
| 68 | + Set<CelLiteDescriptor> celLiteDescriptors) { |
| 69 | + return new ProtoMessageLiteTypeProvider(celLiteDescriptors); |
| 70 | + } |
| 71 | + |
| 72 | + private ProtoMessageLiteTypeProvider(Set<CelLiteDescriptor> celLiteDescriptors) { |
| 73 | + ImmutableMap.Builder<String, CelType> builder = ImmutableMap.builder(); |
| 74 | + for (CelLiteDescriptor descriptor : celLiteDescriptors) { |
| 75 | + for (Entry<String, MessageLiteDescriptor> entry : |
| 76 | + descriptor.getProtoTypeNamesToDescriptors().entrySet()) { |
| 77 | + builder.put(entry.getKey(), createMessageType(entry.getValue())); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + this.allTypes = builder.buildOrThrow(); |
| 82 | + } |
| 83 | + |
| 84 | + private static ProtoMessageType createMessageType(MessageLiteDescriptor messageLiteDescriptor) { |
| 85 | + ImmutableMap<String, FieldLiteDescriptor> fields = |
| 86 | + messageLiteDescriptor.getFieldDescriptors().stream() |
| 87 | + .collect(toImmutableMap(FieldLiteDescriptor::getFieldName, Function.identity())); |
| 88 | + |
| 89 | + return new ProtoMessageType( |
| 90 | + messageLiteDescriptor.getProtoTypeName(), |
| 91 | + fields.keySet(), |
| 92 | + new FieldResolver(fields), |
| 93 | + extensionFieldName -> { |
| 94 | + throw new UnsupportedOperationException( |
| 95 | + "Proto extensions are not yet supported in MessageLite."); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + private static class FieldResolver implements StructType.FieldResolver { |
| 100 | + private final ImmutableMap<String, FieldLiteDescriptor> fields; |
| 101 | + |
| 102 | + @Override |
| 103 | + public Optional<CelType> findField(String fieldName) { |
| 104 | + FieldLiteDescriptor fieldDescriptor = fields.get(fieldName); |
| 105 | + if (fieldDescriptor == null) { |
| 106 | + return Optional.empty(); |
| 107 | + } |
| 108 | + |
| 109 | + FieldLiteDescriptor.Type fieldType = fieldDescriptor.getProtoFieldType(); |
| 110 | + switch (fieldDescriptor.getProtoFieldType()) { |
| 111 | + default: |
| 112 | + return Optional.of(PROTO_TYPE_TO_CEL_TYPE.get(fieldType)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private FieldResolver(ImmutableMap<String, FieldLiteDescriptor> fields) { |
| 117 | + this.fields = fields; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments