1818import static com .google .common .collect .ImmutableMap .toImmutableMap ;
1919
2020import com .google .common .base .Preconditions ;
21+ import com .google .common .collect .ImmutableList ;
22+ import com .google .common .collect .ImmutableMap ;
23+ import com .google .common .primitives .UnsignedLong ;
2124import com .google .errorprone .annotations .Immutable ;
2225import com .google .protobuf .BoolValue ;
2326import com .google .protobuf .ByteString ;
27+ import com .google .protobuf .BytesValue ;
2428import com .google .protobuf .DoubleValue ;
2529import com .google .protobuf .Duration ;
2630import com .google .protobuf .FloatValue ;
2731import com .google .protobuf .Int32Value ;
2832import com .google .protobuf .Int64Value ;
29- import com .google .protobuf .MessageLite ;
33+ import com .google .protobuf .ListValue ;
3034import com .google .protobuf .MessageLiteOrBuilder ;
3135import com .google .protobuf .StringValue ;
3236import com .google .protobuf .Struct ;
3741import dev .cel .common .annotations .Internal ;
3842import dev .cel .common .internal .ProtoTimeUtils ;
3943import dev .cel .common .internal .WellKnownProto ;
40- import java .util .Optional ;
4144
4245/**
4346 * {@code BaseProtoCelValueConverter} contains the common logic for converting between native Java
5154@ Internal
5255public abstract class BaseProtoCelValueConverter extends CelValueConverter {
5356
54- public abstract CelValue fromProtoMessageToCelValue (MessageLite msg );
55-
5657 /** {@inheritDoc} Protobuf semantics take precedence for conversion. */
5758 @ Override
58- public CelValue fromJavaObjectToCelValue (Object value ) {
59+ public Object toRuntimeValue (Object value ) {
5960 Preconditions .checkNotNull (value );
6061
61- Optional <WellKnownProto > wellKnownProto = WellKnownProto .getByClass (value .getClass ());
62- if (wellKnownProto .isPresent ()) {
63- return fromWellKnownProtoToCelValue ((MessageLiteOrBuilder ) value , wellKnownProto .get ());
64- }
65-
6662 if (value instanceof ByteString ) {
67- return BytesValue . create ( CelByteString .of (((ByteString ) value ).toByteArray () ));
63+ return CelByteString .of (((ByteString ) value ).toByteArray ());
6864 } else if (value instanceof com .google .protobuf .NullValue ) {
6965 return NullValue .NULL_VALUE ;
7066 }
7167
72- return super .fromJavaObjectToCelValue (value );
68+ return super .toRuntimeValue (value );
7369 }
7470
75- protected CelValue fromWellKnownProtoToCelValue (
76- MessageLiteOrBuilder message , WellKnownProto wellKnownProto ) {
71+ protected Object fromWellKnownProto (MessageLiteOrBuilder message , WellKnownProto wellKnownProto ) {
7772 switch (wellKnownProto ) {
7873 case JSON_VALUE :
79- return adaptJsonValueToCelValue ((Value ) message );
74+ return adaptJsonValue ((Value ) message );
8075 case JSON_STRUCT_VALUE :
81- return adaptJsonStructToCelValue ((Struct ) message );
76+ return adaptJsonStruct ((Struct ) message );
8277 case JSON_LIST_VALUE :
83- return adaptJsonListToCelValue (( com . google . protobuf . ListValue ) message );
78+ return adaptJsonList (( ListValue ) message );
8479 case DURATION :
85- return DurationValue . create ( ProtoTimeUtils .toJavaDuration ((Duration ) message ) );
80+ return ProtoTimeUtils .toJavaDuration ((Duration ) message );
8681 case TIMESTAMP :
87- return TimestampValue . create ( ProtoTimeUtils .toJavaInstant ((Timestamp ) message ) );
82+ return ProtoTimeUtils .toJavaInstant ((Timestamp ) message );
8883 case BOOL_VALUE :
89- return fromJavaPrimitiveToCelValue (((BoolValue ) message ).getValue ());
84+ return normalizePrimitive (((BoolValue ) message ).getValue ());
9085 case BYTES_VALUE :
91- return fromJavaPrimitiveToCelValue (
92- ((com .google .protobuf .BytesValue ) message ).getValue ().toByteArray ());
86+ return normalizePrimitive (((BytesValue ) message ).getValue ().toByteArray ());
9387 case DOUBLE_VALUE :
94- return fromJavaPrimitiveToCelValue (((DoubleValue ) message ).getValue ());
88+ return normalizePrimitive (((DoubleValue ) message ).getValue ());
9589 case FLOAT_VALUE :
96- return fromJavaPrimitiveToCelValue (((FloatValue ) message ).getValue ());
90+ return normalizePrimitive (((FloatValue ) message ).getValue ());
9791 case INT32_VALUE :
98- return fromJavaPrimitiveToCelValue (((Int32Value ) message ).getValue ());
92+ return normalizePrimitive (((Int32Value ) message ).getValue ());
9993 case INT64_VALUE :
100- return fromJavaPrimitiveToCelValue (((Int64Value ) message ).getValue ());
94+ return normalizePrimitive (((Int64Value ) message ).getValue ());
10195 case STRING_VALUE :
102- return fromJavaPrimitiveToCelValue (((StringValue ) message ).getValue ());
96+ return normalizePrimitive (((StringValue ) message ).getValue ());
10397 case UINT32_VALUE :
104- return UintValue . create (((UInt32Value ) message ).getValue ());
98+ return UnsignedLong . valueOf (((UInt32Value ) message ).getValue ());
10599 case UINT64_VALUE :
106- return UintValue . create (((UInt64Value ) message ).getValue ());
100+ return UnsignedLong . fromLongBits (((UInt64Value ) message ).getValue ());
107101 default :
108102 throw new UnsupportedOperationException (
109- "Unsupported message to CelValue conversion - " + message );
103+ "Unsupported well known proto conversion - " + wellKnownProto );
110104 }
111105 }
112106
113- private CelValue adaptJsonValueToCelValue (Value value ) {
107+ private Object adaptJsonValue (Value value ) {
114108 switch (value .getKindCase ()) {
115109 case BOOL_VALUE :
116- return fromJavaPrimitiveToCelValue (value .getBoolValue ());
110+ return normalizePrimitive (value .getBoolValue ());
117111 case NUMBER_VALUE :
118- return fromJavaPrimitiveToCelValue (value .getNumberValue ());
112+ return normalizePrimitive (value .getNumberValue ());
119113 case STRING_VALUE :
120- return fromJavaPrimitiveToCelValue (value .getStringValue ());
114+ return normalizePrimitive (value .getStringValue ());
121115 case LIST_VALUE :
122- return adaptJsonListToCelValue (value .getListValue ());
116+ return adaptJsonList (value .getListValue ());
123117 case STRUCT_VALUE :
124- return adaptJsonStructToCelValue (value .getStructValue ());
118+ return adaptJsonStruct (value .getStructValue ());
125119 case NULL_VALUE :
126120 case KIND_NOT_SET : // Fall-through is intended
127121 return NullValue .NULL_VALUE ;
@@ -130,28 +124,23 @@ private CelValue adaptJsonValueToCelValue(Value value) {
130124 "Unsupported Json to CelValue conversion: " + value .getKindCase ());
131125 }
132126
133- private ListValue <CelValue > adaptJsonListToCelValue (com .google .protobuf .ListValue listValue ) {
134- return ImmutableListValue .create (
135- listValue .getValuesList ().stream ()
136- .map (this ::adaptJsonValueToCelValue )
137- .collect (toImmutableList ()));
127+ private ImmutableList <Object > adaptJsonList (ListValue listValue ) {
128+ return listValue .getValuesList ().stream ().map (this ::adaptJsonValue ).collect (toImmutableList ());
138129 }
139130
140- private MapValue <dev .cel .common .values .StringValue , CelValue > adaptJsonStructToCelValue (
141- Struct struct ) {
142- return ImmutableMapValue .create (
143- struct .getFieldsMap ().entrySet ().stream ()
144- .collect (
145- toImmutableMap (
146- e -> {
147- CelValue key = fromJavaObjectToCelValue (e .getKey ());
148- if (!(key instanceof dev .cel .common .values .StringValue )) {
149- throw new IllegalStateException (
150- "Expected a string type for the key, but instead got: " + key );
151- }
152- return (dev .cel .common .values .StringValue ) key ;
153- },
154- e -> adaptJsonValueToCelValue (e .getValue ()))));
131+ private ImmutableMap <String , Object > adaptJsonStruct (Struct struct ) {
132+ return struct .getFieldsMap ().entrySet ().stream ()
133+ .collect (
134+ toImmutableMap (
135+ e -> {
136+ Object key = toRuntimeValue (e .getKey ());
137+ if (!(key instanceof String )) {
138+ throw new IllegalStateException (
139+ "Expected a string type for the key, but instead got: " + key );
140+ }
141+ return (String ) key ;
142+ },
143+ e -> adaptJsonValue (e .getValue ())));
155144 }
156145
157146 protected BaseProtoCelValueConverter () {}
0 commit comments