Skip to content

Commit 0009f5e

Browse files
committed
re-organize the functions
1 parent e58e281 commit 0009f5e

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 & 6 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 needed across Java packages.
@@ -39,27 +39,40 @@ public static boolean isJsonMediaType(String mediaType) {
3939
return mediaType.equals("application/json") || mediaType.endsWith("+json");
4040
}
4141

42-
42+
/**
43+
* Get context type for command serializer functions.
44+
* @param writer The code writer.
45+
* @param model The model for the service containing the given command.
46+
* @param operation The operation shape for given command.
47+
* @return The TypeScript type for the serializer context
48+
*/
4349
public static String getOperationSerializerContextType(
4450
TypeScriptWriter writer, Model model, OperationShape operation) {
4551
// add default SerdeContext
4652
List<String> contextInterfaceList = getDefaultOperationSerdeContextTypes(writer);
4753
//check if event stream trait exists
48-
if (AddEventStreamDependency.operationHasEventStreamInput(model, operation)
49-
) {
54+
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
55+
if (eventStreamIndex.getInputInfo(operation).isPresent()) {
5056
writer.addImport("EventStreamSerdeContext", "__EventStreamSerdeContext", "@aws-sdk/types");
5157
contextInterfaceList.add("__EventStreamSerdeContext");
5258
}
5359
return String.join(" & ", contextInterfaceList);
5460
}
5561

62+
/**
63+
* Get context type for command deserializer function.
64+
* @param writer The code writer.
65+
* @param model The model for the service containing the given command.
66+
* @param operation The operation shape for given command.
67+
* @return The TypeScript type for the deserializer context
68+
*/
5669
public static String getOperationDeserializerContextType(
5770
TypeScriptWriter writer, Model model, OperationShape operation) {
5871
// add default SerdeContext
5972
List<String> contextInterfaceList = getDefaultOperationSerdeContextTypes(writer);
6073
//check if event stream trait exists
61-
if (AddEventStreamDependency.operationHasEventStreamOutput(model, operation)
62-
) {
74+
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
75+
if (eventStreamIndex.getOutputInfo(operation).isPresent()) {
6376
writer.addImport("EventStreamSerdeContext", "__EventStreamSerdeContext", "@aws-sdk/types");
6477
contextInterfaceList.add("__EventStreamSerdeContext");
6578
}

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)