22
33import com .fasterxml .jackson .core .type .TypeReference ;
44import com .fasterxml .jackson .databind .json .JsonMapper ;
5- import com .google .protobuf .ByteString ;
6- import com .google .protobuf .Timestamp ;
7- import com .google .protobuf .Duration ;
85import com .google .protobuf .Value ;
96
10- import java .time .Instant ;
11- import java .time .LocalDateTime ;
12- import java .time .ZonedDateTime ;
137import java .util .Collections ;
148import java .util .Map ;
159import java .util .stream .Collectors ;
@@ -25,6 +19,7 @@ public class DynamicValueMapper {
2519 *
2620 * @param jsonMetadata the source metadata as JSON bytes
2721 * @return a map with DynamicValue objects
22+ * @throws IllegalArgumentException if any metadata value is not a string
2823 */
2924 public static Map <String , Value > mapJsonToValueMap (byte [] jsonMetadata ) {
3025 if (jsonMetadata == null || jsonMetadata .length == 0 )
@@ -35,7 +30,7 @@ public static Map<String, Value> mapJsonToValueMap(byte[] jsonMetadata) {
3530 });
3631 return mapToValueMap (metadata );
3732 } catch (Exception e ) {
38- return Collections . emptyMap ( );
33+ throw new IllegalArgumentException ( e );
3934 }
4035 }
4136
@@ -44,85 +39,26 @@ public static Map<String, Value> mapJsonToValueMap(byte[] jsonMetadata) {
4439 *
4540 * @param metadata the source metadata map
4641 * @return a map with DynamicValue objects
42+ * @throws IllegalArgumentException if any metadata value is not a string
4743 */
48- public static Map <String , Value > mapToValueMap (Map <String , Object > metadata ) {
44+ public static Map <String , Value > mapToValueMap (Map <String , ? > metadata ) {
4945 if (metadata == null ) {
5046 return Collections .emptyMap ();
5147 }
5248
53- return metadata .entrySet ().stream ()
54- .collect (Collectors .toMap (
55- Map .Entry ::getKey ,
56- entry -> mapToValue (entry .getValue ())
57- ));
58- }
59-
60- /**
61- * Converts a Java object to a DynamicValue protobuf message.
62- *
63- * @param source the source object
64- * @return the corresponding DynamicValue
65- */
66- public static Value mapToValue (Object source ) {
67- if (source == null ) {
68- return Value .newBuilder ()
69- .setNullValue (com .google .protobuf .NullValue .NULL_VALUE )
70- .build ();
49+ for (Map .Entry <String , ?> entry : metadata .entrySet ()) {
50+ if (entry .getValue () != null && !(entry .getValue () instanceof String )) {
51+ throw new IllegalArgumentException (
52+ String .format ("Metadata value for key '%s' must be a string, but was %s" ,
53+ entry .getKey (),
54+ entry .getValue ().getClass ().getSimpleName ())
55+ );
56+ }
7157 }
7258
7359 Value .Builder builder = Value .newBuilder ();
7460
75- if (source instanceof String ) {
76- return builder .setStringValue ((String ) source ).build ();
77- } else if (source instanceof Boolean ) {
78- return builder .setBoolValue ((Boolean ) source ).build ();
79- } else if (source instanceof Integer ) {
80- return builder .setNumberValue ((Integer ) source ).build ();
81- } else if (source instanceof Long ) {
82- return builder .setNumberValue ((Long ) source ).build ();
83- } else if (source instanceof Float ) {
84- return builder .setNumberValue ((Float ) source ).build ();
85- } else if (source instanceof Double ) {
86- return builder .setNumberValue ((Double ) source ).build ();
87- } else if (source instanceof Instant ) {
88- Instant instant = (Instant ) source ;
89- return builder .setStringValue (
90- Timestamp .newBuilder ()
91- .setSeconds (instant .getEpochSecond ())
92- .setNanos (instant .getNano ())
93- .build ().toString ()
94- ).build ();
95- } else if (source instanceof LocalDateTime ) {
96- LocalDateTime localDateTime = (LocalDateTime ) source ;
97- Instant instant = localDateTime .atZone (java .time .ZoneOffset .UTC ).toInstant ();
98- return builder .setStringValue (
99- Timestamp .newBuilder ()
100- .setSeconds (instant .getEpochSecond ())
101- .setNanos (instant .getNano ())
102- .build ().toString ()
103- ).build ();
104- } else if (source instanceof ZonedDateTime ) {
105- ZonedDateTime zonedDateTime = (ZonedDateTime ) source ;
106- Instant instant = zonedDateTime .toInstant ();
107- return builder .setStringValue (
108- Timestamp .newBuilder ()
109- .setSeconds (instant .getEpochSecond ())
110- .setNanos (instant .getNano ())
111- .build ().toString ()
112- ).build ();
113- } else if (source instanceof java .time .Duration ) {
114- java .time .Duration duration = (java .time .Duration ) source ;
115- return builder .setStringValue (
116- Duration .newBuilder ()
117- .setSeconds (duration .getSeconds ())
118- .setNanos (duration .getNano ())
119- .build ().toString ()
120- ).build ();
121- } else if (source instanceof byte []) {
122- return builder .setStringValue (ByteString .copyFrom ((byte []) source ).toStringUtf8 ()).build ();
123- } else {
124- // For any other type, convert to string
125- return builder .setStringValue (source .toString ()).build ();
126- }
61+ return metadata .entrySet ().stream ()
62+ .collect (Collectors .toMap (Map .Entry ::getKey , entry -> builder .setStringValue ((String ) entry .getValue ()).build ()));
12763 }
12864}
0 commit comments