Skip to content

Commit a4441a7

Browse files
committed
Add event stream and events unions serializer
1 parent 15534c7 commit a4441a7

File tree

3 files changed

+419
-289
lines changed

3 files changed

+419
-289
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java

Lines changed: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
package software.amazon.smithy.typescript.codegen.integration;
1717

18-
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG;
19-
2018
import java.util.List;
2119
import java.util.Set;
2220

@@ -26,6 +24,7 @@
2624
import software.amazon.smithy.model.knowledge.TopDownIndex;
2725
import software.amazon.smithy.model.shapes.OperationShape;
2826
import software.amazon.smithy.model.shapes.ServiceShape;
27+
import software.amazon.smithy.model.shapes.Shape;
2928
import software.amazon.smithy.typescript.codegen.LanguageTarget;
3029
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
3130
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
@@ -37,14 +36,79 @@
3736
*/
3837
public class EventStreamGenerator implements TypeScriptIntegration {
3938

39+
public static final boolean hasEventStream(
40+
Model model,
41+
ServiceShape service
42+
) {
43+
TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class);
44+
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
45+
for (OperationShape operation : operations) {
46+
if (operationHasEventStream(model, service, operation)) {
47+
return true;
48+
}
49+
}
50+
return false;
51+
}
52+
53+
public static final boolean operationHasEventStream(
54+
Model model,
55+
ServiceShape service,
56+
OperationShape operation
57+
) {
58+
if (operationHasEventStreamInput(model, operation) || operationHasEventStreamOutput(model, operation)) {
59+
return true;
60+
}
61+
return false;
62+
}
63+
64+
public static final boolean operationHasEventStreamInput(
65+
Model model,
66+
OperationShape operation
67+
) {
68+
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
69+
if (eventStreamIndex.getInputInfo(operation).isPresent()) {
70+
return true;
71+
}
72+
return false;
73+
}
74+
75+
public static final boolean operationHasEventStreamOutput(
76+
Model model,
77+
OperationShape operation
78+
) {
79+
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
80+
if (eventStreamIndex.getOutputInfo(operation).isPresent()) {
81+
return true;
82+
}
83+
return false;
84+
}
85+
86+
public static String getEventHeaderType(Shape shape) {
87+
switch (shape.getType()) {
88+
case BOOLEAN:
89+
case BYTE:
90+
case SHORT:
91+
case INTEGER:
92+
case LONG:
93+
case STRING:
94+
case TIMESTAMP:
95+
return shape.getType().toString();
96+
case BLOB:
97+
return "binary";
98+
default:
99+
return "binary";
100+
}
101+
102+
}
103+
40104
@Override
41105
public List<RuntimeClientPlugin> getClientPlugins() {
42106
return ListUtils.of(
43107
RuntimeClientPlugin.builder()
44108
.withConventions(
45109
TypeScriptDependency.MIDDLEWARE_EVENT_STREAM.dependency,
46110
"EventStream",
47-
HAS_CONFIG
111+
RuntimeClientPlugin.Convention.HAS_CONFIG
48112
)
49113
.servicePredicate(EventStreamGenerator::hasEventStream)
50114
.build()
@@ -106,51 +170,4 @@ public void addRuntimeConfigValues(
106170
// do nothing
107171
}
108172
}
109-
110-
public static final boolean hasEventStream(
111-
Model model,
112-
ServiceShape service
113-
) {
114-
TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class);
115-
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
116-
for (OperationShape operation : operations) {
117-
if (operationHasEventStream(model, service, operation)) {
118-
return true;
119-
}
120-
}
121-
return false;
122-
}
123-
124-
public static final boolean operationHasEventStream(
125-
Model model,
126-
ServiceShape service,
127-
OperationShape operation
128-
) {
129-
if (operationHasEventStreamInput(model, operation) || operationHasEventStreamOutput(model, operation)) {
130-
return true;
131-
}
132-
return false;
133-
}
134-
135-
public static final boolean operationHasEventStreamInput(
136-
Model model,
137-
OperationShape operation
138-
) {
139-
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
140-
if (eventStreamIndex.getInputInfo(operation).isPresent()) {
141-
return true;
142-
}
143-
return false;
144-
}
145-
146-
public static final boolean operationHasEventStreamOutput(
147-
Model model,
148-
OperationShape operation
149-
) {
150-
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
151-
if (eventStreamIndex.getOutputInfo(operation).isPresent()) {
152-
return true;
153-
}
154-
return false;
155-
}
156173
}

0 commit comments

Comments
 (0)