Skip to content

Commit bf83bd8

Browse files
committed
re-organize the functions
1 parent 380dcb4 commit bf83bd8

File tree

4 files changed

+263
-277
lines changed

4 files changed

+263
-277
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.util.List;
2020

2121
import software.amazon.smithy.model.Model;
22+
import software.amazon.smithy.model.knowledge.EventStreamIndex;
2223
import software.amazon.smithy.model.shapes.OperationShape;
23-
import software.amazon.smithy.typescript.codegen.integration.AddEventStreamDependency;
2424

2525
/**
2626
* Utility methods for generators for each components.
@@ -29,26 +29,40 @@ public final class CodegenUtils {
2929

3030
private CodegenUtils() {}
3131

32+
/**
33+
* Get context type for command serializer functions.
34+
* @param writer The code writer.
35+
* @param model The model for the service containing the given command.
36+
* @param operation The operation shape for given command.
37+
* @return The TypeScript type for the serializer context
38+
*/
3239
public static String getOperationSerializerContextType(
3340
TypeScriptWriter writer, Model model, OperationShape operation) {
3441
// add default SerdeContext
3542
List<String> contextInterfaceList = getDefaultOperationSerdeContextTypes(writer);
3643
//check if event stream trait exists
37-
if (AddEventStreamDependency.operationHasEventStreamInput(model, operation)
38-
) {
44+
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
45+
if (eventStreamIndex.getInputInfo(operation).isPresent()) {
3946
writer.addImport("EventStreamSerdeContext", "__EventStreamSerdeContext", "@aws-sdk/types");
4047
contextInterfaceList.add("__EventStreamSerdeContext");
4148
}
4249
return String.join(" & ", contextInterfaceList);
4350
}
4451

52+
/**
53+
* Get context type for command deserializer function.
54+
* @param writer The code writer.
55+
* @param model The model for the service containing the given command.
56+
* @param operation The operation shape for given command.
57+
* @return The TypeScript type for the deserializer context
58+
*/
4559
public static String getOperationDeserializerContextType(
4660
TypeScriptWriter writer, Model model, OperationShape operation) {
4761
// add default SerdeContext
4862
List<String> contextInterfaceList = getDefaultOperationSerdeContextTypes(writer);
4963
//check if event stream trait exists
50-
if (AddEventStreamDependency.operationHasEventStreamOutput(model, operation)
51-
) {
64+
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
65+
if (eventStreamIndex.getOutputInfo(operation).isPresent()) {
5266
writer.addImport("EventStreamSerdeContext", "__EventStreamSerdeContext", "@aws-sdk/types");
5367
contextInterfaceList.add("__EventStreamSerdeContext");
5468
}

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

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import software.amazon.smithy.model.knowledge.TopDownIndex;
2525
import software.amazon.smithy.model.shapes.OperationShape;
2626
import software.amazon.smithy.model.shapes.ServiceShape;
27-
import software.amazon.smithy.model.shapes.Shape;
2827
import software.amazon.smithy.typescript.codegen.LanguageTarget;
2928
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
3029
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
@@ -34,7 +33,7 @@
3433
/**
3534
* Adds event streams if needed.
3635
*/
37-
public class AddEventStreamDependency implements TypeScriptIntegration {
36+
public final class AddEventStreamDependency implements TypeScriptIntegration {
3837

3938
@Override
4039
public List<RuntimeClientPlugin> getClientPlugins() {
@@ -100,67 +99,13 @@ private static boolean hasEventStream(
10099
) {
101100
TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class);
102101
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
102+
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
103103
for (OperationShape operation : operations) {
104-
if (operationHasEventStream(model, service, operation)) {
104+
if (eventStreamIndex.getInputInfo(operation).isPresent()
105+
|| eventStreamIndex.getOutputInfo(operation).isPresent()) {
105106
return true;
106107
}
107108
}
108109
return false;
109110
}
110-
111-
public static final boolean operationHasEventStream(
112-
Model model,
113-
ServiceShape service,
114-
OperationShape operation
115-
) {
116-
if (operationHasEventStreamInput(model, operation) || operationHasEventStreamOutput(model, operation)) {
117-
return true;
118-
}
119-
return false;
120-
}
121-
122-
public static final boolean operationHasEventStreamInput(
123-
Model model,
124-
OperationShape operation
125-
) {
126-
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
127-
if (eventStreamIndex.getInputInfo(operation).isPresent()) {
128-
return true;
129-
}
130-
return false;
131-
}
132-
133-
public static final boolean operationHasEventStreamOutput(
134-
Model model,
135-
OperationShape operation
136-
) {
137-
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
138-
if (eventStreamIndex.getOutputInfo(operation).isPresent()) {
139-
return true;
140-
}
141-
return false;
142-
}
143-
144-
/**
145-
* The value of event header 'type' property of given shape.
146-
*
147-
* @param shape shape bond to event header
148-
* @return string literal for the event message header type
149-
*/
150-
public static String getEventHeaderType(Shape shape) {
151-
switch (shape.getType()) {
152-
case BOOLEAN:
153-
case BYTE:
154-
case SHORT:
155-
case INTEGER:
156-
case LONG:
157-
case STRING:
158-
case TIMESTAMP:
159-
return shape.getType().toString();
160-
case BLOB:
161-
return "binary";
162-
default:
163-
return "binary";
164-
}
165-
}
166111
}

0 commit comments

Comments
 (0)