Skip to content

support eventstream traits #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

package software.amazon.smithy.typescript.codegen;

import java.util.ArrayList;
import java.util.List;

import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.EventStreamIndex;
import software.amazon.smithy.model.shapes.OperationShape;

/**
* Utility methods needed across Java packages.
*/
Expand All @@ -31,4 +38,58 @@ private CodegenUtils() {}
public static boolean isJsonMediaType(String mediaType) {
return mediaType.equals("application/json") || mediaType.endsWith("+json");
}

/**
* Get context type for command serializer functions.
* @param writer The code writer.
* @param model The model for the service containing the given command.
* @param operation The operation shape for given command.
* @return The TypeScript type for the serializer context
*/
public static String getOperationSerializerContextType(
TypeScriptWriter writer,
Model model,
OperationShape operation
) {
// Get default SerdeContext.
List<String> contextInterfaceList = getDefaultOperationSerdeContextTypes(writer);
// If event stream trait exists, add corresponding serde context type to the intersection type.
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
if (eventStreamIndex.getInputInfo(operation).isPresent()) {
writer.addImport("EventStreamSerdeContext", "__EventStreamSerdeContext", "@aws-sdk/types");
contextInterfaceList.add("__EventStreamSerdeContext");
}
return String.join(" & ", contextInterfaceList);
}

/**
* Get context type for command deserializer function.
* @param writer The code writer.
* @param model The model for the service containing the given command.
* @param operation The operation shape for given command.
* @return The TypeScript type for the deserializer context
*/
public static String getOperationDeserializerContextType(
TypeScriptWriter writer,
Model model,
OperationShape operation
) {
// Get default SerdeContext.
List<String> contextInterfaceList = getDefaultOperationSerdeContextTypes(writer);
// If event stream trait exists, add corresponding serde context type to the intersection type.
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
if (eventStreamIndex.getOutputInfo(operation).isPresent()) {
writer.addImport("EventStreamSerdeContext", "__EventStreamSerdeContext", "@aws-sdk/types");
contextInterfaceList.add("__EventStreamSerdeContext");
}
return String.join(" & ", contextInterfaceList);
}

private static List<String> getDefaultOperationSerdeContextTypes(TypeScriptWriter writer) {
List<String> contextInterfaceList = new ArrayList<>();
// Get default SerdeContext.
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
contextInterfaceList.add("__SerdeContext");
return contextInterfaceList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public void run() {
writer.addImport("Handler", "Handler", "@aws-sdk/types");
writer.addImport("HandlerExecutionContext", "HandlerExecutionContext", "@aws-sdk/types");
writer.addImport("MiddlewareStack", "MiddlewareStack", "@aws-sdk/types");
writer.addImport("SerdeContext", "SerdeContext", "@aws-sdk/types");

addInputAndOutputTypes();

Expand Down Expand Up @@ -204,7 +203,7 @@ private void writeSerde() {
.write("private serialize(")
.indent()
.write("input: $T,", inputType)
.write("context: SerdeContext")
.write("context: $L", CodegenUtils.getOperationSerializerContextType(writer, model, operation))
.dedent()
.openBlock(
"): Promise<$T> {", "}",
Expand All @@ -216,7 +215,7 @@ private void writeSerde() {
.write("private deserialize(")
.indent()
.write("output: $T,", applicationProtocol.getResponseType())
.write("context: SerdeContext")
.write("context: $L", CodegenUtils.getOperationDeserializerContextType(writer, model, operation))
.dedent()
.openBlock("): Promise<$T> {", "}", outputType, () -> writeSerdeDispatcher(false))
.write("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import software.amazon.smithy.model.shapes.ToShapeId;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.EventStreamTrait;
import software.amazon.smithy.model.traits.MediaTypeTrait;
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.utils.StringUtils;
Expand Down Expand Up @@ -321,6 +322,10 @@ public Symbol memberShape(MemberShape shape) {
return createMemberSymbolWithEnumTarget(targetSymbol);
}

if (shape.hasTrait(EventStreamTrait.class)) {
return createMemberSymbolWithEventStream(targetSymbol);
}

return targetSymbol;
}

Expand All @@ -332,6 +337,14 @@ private Symbol createMemberSymbolWithEnumTarget(Symbol targetSymbol) {
.build();
}

private Symbol createMemberSymbolWithEventStream(Symbol targetSymbol) {
return targetSymbol.toBuilder()
.namespace(null, "/")
.name(String.format("AsyncIterable<%s>", targetSymbol.getName()))
.addReference(targetSymbol)
.build();
}

@Override
public Symbol timestampShape(TimestampShape shape) {
return createSymbolBuilder(shape, "Date").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolDependency;
import software.amazon.smithy.codegen.core.SymbolDependencyContainer;
Expand Down Expand Up @@ -67,6 +68,12 @@ public enum TypeScriptDependency implements SymbolDependencyContainer {
AWS_SDK_FETCH_HTTP_HANDLER("dependencies", "@aws-sdk/fetch-http-handler", "^1.0.0-alpha.1", false),
AWS_SDK_NODE_HTTP_HANDLER("dependencies", "@aws-sdk/node-http-handler", "^1.0.0-alpha.1", false),

// Conditionally added if a event stream shape is found anywhere in the model
AWS_SDK_EVENTSTREAM_SERDE_CONFIG_RESOLVER("dependencies", "@aws-sdk/eventstream-serde-config-resolver",
"^1.0.0-alpha.0", false),
AWS_SDK_EVENTSTREAM_SERDE_NODE("dependencies", "@aws-sdk/eventstream-serde-node", "^1.0.0-alpha.0", false),
AWS_SDK_EVENTSTREAM_SERDE_BROWSER("dependencies", "@aws-sdk/eventstream-serde-browser", "^1.0.0-alpha.0", false),

// Conditionally added if a big decimal shape is found in a model.
BIG_JS("dependencies", "big.js", "^5.2.2", false),
TYPES_BIG_JS("devDependencies", "@types/big.js", "^4.0.5", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

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

import java.util.List;
import java.util.Set;

import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.EventStreamIndex;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.typescript.codegen.LanguageTarget;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.utils.ListUtils;

/**
* Adds event streams if needed.
*/
public final class AddEventStreamDependency implements TypeScriptIntegration {

@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return ListUtils.of(
RuntimeClientPlugin.builder()
.withConventions(TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_CONFIG_RESOLVER.dependency,
"EventStreamSerde", RuntimeClientPlugin.Convention.HAS_CONFIG)
.servicePredicate(AddEventStreamDependency::hasEventStream)
.build()
);
}

@Override
public void addConfigInterfaceFields(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
if (!hasEventStream(model, settings.getService(model))) {
return;
}

writer.addDependency(TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_CONFIG_RESOLVER);
writer.addImport("EventStreamSerdeProvider", "__EventStreamSerdeProvider",
TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.writeDocs("The function that provides necessary utilities for generating and signing event stream");
writer.write("eventStreamSerdeProvider?: __EventStreamSerdeProvider;\n");
}

@Override
public void addRuntimeConfigValues(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer,
LanguageTarget target
) {
if (!hasEventStream(model, settings.getService(model))) {
return;
}

switch (target) {
case NODE:
writer.addDependency(TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_NODE);
writer.addImport("eventStreamSerdeProvider", "eventStreamSerdeProvider",
TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_NODE.packageName);
writer.write("eventStreamSerdeProvider");
break;
case BROWSER:
writer.addDependency(TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_BROWSER);
writer.addImport("eventStreamSerdeProvider", "eventStreamSerdeProvider",
TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_BROWSER.packageName);
writer.write("eventStreamSerdeProvider");
break;
case REACT_NATIVE:
// TODO: add ReactNative eventstream support
writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY);
writer.addImport("invalidFunction", "invalidFunction",
TypeScriptDependency.INVALID_DEPENDENCY.packageName);
writer.write("eventStreamSerdeProvider: invalidFunction(\"event stream is not supported in "
+ "ReactNative\") as any,");
break;
default:
// do nothing
}
}

private static boolean hasEventStream(
Model model,
ServiceShape service
) {
TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class);
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
EventStreamIndex eventStreamIndex = model.getKnowledge(EventStreamIndex.class);
for (OperationShape operation : operations) {
if (eventStreamIndex.getInputInfo(operation).isPresent()
|| eventStreamIndex.getOutputInfo(operation).isPresent()
) {
return true;
}
}
return false;
}
}
Loading