Repository navigation
Expand file tree
/
Copy pathPartMapper.java
More file actions
116 lines (105 loc) · 5.06 KB
/
Copy pathPartMapper.java
File metadata and controls
116 lines (105 loc) · 5.06 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
package io.a2a.grpc.mapper;
import java.util.Base64;
import com.google.protobuf.ByteString;
import com.google.protobuf.Value;
import io.a2a.spec.DataPart;
import io.a2a.spec.FileContent;
import io.a2a.spec.FilePart;
import io.a2a.spec.FileWithBytes;
import io.a2a.spec.FileWithUri;
import io.a2a.spec.InvalidRequestError;
import io.a2a.spec.Part;
import io.a2a.spec.TextPart;
import java.util.Map;
import org.mapstruct.Mapper;
/**
* Mapper between {@link io.a2a.spec.Part} and {@link io.a2a.grpc.Part}.
* <p>
* Handles polymorphic Part conversion using the proto's oneof content field:
* <ul>
* <li>TextPart - maps to Part.text</li>
* <li>FilePart(FileWithBytes) - maps to Part.raw + Part.filename + Part.media_type</li>
* <li>FilePart(FileWithUri) - maps to Part.url + Part.filename + Part.media_type</li>
* <li>DataPart - maps to Part.data (google.protobuf.Value containing any JSON value: object, array, primitive, or null)</li>
* </ul>
* <p>
* <b>Manual Implementation Required:</b> Must use manual instanceof dispatch to handle protobuf oneof pattern,
* as MapStruct's @SubclassMapping maps to different target types, not different fields of the same type.
*/
@Mapper(config = A2AProtoMapperConfig.class, uses = {A2ACommonFieldMapper.class})
public interface PartMapper {
PartMapper INSTANCE = A2AMappers.getMapper(PartMapper.class);
/**
* Converts domain Part to proto Part.
* Handles TextPart, FilePart (FileWithBytes and FileWithUri), and DataPart polymorphism.
*/
default io.a2a.grpc.Part toProto(Part<?> domain) {
if (domain == null) {
return null;
}
io.a2a.grpc.Part.Builder builder = io.a2a.grpc.Part.newBuilder();
if (domain instanceof TextPart textPart) {
builder.setText(textPart.text());
builder.setMetadata(A2ACommonFieldMapper.INSTANCE.metadataToProto(textPart.metadata()));
} else if (domain instanceof FilePart filePart) {
FileContent fileContent = filePart.file();
if (fileContent instanceof FileWithBytes fileWithBytes) {
// Map to raw (bytes), filename, and media_type
builder.setRaw(ByteString.copyFrom(Base64.getDecoder().decode(fileWithBytes.bytes())));
if (fileWithBytes.name() != null) {
builder.setFilename(fileWithBytes.name());
}
if (fileWithBytes.mimeType() != null) {
builder.setMediaType(fileWithBytes.mimeType());
}
} else if (fileContent instanceof FileWithUri fileWithUri) {
// Map to url, filename, and media_type
builder.setUrl(fileWithUri.uri());
if (fileWithUri.name() != null) {
builder.setFilename(fileWithUri.name());
}
if (fileWithUri.mimeType() != null) {
builder.setMediaType(fileWithUri.mimeType());
}
}
builder.setMetadata(A2ACommonFieldMapper.INSTANCE.metadataToProto(filePart.metadata()));
} else if (domain instanceof DataPart dataPart) {
// Map data to google.protobuf.Value (supports object, array, primitive, or null)
Value dataValue = A2ACommonFieldMapper.INSTANCE.objectToValue(dataPart.data());
builder.setData(dataValue);
builder.setMetadata(A2ACommonFieldMapper.INSTANCE.metadataToProto(dataPart.metadata()));
}
return builder.build();
}
/**
* Converts proto Part to domain Part.
* Reconstructs TextPart, FilePart, or DataPart based on oneof content field.
*/
default Part<?> fromProto(io.a2a.grpc.Part proto) {
if (proto == null) {
return null;
}
Map<String, Object> metadata = A2ACommonFieldMapper.INSTANCE.metadataFromProto(proto.getMetadata());
if (proto.hasText()) {
return new TextPart(proto.getText(), metadata);
} else if (proto.hasRaw()) {
// raw bytes → FilePart(FileWithBytes)
String bytes = Base64.getEncoder().encodeToString(proto.getRaw().toByteArray());
String mimeType = proto.getMediaType().isEmpty() ? "" : proto.getMediaType();
String name = proto.getFilename().isEmpty() ? "" : proto.getFilename();
return new FilePart(new FileWithBytes(mimeType, name, bytes), metadata);
} else if (proto.hasUrl()) {
// url → FilePart(FileWithUri)
String uri = proto.getUrl();
String mimeType = proto.getMediaType().isEmpty() ? null : proto.getMediaType();
String name = proto.getFilename().isEmpty() ? null : proto.getFilename();
return new FilePart(new FileWithUri(mimeType, name, uri), metadata);
} else if (proto.hasData()) {
// data (google.protobuf.Value containing any JSON value) → DataPart
Value dataValue = proto.getData();
Object data = A2ACommonFieldMapper.INSTANCE.valueToObject(dataValue);
return new DataPart(data, metadata);
}
throw new InvalidRequestError();
}
}