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 ;
33+ import com .google .protobuf .ListValue ;
2934import com .google .protobuf .MessageLite ;
3035import com .google .protobuf .MessageLiteOrBuilder ;
3136import com .google .protobuf .StringValue ;
3742import dev .cel .common .annotations .Internal ;
3843import dev .cel .common .internal .ProtoTimeUtils ;
3944import dev .cel .common .internal .WellKnownProto ;
40- import java .util .Optional ;
4145
4246/**
4347 * {@code BaseProtoCelValueConverter} contains the common logic for converting between native Java
5155@ Internal
5256public abstract class BaseProtoCelValueConverter extends CelValueConverter {
5357
54- public abstract CelValue fromProtoMessageToCelValue (MessageLite msg );
58+ public abstract Object fromProtoMessageToCelValue (MessageLite msg );
5559
5660 /** {@inheritDoc} Protobuf semantics take precedence for conversion. */
5761 @ Override
58- public CelValue fromJavaObjectToCelValue (Object value ) {
62+ public Object fromJavaObjectToCelValue (Object value ) {
5963 Preconditions .checkNotNull (value );
6064
61- Optional <WellKnownProto > wellKnownProto = WellKnownProto .getByClass (value .getClass ());
62- if (wellKnownProto .isPresent ()) {
63- return fromWellKnownProtoToCelValue ((MessageLiteOrBuilder ) value , wellKnownProto .get ());
64- }
65-
66- if (value instanceof ByteString ) {
67- return BytesValue .create (CelByteString .of (((ByteString ) value ).toByteArray ()));
65+ if (value instanceof MessageLite ) {
66+ return fromProtoMessageToCelValue ((MessageLite ) value );
67+ } else if (value instanceof ByteString ) {
68+ return CelByteString .of (((ByteString ) value ).toByteArray ());
6869 } else if (value instanceof com .google .protobuf .NullValue ) {
6970 return NullValue .NULL_VALUE ;
7071 }
7172
7273 return super .fromJavaObjectToCelValue (value );
7374 }
7475
75- protected CelValue fromWellKnownProtoToCelValue (
76+ protected Object fromWellKnownProtoToCelValue (
7677 MessageLiteOrBuilder message , WellKnownProto wellKnownProto ) {
7778 switch (wellKnownProto ) {
7879 case JSON_VALUE :
79- return adaptJsonValueToCelValue ((Value ) message );
80+ return adaptJsonValue ((Value ) message );
8081 case JSON_STRUCT_VALUE :
81- return adaptJsonStructToCelValue ((Struct ) message );
82+ return adaptJsonStruct ((Struct ) message );
8283 case JSON_LIST_VALUE :
83- return adaptJsonListToCelValue (( com . google . protobuf . ListValue ) message );
84+ return adaptJsonList (( ListValue ) message );
8485 case DURATION :
85- return DurationValue . create ( ProtoTimeUtils .toJavaDuration ((Duration ) message ) );
86+ return ProtoTimeUtils .toJavaDuration ((Duration ) message );
8687 case TIMESTAMP :
87- return TimestampValue . create ( ProtoTimeUtils .toJavaInstant ((Timestamp ) message ) );
88+ return ProtoTimeUtils .toJavaInstant ((Timestamp ) message );
8889 case BOOL_VALUE :
8990 return fromJavaPrimitiveToCelValue (((BoolValue ) message ).getValue ());
9091 case BYTES_VALUE :
91- return fromJavaPrimitiveToCelValue (
92- ((com .google .protobuf .BytesValue ) message ).getValue ().toByteArray ());
92+ return fromJavaPrimitiveToCelValue (((BytesValue ) message ).getValue ().toByteArray ());
9393 case DOUBLE_VALUE :
9494 return fromJavaPrimitiveToCelValue (((DoubleValue ) message ).getValue ());
9595 case FLOAT_VALUE :
@@ -101,16 +101,16 @@ protected CelValue fromWellKnownProtoToCelValue(
101101 case STRING_VALUE :
102102 return fromJavaPrimitiveToCelValue (((StringValue ) message ).getValue ());
103103 case UINT32_VALUE :
104- return UintValue . create (((UInt32Value ) message ).getValue ());
104+ return UnsignedLong . valueOf (((UInt32Value ) message ).getValue ());
105105 case UINT64_VALUE :
106- return UintValue . create (((UInt64Value ) message ).getValue ());
106+ return UnsignedLong . fromLongBits (((UInt64Value ) message ).getValue ());
107107 default :
108108 throw new UnsupportedOperationException (
109- "Unsupported message to CelValue conversion - " + message );
109+ "Unsupported well known proto conversion - " + wellKnownProto );
110110 }
111111 }
112112
113- private CelValue adaptJsonValueToCelValue (Value value ) {
113+ private Object adaptJsonValue (Value value ) {
114114 switch (value .getKindCase ()) {
115115 case BOOL_VALUE :
116116 return fromJavaPrimitiveToCelValue (value .getBoolValue ());
@@ -119,9 +119,9 @@ private CelValue adaptJsonValueToCelValue(Value value) {
119119 case STRING_VALUE :
120120 return fromJavaPrimitiveToCelValue (value .getStringValue ());
121121 case LIST_VALUE :
122- return adaptJsonListToCelValue (value .getListValue ());
122+ return adaptJsonList (value .getListValue ());
123123 case STRUCT_VALUE :
124- return adaptJsonStructToCelValue (value .getStructValue ());
124+ return adaptJsonStruct (value .getStructValue ());
125125 case NULL_VALUE :
126126 case KIND_NOT_SET : // Fall-through is intended
127127 return NullValue .NULL_VALUE ;
@@ -130,28 +130,23 @@ private CelValue adaptJsonValueToCelValue(Value value) {
130130 "Unsupported Json to CelValue conversion: " + value .getKindCase ());
131131 }
132132
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 ()));
133+ private ImmutableList <Object > adaptJsonList (ListValue listValue ) {
134+ return listValue .getValuesList ().stream ().map (this ::adaptJsonValue ).collect (toImmutableList ());
138135 }
139136
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 ()))));
137+ private ImmutableMap <String , Object > adaptJsonStruct (Struct struct ) {
138+ return struct .getFieldsMap ().entrySet ().stream ()
139+ .collect (
140+ toImmutableMap (
141+ e -> {
142+ Object key = fromJavaObjectToCelValue (e .getKey ());
143+ if (!(key instanceof String )) {
144+ throw new IllegalStateException (
145+ "Expected a string type for the key, but instead got: " + key );
146+ }
147+ return (String ) key ;
148+ },
149+ e -> adaptJsonValue (e .getValue ())));
155150 }
156151
157152 protected BaseProtoCelValueConverter () {}
0 commit comments