Repository navigation
Expand file tree
/
Copy pathStreamResponseMapper.java
More file actions
87 lines (80 loc) · 3.82 KB
/
Copy pathStreamResponseMapper.java
File metadata and controls
87 lines (80 loc) · 3.82 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
package io.a2a.grpc.mapper;
import io.a2a.spec.Message;
import io.a2a.spec.StreamingEventKind;
import io.a2a.spec.Task;
import io.a2a.spec.TaskArtifactUpdateEvent;
import io.a2a.spec.TaskStatusUpdateEvent;
import org.mapstruct.Mapper;
/**
* Mapper between {@link io.a2a.spec.StreamingEventKind} and {@link io.a2a.grpc.StreamResponse}.
* <p>
* StreamResponse uses a protobuf oneof field to represent polymorphic streaming events.
* StreamingEventKind is a sealed interface with four permitted implementations:
* <ul>
* <li>{@link Task} - Complete task state</li>
* <li>{@link Message} - Full message</li>
* <li>{@link TaskStatusUpdateEvent} - Status update event</li>
* <li>{@link TaskArtifactUpdateEvent} - Artifact update event</li>
* </ul>
* <p>
* This mapper provides bidirectional conversion using instanceof checks (toProto)
* and switch expressions on the oneof case (fromProto).
*/
@Mapper(config = A2AProtoMapperConfig.class,
uses = {TaskMapper.class, MessageMapper.class, TaskStatusUpdateEventMapper.class, TaskArtifactUpdateEventMapper.class})
public interface StreamResponseMapper {
StreamResponseMapper INSTANCE = A2AMappers.getMapper(StreamResponseMapper.class);
/**
* Converts domain StreamingEventKind to proto StreamResponse.
* Uses instanceof checks to determine which oneof field to set.
*
* @param domain the streaming event kind (Task, Message, TaskStatusUpdateEvent, or TaskArtifactUpdateEvent)
* @return the proto StreamResponse with the appropriate oneof field set
*/
default io.a2a.grpc.StreamResponse toProto(StreamingEventKind domain) {
if (domain == null) {
return null;
}
return switch (domain.kind()) {
case Task.STREAMING_EVENT_ID -> io.a2a.grpc.StreamResponse.newBuilder()
.setTask(TaskMapper.INSTANCE.toProto((Task) domain))
.build();
case Message.STREAMING_EVENT_ID -> io.a2a.grpc.StreamResponse.newBuilder()
.setMsg(MessageMapper.INSTANCE.toProto((Message) domain))
.build();
case TaskStatusUpdateEvent.STREAMING_EVENT_ID -> io.a2a.grpc.StreamResponse.newBuilder()
.setStatusUpdate(TaskStatusUpdateEventMapper.INSTANCE.toProto((TaskStatusUpdateEvent) domain))
.build();
case TaskArtifactUpdateEvent.STREAMING_EVENT_ID -> io.a2a.grpc.StreamResponse.newBuilder()
.setArtifactUpdate(TaskArtifactUpdateEventMapper.INSTANCE.toProto((TaskArtifactUpdateEvent) domain))
.build();
default ->
throw new IllegalArgumentException("Unknown StreamingEventKind type: " + domain.getClass().getName());
};
}
/**
* Converts proto StreamResponse to domain StreamingEventKind.
* Uses switch expression on the oneof case to determine which type to return.
*
* @param proto the proto StreamResponse
* @return the corresponding domain streaming event kind
* @throws IllegalArgumentException if the oneof field is not set
*/
default StreamingEventKind fromProto(io.a2a.grpc.StreamResponse proto) {
if (proto == null) {
return null;
}
return switch (proto.getPayloadCase()) {
case TASK ->
TaskMapper.INSTANCE.fromProto(proto.getTask());
case MSG ->
MessageMapper.INSTANCE.fromProto(proto.getMsg());
case STATUS_UPDATE ->
TaskStatusUpdateEventMapper.INSTANCE.fromProto(proto.getStatusUpdate());
case ARTIFACT_UPDATE ->
TaskArtifactUpdateEventMapper.INSTANCE.fromProto(proto.getArtifactUpdate());
case PAYLOAD_NOT_SET ->
throw new IllegalArgumentException("StreamResponse payload oneof field not set");
};
}
}