|
| 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 com.google.common.collect.ImmutableCollection; |
| 18 | +import com.google.common.collect.ImmutableMap; |
| 19 | +import com.google.common.collect.Maps; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +/** TODO */ |
| 24 | +public class DefaultTypeProvider implements CelTypeProvider { |
| 25 | + |
| 26 | + private static final ImmutableMap<String, CelType> COMMON_TYPES = |
| 27 | + ImmutableMap.<String, CelType>builder() |
| 28 | + .put("bool", TypeType.create(SimpleType.BOOL)) |
| 29 | + .put("bytes", TypeType.create(SimpleType.BYTES)) |
| 30 | + .put("double", TypeType.create(SimpleType.DOUBLE)) |
| 31 | + .put("int", TypeType.create(SimpleType.INT)) |
| 32 | + .put("uint", TypeType.create(SimpleType.UINT)) |
| 33 | + .put("string", TypeType.create(SimpleType.STRING)) |
| 34 | + .put("null_type", TypeType.create(SimpleType.NULL_TYPE)) |
| 35 | + .put("dyn", TypeType.create(SimpleType.DYN)) |
| 36 | + .put("list", TypeType.create(ListType.create(SimpleType.DYN))) |
| 37 | + .put("map", TypeType.create(MapType.create(SimpleType.DYN, SimpleType.DYN))) |
| 38 | + .put("google.protobuf.Duration", TypeType.create(SimpleType.DURATION)) |
| 39 | + .put("google.protobuf.Timestamp", TypeType.create(SimpleType.TIMESTAMP)) |
| 40 | + .put( |
| 41 | + "optional_type", |
| 42 | + TypeType.create( |
| 43 | + OptionalType.create( |
| 44 | + SimpleType.DYN))) // TODO: Move to CelOptionalLibrary |
| 45 | + .buildOrThrow(); |
| 46 | + |
| 47 | + // private static final ImmutableMap<Class<?>, TypeType> EXTENDABLE_TYPES = |
| 48 | + // ImmutableMap.<Class<?>, TypeType>builder() |
| 49 | + // .put(Collection.class, TypeType.create(ListType.create(SimpleType.DYN))) |
| 50 | + // .put(ByteString.class, TypeType.create(SimpleType.BYTES)) |
| 51 | + // .put(Map.class, TypeType.create(MapType.create(SimpleType.DYN, SimpleType.DYN))) |
| 52 | + // .buildOrThrow(); |
| 53 | + |
| 54 | + private static Map.Entry<String, TypeType> newTypeMapEntry(CelType type) { |
| 55 | + return Maps.immutableEntry(type.name(), TypeType.create(type)); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public ImmutableCollection<CelType> types() { |
| 60 | + return COMMON_TYPES.values(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Optional<CelType> findType(String typeName) { |
| 65 | + return Optional.ofNullable(COMMON_TYPES.get(typeName)); |
| 66 | + } |
| 67 | + |
| 68 | + public static DefaultTypeProvider create() { |
| 69 | + return new DefaultTypeProvider(); |
| 70 | + } |
| 71 | + |
| 72 | + private DefaultTypeProvider() {} |
| 73 | +} |
0 commit comments