Skip to content
Open
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 @@ -25,16 +25,21 @@
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import java.util.logging.Logger;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolDependency;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.codegen.core.TopologicalIndex;
import software.amazon.smithy.codegen.core.trace.ArtifactDefinitions;
import software.amazon.smithy.codegen.core.trace.TraceMetadata;
import software.amazon.smithy.codegen.core.trace.TracingSymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.neighbor.Walker;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
Expand Down Expand Up @@ -93,6 +98,7 @@ class CodegenVisitor extends ShapeVisitor.Default<Void> {
runtimePlugins.add(runtimePlugin);
});
});

// Sort the integrations in specified order.
integrations.sort(Comparator.comparingInt(TypeScriptIntegration::getOrder));

Expand All @@ -119,13 +125,44 @@ class CodegenVisitor extends ShapeVisitor.Default<Void> {
for (TypeScriptIntegration integration : integrations) {
resolvedProvider = integration.decorateSymbolProvider(settings, model, resolvedProvider);
}
symbolProvider = SymbolProvider.cache(resolvedProvider);

// Resolve the nullable protocol generator and application protocol.
protocolGenerator = resolveProtocolGenerator(integrations, service, settings);
applicationProtocol = protocolGenerator == null
? ApplicationProtocol.createDefaultHttpApplicationProtocol()
: protocolGenerator.getApplicationProtocol();
// Make the symbol provider a cachingSymbolProvider.
SymbolProvider cachedProvider = SymbolProvider.cache(resolvedProvider);
// Defining Definitions for TraceFile Generation.
ArtifactDefinitions artifactDefinitions = ArtifactDefinitions.builder()
.addType(TypeScriptShapeLinkProvider.FIELD_TYPE,
"Field declaration (includes enum constants)")
.addType(TypeScriptShapeLinkProvider.METHOD_TYPE, "Method declaration")
.addType(TypeScriptShapeLinkProvider.TYPE_TYPE,
"Class, interface (including annotation type), or enum declaration")
.addTag(TypeScriptShapeLinkProvider.SERVICE_TAG, "Service client")
.addTag(TypeScriptShapeLinkProvider.REQUEST_TAG, "AWS SDK request type")
.addTag(TypeScriptShapeLinkProvider.RESPONSE_TAG, "AWS SDK response type")
Comment on lines +144 to +145
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.addTag(TypeScriptShapeLinkProvider.REQUEST_TAG, "AWS SDK request type")
.addTag(TypeScriptShapeLinkProvider.RESPONSE_TAG, "AWS SDK response type")
.addTag(TypeScriptShapeLinkProvider.REQUEST_TAG, "Smithy client request type")
.addTag(TypeScriptShapeLinkProvider.RESPONSE_TAG, "Smithy client response type")

.addTag(TypeScriptShapeLinkProvider.SERIALIZER_TAG, "Command serializer")
.addTag(TypeScriptShapeLinkProvider.DESERIALIZER_TAG, "Command deserializer")
.build();

String serviceId = service.getId().getName();
TraceMetadata artifactMetadata = TraceMetadata.builder()
.setTimestampAsNow()
.id(serviceId)
.version(UUID.randomUUID().toString())
.type("TypeScript")
.build();


// Decorate the symbol provider using the trace file generator.
symbolProvider = TracingSymbolProvider.builder()
.symbolProvider(cachedProvider)
.metadata(artifactMetadata)
.artifactDefinitions(artifactDefinitions)
.shapeLinkCreator(new TypeScriptShapeLinkProvider())
.build();

writers = new TypeScriptDelegator(settings, model, fileManifest, symbolProvider, integrations);
}
Expand Down Expand Up @@ -212,6 +249,13 @@ void execute() {
settings, model, protocol, symbolProvider, writers, protocolGenerator).run();
}

// Write the TraceFile.
TracingSymbolProvider traceProvider = (TracingSymbolProvider) symbolProvider;
String traceName = symbolProvider.toSymbol(service).getName().replace("Client", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notable that the replace here won't do anything for ssdks. That's probably fine though because we don't want them to share a filename, even if they can't be generated in the same plugin.

.toLowerCase() + ".trace.json";
fileManifest.writeFile(traceName,
Node.prettyPrintJson(traceProvider.buildTraceFile().toNode()));

// Write each pending writer.
LOGGER.fine("Flushing TypeScript writers");
List<SymbolDependency> dependencies = writers.getDependencies();
Expand Down Expand Up @@ -428,7 +472,7 @@ private void generateCommands(ServiceShape shape) {
TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(shape));
for (OperationShape operation : containedOperations) {
// Right now this only generates stubs
// Right now this only generates stubs.
if (settings.generateClient()) {
writers.useShapeWriter(operation, commandWriter -> new CommandGenerator(
settings, model, operation, symbolProvider, commandWriter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public Symbol operationShape(OperationShape shape) {
// Add input and output type symbols (XCommandInput / XCommandOutput).
builder.putProperty("inputType", intermediate.toBuilder().name(commandName + "Input").build());
builder.putProperty("outputType", intermediate.toBuilder().name(commandName + "Output").build());

return builder.build();
}

Expand Down Expand Up @@ -327,14 +328,20 @@ private Symbol.Builder addSmithyUseImport(Symbol.Builder builder, String name, S

@Override
public Symbol unionShape(UnionShape shape) {
return createObjectSymbolBuilder(shape).build();
return createObjectSymbolBuilder(shape)
.putProperty("SymbolProvider", this)
.build();
}

@Override
public Symbol memberShape(MemberShape shape) {
Shape targetShape = model.getShape(shape.getTarget())
.orElseThrow(() -> new CodegenException("Shape not found: " + shape.getTarget()));
Symbol targetSymbol = toSymbol(targetShape);
Symbol targetSymbol = toSymbol(targetShape)
.toBuilder()
.putProperty("model", model)
.putProperty("SymbolProvider", this)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this safe? Any plugin that wraps the symbol provider could trip this up. Ideally they wouldn't, since they shouldn't touch members or unions, but it could become an issue.

.build();

if (targetSymbol.getProperties().containsKey(EnumTrait.class.getName())) {
return createMemberSymbolWithEnumTarget(targetSymbol);
Expand Down Expand Up @@ -383,14 +390,20 @@ private Symbol.Builder createObjectSymbolBuilder(Shape shape) {
}

private Symbol.Builder createSymbolBuilder(Shape shape, String typeName) {
return Symbol.builder().putProperty("shape", shape).name(typeName);
return Symbol.builder()
.putProperty("shape", shape)
.putProperty("traceFileNamespace", moduleNameDelegator.formatModuleName(shape, null))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These calls to the moduleNameDelegator are causing issues for the model chunking. This needs address before the PR can be merged. Testing with regenerating an existing client should be performed to assure that the model chunking isn't changed unnecessarily for an existing client.

.putProperty("traceFileNamespaceDelimiter", "/")
.name(typeName);
}

private Symbol.Builder createSymbolBuilder(Shape shape, String typeName, String namespace) {
return Symbol.builder()
.putProperty("shape", shape)
.name(typeName)
.namespace(namespace, "/");
.namespace(namespace, "/")
.putProperty("traceFileNamespace", moduleNameDelegator.formatModuleName(shape, null))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above. Another call to moduleNameDelegator that should be addressed.

.putProperty("traceFileNamespaceDelimiter", "/");
}

private Symbol.Builder createGeneratedSymbolBuilder(Shape shape, String typeName, String namespace) {
Expand Down
Loading