|
| 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.base.Preconditions; |
| 18 | +import com.google.common.collect.ImmutableList; |
| 19 | +import com.google.common.collect.ImmutableMap; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +/** |
| 25 | + * {@code CombinedCelValueConverter} delegates value conversion to a list of underlying {@link |
| 26 | + * CelValueConverter}s. |
| 27 | + */ |
| 28 | +@SuppressWarnings("unchecked") |
| 29 | +public final class CombinedCelValueConverter extends CelValueConverter { |
| 30 | + private final ImmutableList<CelValueConverter> converters; |
| 31 | + |
| 32 | + /** Creates a new instance of {@code CombinedCelValueConverter}. */ |
| 33 | + public static CombinedCelValueConverter combine(ImmutableList<CelValueConverter> converters) { |
| 34 | + return new CombinedCelValueConverter(converters); |
| 35 | + } |
| 36 | + |
| 37 | + private CombinedCelValueConverter(ImmutableList<CelValueConverter> converters) { |
| 38 | + this.converters = Preconditions.checkNotNull(converters); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public Object toRuntimeValue(Object value) { |
| 43 | + Preconditions.checkNotNull(value); |
| 44 | + |
| 45 | + if (value instanceof CelValue) { |
| 46 | + return value; |
| 47 | + } |
| 48 | + |
| 49 | + if (value instanceof Collection) { |
| 50 | + Collection<Object> collection = (Collection<Object>) value; |
| 51 | + ImmutableList.Builder<Object> listBuilder = |
| 52 | + ImmutableList.builderWithExpectedSize(collection.size()); |
| 53 | + for (Object entry : collection) { |
| 54 | + listBuilder.add(toRuntimeValue(entry)); |
| 55 | + } |
| 56 | + return listBuilder.build(); |
| 57 | + } |
| 58 | + |
| 59 | + if (value instanceof Map) { |
| 60 | + Map<Object, Object> map = (Map<Object, Object>) value; |
| 61 | + ImmutableMap.Builder<Object, Object> mapBuilder = |
| 62 | + ImmutableMap.builderWithExpectedSize(map.size()); |
| 63 | + for (Map.Entry<Object, Object> entry : map.entrySet()) { |
| 64 | + mapBuilder.put(toRuntimeValue(entry.getKey()), toRuntimeValue(entry.getValue())); |
| 65 | + } |
| 66 | + return mapBuilder.buildOrThrow(); |
| 67 | + } |
| 68 | + |
| 69 | + if (value instanceof Optional) { |
| 70 | + Optional<Object> optionalValue = (Optional<Object>) value; |
| 71 | + return optionalValue |
| 72 | + .map(this::toRuntimeValue) |
| 73 | + .map(OptionalValue::create) |
| 74 | + .orElse(OptionalValue.EMPTY); |
| 75 | + } |
| 76 | + |
| 77 | + // For non-container types, try each converter |
| 78 | + for (CelValueConverter converter : converters) { |
| 79 | + Object result = converter.toRuntimeValue(value); |
| 80 | + if (result != value) { |
| 81 | + return result; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return normalizePrimitive(value); |
| 86 | + } |
| 87 | +} |
0 commit comments