3232import com .google .protobuf .Int32Value ;
3333import com .google .protobuf .Int64Value ;
3434import com .google .protobuf .ListValue ;
35- import com .google .protobuf .Message ;
3635import com .google .protobuf .MessageLite ;
3736import com .google .protobuf .MessageLiteOrBuilder ;
3837import com .google .protobuf .NullValue ;
@@ -112,7 +111,7 @@ public MessageLite adaptValueToWellKnownProto(Object value, WellKnownProto wellK
112111
113112 public Any adaptValueToAny (Object value , String typeName ) {
114113 if (value instanceof MessageLite ) {
115- return packAnyMessage (( MessageLite ) value , typeName );
114+ return packAny ((( MessageLite ) value ). toByteString () , typeName );
116115 }
117116
118117 if (value instanceof dev .cel .common .values .NullValue ) {
@@ -234,7 +233,7 @@ private ImmutableMap<String, Object> adaptJsonStructToValue(Struct struct) {
234233 .collect (toImmutableMap (Entry ::getKey , e -> adaptJsonToValue (e .getValue ())));
235234 }
236235
237- private Message adaptValueToDouble (Object value ) {
236+ private MessageLite adaptValueToDouble (Object value ) {
238237 if (value instanceof Double ) {
239238 return DoubleValue .of ((Double ) value );
240239 }
@@ -244,7 +243,7 @@ private Message adaptValueToDouble(Object value) {
244243 throw new IllegalArgumentException ("Unexpected value type: " + value );
245244 }
246245
247- private Message adaptValueToFloat (Object value ) {
246+ private MessageLite adaptValueToFloat (Object value ) {
248247 if (value instanceof Double ) {
249248 return FloatValue .of (((Double ) value ).floatValue ());
250249 }
@@ -254,7 +253,7 @@ private Message adaptValueToFloat(Object value) {
254253 throw new IllegalArgumentException ("Unexpected value type: " + value );
255254 }
256255
257- private Message adaptValueToInt32 (Object value ) {
256+ private MessageLite adaptValueToInt32 (Object value ) {
258257 if (value instanceof Integer ) {
259258 return Int32Value .of ((Integer ) value );
260259 }
@@ -264,7 +263,7 @@ private Message adaptValueToInt32(Object value) {
264263 throw new IllegalArgumentException ("Unexpected value type: " + value );
265264 }
266265
267- private Message adaptValueToInt64 (Object value ) {
266+ private MessageLite adaptValueToInt64 (Object value ) {
268267 if (value instanceof Integer ) {
269268 return Int64Value .of (((Integer ) value ).longValue ());
270269 }
@@ -275,29 +274,21 @@ private Message adaptValueToInt64(Object value) {
275274 throw new IllegalArgumentException ("Unexpected value type: " + value );
276275 }
277276
278- private Message adaptValueToUint32 (Object value ) {
277+ private MessageLite adaptValueToUint32 (Object value ) {
279278 if (value instanceof Integer ) {
280279 return UInt32Value .of ((Integer ) value );
281280 }
282281 if (value instanceof Long ) {
283- try {
284- return UInt32Value .of (unsignedIntCheckedCast ((Long ) value ));
285- } catch (IllegalArgumentException e ) {
286- throw new CelNumericOverflowException (e );
287- }
282+ return UInt32Value .of (unsignedIntCheckedCast ((Long ) value ));
288283 }
289284 if (value instanceof UnsignedLong ) {
290- try {
291- return UInt32Value .of (unsignedIntCheckedCast (((UnsignedLong ) value ).longValue ()));
292- } catch (IllegalArgumentException e ) {
293- throw new CelNumericOverflowException (e );
294- }
285+ return UInt32Value .of (unsignedIntCheckedCast (((UnsignedLong ) value ).longValue ()));
295286 }
296287
297288 throw new IllegalArgumentException ("Unexpected value type: " + value );
298289 }
299290
300- private Message adaptValueToUint64 (Object value ) {
291+ private MessageLite adaptValueToUint64 (Object value ) {
301292 if (value instanceof Integer ) {
302293 return UInt64Value .of (UnsignedInts .toLong ((Integer ) value ));
303294 }
@@ -311,15 +302,17 @@ private Message adaptValueToUint64(Object value) {
311302 throw new IllegalArgumentException ("Unexpected value type: " + value );
312303 }
313304
314- private static int intCheckedCast (long value ) {
305+ /** Narrows {@code value} to an int32, or throws {@link CelNumericOverflowException}. */
306+ public static int intCheckedCast (long value ) {
315307 try {
316308 return Ints .checkedCast (value );
317309 } catch (IllegalArgumentException e ) {
318310 throw new CelNumericOverflowException (e );
319311 }
320312 }
321313
322- private static int unsignedIntCheckedCast (long value ) {
314+ /** Narrows {@code value} to a uint32, or throws {@link CelNumericOverflowException}. */
315+ public static int unsignedIntCheckedCast (long value ) {
323316 try {
324317 return UnsignedInts .checkedCast (value );
325318 } catch (IllegalArgumentException e ) {
@@ -328,14 +321,12 @@ private static int unsignedIntCheckedCast(long value) {
328321 }
329322
330323 private static Any packAnyMessage (MessageLite msg , WellKnownProto wellKnownProto ) {
331- return packAnyMessage (msg , wellKnownProto .typeName ());
324+ return packAny (msg . toByteString () , wellKnownProto .typeName ());
332325 }
333326
334- private static Any packAnyMessage (MessageLite msg , String typeUrl ) {
335- return Any .newBuilder ()
336- .setValue (msg .toByteString ())
337- .setTypeUrl ("type.googleapis.com/" + typeUrl )
338- .build ();
327+ /** Packs {@code value}, a serialized message of type {@code typeName}, into an {@link Any}. */
328+ public static Any packAny (ByteString value , String typeName ) {
329+ return Any .newBuilder ().setValue (value ).setTypeUrl ("type.googleapis.com/" + typeName ).build ();
339330 }
340331
341332 private Timestamp adaptValueToProtoTimestamp (Object value ) {
0 commit comments