|
| 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.auto.value.AutoValue; |
| 18 | +import com.google.errorprone.annotations.Immutable; |
| 19 | +import com.google.protobuf.ByteString; |
| 20 | +import com.google.protobuf.FloatValue; |
| 21 | +import com.google.protobuf.Int32Value; |
| 22 | +import com.google.protobuf.Int64Value; |
| 23 | +import com.google.protobuf.MessageOrBuilder; |
| 24 | +import com.google.protobuf.UInt32Value; |
| 25 | +import com.google.protobuf.UInt64Value; |
| 26 | +import dev.cel.common.internal.WellKnownProto; |
| 27 | +import dev.cel.common.types.NullableType; |
| 28 | + |
| 29 | +/** ProtoWrapperValue represents a */ |
| 30 | +@Immutable |
| 31 | +@AutoValue |
| 32 | +public abstract class ProtoWrapperValue extends StructValue { |
| 33 | + |
| 34 | + @Override |
| 35 | + public abstract CelValue value(); |
| 36 | + |
| 37 | + abstract WellKnownProto wellKnownProto(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Retrieves the underlying value being held in the wrapper. For example, if this is a |
| 41 | + * `google.protobuf.IntValue', a Java long is returned. |
| 42 | + */ |
| 43 | + public Object nativeValue() { |
| 44 | + if (wellKnownProto().equals(WellKnownProto.BYTES_VALUE)) { |
| 45 | + // Return the proto ByteString as the underlying primitive value rather than a mutable byte |
| 46 | + // array. |
| 47 | + return ByteString.copyFrom(((BytesValue) value()).value().toByteArray()); |
| 48 | + } |
| 49 | + return value().value(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public abstract NullableType celType(); |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean isZeroValue() { |
| 57 | + return value().isZeroValue(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public CelValue select(String fieldName) { |
| 62 | + throw new UnsupportedOperationException("Wrappers do not support field selection"); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean hasField(String fieldName) { |
| 67 | + throw new UnsupportedOperationException("Wrappers do not support presence tests"); |
| 68 | + } |
| 69 | + |
| 70 | + public static ProtoWrapperValue create( |
| 71 | + MessageOrBuilder wrapperMessage, boolean enableUnsignedLongs) { |
| 72 | + WellKnownProto wellKnownProto = getWellKnownProtoFromWrapperName(wrapperMessage); |
| 73 | + CelValue celValue = newCelValueFromWrapper(wrapperMessage, wellKnownProto, enableUnsignedLongs); |
| 74 | + NullableType nullableType = NullableType.create(celValue.celType()); |
| 75 | + return new AutoValue_ProtoWrapperValue(celValue, wellKnownProto, nullableType); |
| 76 | + } |
| 77 | + |
| 78 | + private static CelValue newCelValueFromWrapper( |
| 79 | + MessageOrBuilder message, WellKnownProto wellKnownProto, boolean enableUnsignedLongs) { |
| 80 | + switch (wellKnownProto) { |
| 81 | + case BOOL_VALUE: |
| 82 | + return BoolValue.create(((com.google.protobuf.BoolValue) message).getValue()); |
| 83 | + case BYTES_VALUE: |
| 84 | + return BytesValue.create( |
| 85 | + CelByteString.of(((com.google.protobuf.BytesValue) message).getValue().toByteArray())); |
| 86 | + case DOUBLE_VALUE: |
| 87 | + return DoubleValue.create(((com.google.protobuf.DoubleValue) message).getValue()); |
| 88 | + case FLOAT_VALUE: |
| 89 | + return DoubleValue.create(((FloatValue) message).getValue()); |
| 90 | + case INT32_VALUE: |
| 91 | + return IntValue.create(((Int32Value) message).getValue()); |
| 92 | + case INT64_VALUE: |
| 93 | + return IntValue.create(((Int64Value) message).getValue()); |
| 94 | + case STRING_VALUE: |
| 95 | + return StringValue.create(((com.google.protobuf.StringValue) message).getValue()); |
| 96 | + case UINT32_VALUE: |
| 97 | + return UintValue.create(((UInt32Value) message).getValue(), enableUnsignedLongs); |
| 98 | + case UINT64_VALUE: |
| 99 | + return UintValue.create(((UInt64Value) message).getValue(), enableUnsignedLongs); |
| 100 | + default: |
| 101 | + throw new IllegalArgumentException( |
| 102 | + "Should only be called for wrapper types. Got: " + wellKnownProto.name()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static WellKnownProto getWellKnownProtoFromWrapperName(MessageOrBuilder message) { |
| 107 | + WellKnownProto wellKnownProto = |
| 108 | + WellKnownProto.getByDescriptorName(message.getDescriptorForType().getFullName()); |
| 109 | + if (!wellKnownProto.isWrapperType()) { |
| 110 | + throw new IllegalArgumentException("Expected a wrapper type. Got: " + wellKnownProto.name()); |
| 111 | + } |
| 112 | + |
| 113 | + return wellKnownProto; |
| 114 | + } |
| 115 | +} |
0 commit comments