Repository navigation
Expand file tree
/
Copy pathA2ACommonFieldMapper.java
More file actions
405 lines (380 loc) · 15.2 KB
/
Copy pathA2ACommonFieldMapper.java
File metadata and controls
405 lines (380 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package io.a2a.grpc.mapper;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
import com.google.protobuf.Value;
import org.mapstruct.Mapper;
import org.mapstruct.Named;
import io.a2a.spec.InvalidParamsError;
/**
* Common field mapping utilities shared across all mappers.
* <p>
* Provides reusable conversion methods for common protobuf ↔ domain transformations:
* <ul>
* <li>Empty string → null conversion (protobuf optional string defaults)</li>
* <li>Timestamp conversions (OffsetDateTime ↔ Protobuf Timestamp, Instant ↔ millis)</li>
* <li>Metadata conversions (Map ↔ Protobuf Struct)</li>
* <li>Empty list → null conversion (protobuf repeated field defaults)</li>
* <li>Zero/false → null conversion (protobuf optional numeric/bool defaults)</li>
* <li>Enum → null conversion (protobuf UNSPECIFIED/UNKNOWN handling)</li>
* </ul>
*/
@Mapper(config = A2AProtoMapperConfig.class, uses = {TaskStateMapper.class})
public interface A2ACommonFieldMapper {
A2ACommonFieldMapper INSTANCE = A2AMappers.getMapper(A2ACommonFieldMapper.class);
/**
* Converts protobuf empty strings to null for optional fields.
* <p>
* Protobuf optional strings return "" when unset, but domain models use null.
* Use this with {@code @Mapping(qualifiedByName = "emptyToNull")}.
*
* @param value the protobuf string value
* @return null if empty/null, otherwise the value
*/
@Named("emptyToNull")
default String emptyToNull(String value) {
return (value == null || value.isEmpty()) ? null : value;
}
/**
* Validates that a required string field is not null or empty.
* <p>
* Throws an exception if the protobuf string is null or empty.
* Use this with {@code @Mapping(qualifiedByName = "requireNonEmpty")}.
*
* @param value the protobuf string value
* @return the value if not null/empty
* @throws IllegalArgumentException if value is null or empty
*/
@Named("requireNonEmpty")
default String requireNonEmpty(String value) {
if (value == null || value.isEmpty()) {
throw new InvalidParamsError("Required field cannot be null or empty");
}
return value;
}
/**
* Converts null strings to empty strings for protobuf.
* <p>
* Domain models use null for optional fields, but protobuf uses "".
* Use this with {@code @Mapping(qualifiedByName = "nullToEmpty")}.
*
* @param value the domain string value
* @return "" if null, otherwise the value
*/
@Named("nullToEmpty")
default String nullToEmpty(String value) {
return value == null ? "" : value;
}
/**
* Converts domain OffsetDateTime to protobuf Timestamp.
* <p>
* Use this with {@code @Mapping(qualifiedByName = "offsetDateTimeToProtoTimestamp")}.
*
* @param dateTime the domain OffsetDateTime
* @return protobuf Timestamp, or default instance if input is null
*/
@Named("offsetDateTimeToProtoTimestamp")
default Timestamp offsetDateTimeToProtoTimestamp(OffsetDateTime dateTime) {
if (dateTime == null) {
return Timestamp.getDefaultInstance();
}
Instant instant = dateTime.toInstant();
return Timestamp.newBuilder()
.setSeconds(instant.getEpochSecond())
.setNanos(instant.getNano())
.build();
}
/**
* Converts protobuf Timestamp to domain OffsetDateTime (UTC).
* <p>
* Use this with {@code @Mapping(qualifiedByName = "protoTimestampToOffsetDateTime")}.
*
* @param timestamp the protobuf Timestamp
* @return OffsetDateTime in UTC, or null if input is null/default
*/
@Named("protoTimestampToOffsetDateTime")
default OffsetDateTime protoTimestampToOffsetDateTime(Timestamp timestamp) {
if (timestamp == null || timestamp.equals(Timestamp.getDefaultInstance())) {
return null;
}
return OffsetDateTime.ofInstant(
Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()),
ZoneOffset.UTC
);
}
/**
* Converts empty lists to null for optional list fields.
* <p>
* Protobuf repeated fields return empty list when unset, but domain models may use null.
* Use this with {@code @Mapping(qualifiedByName = "emptyListToNull")}.
*
* @param list the protobuf list
* @return null if empty/null, otherwise the list
*/
@Named("emptyListToNull")
default <T> java.util.List<T> emptyListToNull(java.util.List<T> list) {
return (list == null || list.isEmpty()) ? null : list;
}
/**
* Converts domain Map to protobuf Struct (generic conversion).
* <p>
* Used for any {@code Map<String, Object>} field that maps to protobuf Struct (header, params, etc.).
* Use this with {@code @Mapping(qualifiedByName = "mapToStruct")}.
*
* @param map the domain map
* @return protobuf Struct, or default instance if input is null
*/
@Named("mapToStruct")
default Struct mapToStruct(Map<String, Object> map) {
if (map == null) {
return Struct.getDefaultInstance();
}
Struct.Builder structBuilder = Struct.newBuilder();
map.forEach((k, v) -> structBuilder.putFields(k, objectToValue(v)));
return structBuilder.build();
}
/**
* Converts protobuf Struct to domain Map (generic conversion).
* <p>
* Used for any protobuf Struct field that maps to {@code Map<String, Object>} (header, params, etc.).
* Use this with {@code @Mapping(qualifiedByName = "structToMap")}.
*
* @param struct the protobuf Struct
* @return domain Map (may be null for empty Struct)
*/
@Named("structToMap")
default Map<String, Object> structToMap(Struct struct) {
if (struct == null || struct.getFieldsCount() == 0) {
return null;
}
return struct.getFieldsMap().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> valueToObject(e.getValue())));
}
/**
* Converts a Java Object to protobuf Value.
* <p>
* Supports String, Number, Boolean, Map, and List types.
* Used internally for struct conversion.
*
* @param value the Java object
* @return protobuf Value
*/
private Value objectToValue(Object value) {
Value.Builder valueBuilder = Value.newBuilder();
if (value instanceof String) {
valueBuilder.setStringValue((String) value);
} else if (value instanceof Number) {
valueBuilder.setNumberValue(((Number) value).doubleValue());
} else if (value instanceof Boolean) {
valueBuilder.setBoolValue((Boolean) value);
} else if (value instanceof Map) {
valueBuilder.setStructValue(mapToStruct((Map<String, Object>) value));
} else if (value instanceof List) {
valueBuilder.setListValue(listToListValue((List<Object>) value));
}
return valueBuilder.build();
}
/**
* Converts protobuf Value to Java Object.
* <p>
* Returns appropriate Java type based on Value's kind.
* Used internally for struct conversion.
*
* @param value the protobuf Value
* @return Java object (String, Double, Boolean, Map, List, or null)
*/
private Object valueToObject(Value value) {
switch (value.getKindCase()) {
case STRUCT_VALUE:
return structToMap(value.getStructValue());
case LIST_VALUE:
return value.getListValue().getValuesList().stream()
.map(this::valueToObject)
.collect(Collectors.toList());
case BOOL_VALUE:
return value.getBoolValue();
case NUMBER_VALUE:
return value.getNumberValue();
case STRING_VALUE:
return value.getStringValue();
case NULL_VALUE:
default:
return null;
}
}
/**
* Converts Java List to protobuf ListValue.
* <p>
* Used internally for struct conversion.
*
* @param list the Java list
* @return protobuf ListValue
*/
private com.google.protobuf.ListValue listToListValue(List<Object> list) {
com.google.protobuf.ListValue.Builder listValueBuilder = com.google.protobuf.ListValue.newBuilder();
if (list != null) {
list.forEach(o -> listValueBuilder.addValues(objectToValue(o)));
}
return listValueBuilder.build();
}
/**
* Converts domain metadata Map to protobuf Struct.
* <p>
* Used for metadata fields in Artifact, Message, Task, and Events.
* Use this with {@code @Mapping(qualifiedByName = "metadataToProto")}.
*
* @param metadata the domain metadata map
* @return protobuf Struct, or default instance if input is null
*/
@Named("metadataToProto")
default Struct metadataToProto(Map<String, Object> metadata) {
return mapToStruct(metadata);
}
/**
* Converts protobuf Struct to domain metadata Map.
* <p>
* Used for metadata fields in Artifact, Message, Task, and Events.
* Use this with {@code @Mapping(qualifiedByName = "metadataFromProto")}.
*
* @param struct the protobuf Struct
* @return domain metadata Map (may be null for empty Struct)
*/
@Named("metadataFromProto")
default Map<String, Object> metadataFromProto(Struct struct) {
if (struct == null || struct.getFieldsCount() == 0) {
return Collections.emptyMap();
}
return structToMap(struct);
}
// ========================================================================
// Optional Numeric/Boolean Conversions
// ========================================================================
/**
* Converts protobuf int to Integer, treating 0 as null (unset).
* <p>
* Protobuf optional int32 fields default to 0 when unset, but domain models use null.
* Use this with {@code @Mapping(qualifiedByName = "zeroToNull")}.
*
* @param value the protobuf int value
* @return Integer or null if value is 0
*/
@Named("zeroToNull")
default Integer zeroToNull(int value) {
return value != 0 ? value : null;
}
/**
* Converts protobuf int to Integer, preserving all values including 0.
* <p>
* Unlike zeroToNull, this method preserves 0 values, allowing compact constructor
* validation to catch invalid values (e.g., pageSize=0 must fail validation).
* For truly optional fields where 0 means "unset", use zeroToNull instead.
* Use this with {@code @Mapping(qualifiedByName = "intToIntegerOrNull")}.
*
* @param value the protobuf int value
* @return Integer (never null for primitive int input)
*/
@Named("intToIntegerOrNull")
default Integer intToIntegerOrNull(int value) {
return value;
}
/**
* Converts protobuf long to Long, treating 0 as null (unset).
* <p>
* Protobuf optional int64 fields default to 0 when unset, but domain models use null.
* Use this with {@code @Mapping(qualifiedByName = "zeroLongToNull")}.
*
* @param value the protobuf long value
* @return Long or null if value is 0
*/
@Named("zeroLongToNull")
default Long zeroLongToNull(long value) {
return value > 0L ? value : null;
}
/**
* Converts protobuf bool to Boolean, treating false as null (unset).
* <p>
* Protobuf optional bool fields default to false when unset, but domain models use null.
* Use this with {@code @Mapping(qualifiedByName = "falseToNull")}.
*
* @param value the protobuf bool value
* @return Boolean or null if value is false
*/
@Named("falseToNull")
default Boolean falseToNull(boolean value) {
return value ? true : null;
}
// ========================================================================
// Instant ↔ Millis Conversions (for int64 timestamp fields)
// ========================================================================
/**
* Converts domain Instant to protobuf milliseconds-since-epoch (int64).
* <p>
* Returns 0 if input is null (protobuf default for unset int64).
* Use this with {@code @Mapping(qualifiedByName = "instantToMillis")}.
*
* @param instant the domain Instant
* @return milliseconds since epoch, or 0 if null
*/
@Named("instantToMillis")
default long instantToMillis(Instant instant) {
return instant != null ? instant.toEpochMilli() : 0L;
}
/**
* Converts protobuf milliseconds-since-epoch (int64) to domain Instant.
* <p>
* Returns null if input is 0 (protobuf default for unset field).
* Throws InvalidParamsError for negative values (invalid timestamps).
* Use this with {@code @Mapping(qualifiedByName = "millisToInstant")}.
*
* @param millis milliseconds since epoch
* @return domain Instant, or null if millis is 0
* @throws InvalidParamsError if millis is negative
*/
@Named("millisToInstant")
default Instant millisToInstant(long millis) {
if (millis < 0L) {
throw new InvalidParamsError(null,
"Timestamp must be a non-negative number of milliseconds since epoch, but got: " + millis,
null);
}
return millis > 0L ? Instant.ofEpochMilli(millis) : null;
}
// ========================================================================
// Enum Conversions (handling UNSPECIFIED/UNKNOWN)
// ========================================================================
/**
* Converts protobuf TaskState to domain TaskState, treating UNSPECIFIED as null.
* <p>
* Protobuf enums default to UNSPECIFIED (0 value) when unset, which maps to null for optional fields.
* However, UNRECOGNIZED (invalid enum values from JSON) throws InvalidParamsError for proper validation.
* Use this with {@code @Mapping(qualifiedByName = "taskStateOrNull")}.
*
* @param state the protobuf TaskState
* @return domain TaskState or null if UNSPECIFIED
* @throws InvalidParamsError if state is UNRECOGNIZED (invalid enum value)
*/
@Named("taskStateOrNull")
default io.a2a.spec.TaskState taskStateOrNull(io.a2a.grpc.TaskState state) {
if (state == null || state == io.a2a.grpc.TaskState.TASK_STATE_UNSPECIFIED) {
return null;
}
// Reject invalid enum values (e.g., "INVALID_STATUS" from JSON)
if (state == io.a2a.grpc.TaskState.UNRECOGNIZED) {
String validStates = java.util.Arrays.stream(io.a2a.spec.TaskState.values())
.filter(s -> s != io.a2a.spec.TaskState.UNKNOWN)
.map(Enum::name)
.collect(java.util.stream.Collectors.joining(", "));
throw new InvalidParamsError(null,
"Invalid task state value. Must be one of: " + validStates,
null);
}
io.a2a.spec.TaskState result = TaskStateMapper.INSTANCE.fromProto(state);
return result;
}
}