|
| 1 | +// Copyright 2023 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.collect.ImmutableList; |
| 18 | +import com.google.common.collect.ImmutableMap; |
| 19 | +import com.google.common.primitives.UnsignedLong; |
| 20 | +import dev.cel.common.CelOptions; |
| 21 | +import dev.cel.common.annotations.Internal; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Map.Entry; |
| 24 | + |
| 25 | +/** |
| 26 | + * {@code CelValueConverter} handles bidirectional conversion between native Java objects to {@link |
| 27 | + * CelValue}. |
| 28 | + * |
| 29 | + * <p>CEL Library Internals. Do Not Use. |
| 30 | + */ |
| 31 | +@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue). |
| 32 | +@Internal |
| 33 | +abstract class CelValueConverter { |
| 34 | + |
| 35 | + protected final CelOptions celOptions; |
| 36 | + |
| 37 | + /** Adapts a plain old Java Object to a {@link CelValue}. */ |
| 38 | + public CelValue fromJavaObjectToCelValue(Object value) { |
| 39 | + if (value instanceof CelValue) { |
| 40 | + return (CelValue) value; |
| 41 | + } |
| 42 | + |
| 43 | + if (value instanceof Iterable) { |
| 44 | + return toListValue((Iterable<Object>) value); |
| 45 | + } else if (value instanceof Map) { |
| 46 | + return toMapValue((Map<Object, Object>) value); |
| 47 | + } |
| 48 | + |
| 49 | + return fromJavaPrimitiveToCelValue(value); |
| 50 | + } |
| 51 | + |
| 52 | + /** Adapts a plain old Java Object that are considered primitives to a {@link CelValue}. */ |
| 53 | + protected CelValue fromJavaPrimitiveToCelValue(Object value) { |
| 54 | + if (value instanceof Boolean) { |
| 55 | + return BoolValue.create((Boolean) value); |
| 56 | + } else if (value instanceof Long) { |
| 57 | + return IntValue.create((Long) value); |
| 58 | + } else if (value instanceof Integer) { |
| 59 | + return IntValue.create((Integer) value); |
| 60 | + } else if (value instanceof String) { |
| 61 | + return StringValue.create((String) value); |
| 62 | + } else if (value instanceof byte[]) { |
| 63 | + return BytesValue.create(CelByteString.of((byte[]) value)); |
| 64 | + } else if (value instanceof Double) { |
| 65 | + return DoubleValue.create((Double) value); |
| 66 | + } else if (value instanceof Float) { |
| 67 | + return DoubleValue.create(Double.valueOf((Float) value)); |
| 68 | + } else if (value instanceof UnsignedLong) { |
| 69 | + return UintValue.create((UnsignedLong) value); |
| 70 | + } |
| 71 | + |
| 72 | + // Fall back to an Opaque value, as a custom class was supplied in the runtime. The legacy |
| 73 | + // interpreter allows this but this should not be allowed when a new runtime is introduced. |
| 74 | + // TODO: Migrate consumers to directly supply an appropriate CelValue. |
| 75 | + return OpaqueValue.create(value.toString(), value); |
| 76 | + } |
| 77 | + |
| 78 | + private ListValue<CelValue> toListValue(Iterable<Object> iterable) { |
| 79 | + ImmutableList.Builder<CelValue> listBuilder = ImmutableList.builder(); |
| 80 | + for (Object entry : iterable) { |
| 81 | + listBuilder.add(fromJavaObjectToCelValue(entry)); |
| 82 | + } |
| 83 | + |
| 84 | + return ImmutableListValue.create(listBuilder.build()); |
| 85 | + } |
| 86 | + |
| 87 | + private MapValue<CelValue, CelValue> toMapValue(Map<Object, Object> map) { |
| 88 | + ImmutableMap.Builder<CelValue, CelValue> mapBuilder = ImmutableMap.builder(); |
| 89 | + for (Entry<Object, Object> entry : map.entrySet()) { |
| 90 | + CelValue mapKey = fromJavaObjectToCelValue(entry.getKey()); |
| 91 | + CelValue mapValue = fromJavaObjectToCelValue(entry.getValue()); |
| 92 | + mapBuilder.put(mapKey, mapValue); |
| 93 | + } |
| 94 | + |
| 95 | + return ImmutableMapValue.create(mapBuilder.buildOrThrow()); |
| 96 | + } |
| 97 | + |
| 98 | + protected CelValueConverter(CelOptions celOptions) { |
| 99 | + this.celOptions = celOptions; |
| 100 | + } |
| 101 | +} |
0 commit comments