-
Notifications
You must be signed in to change notification settings - Fork 105
Use server symbol provider in protocol test gen #284
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
JordonPhillips
merged 2 commits into
smithy-lang:ssdk
from
JordonPhillips:update-ssdk-request-tests
Mar 15, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...degen/src/main/java/software/amazon/smithy/typescript/codegen/ServerCommandGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2021 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; | ||
|
||
import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobStreamingMembers; | ||
import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeStreamingMemberType; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import software.amazon.smithy.codegen.core.Symbol; | ||
import software.amazon.smithy.codegen.core.SymbolProvider; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.knowledge.OperationIndex; | ||
import software.amazon.smithy.model.shapes.MemberShape; | ||
import software.amazon.smithy.model.shapes.OperationShape; | ||
import software.amazon.smithy.model.shapes.StructureShape; | ||
|
||
/** | ||
* Generates server operation types. | ||
*/ | ||
final class ServerCommandGenerator implements Runnable { | ||
|
||
private final TypeScriptSettings settings; | ||
private final Model model; | ||
private final OperationShape operation; | ||
private final SymbolProvider symbolProvider; | ||
private final TypeScriptWriter writer; | ||
private final OperationIndex operationIndex; | ||
private final Symbol inputType; | ||
private final Symbol outputType; | ||
|
||
ServerCommandGenerator( | ||
TypeScriptSettings settings, | ||
Model model, | ||
OperationShape operation, | ||
SymbolProvider symbolProvider, | ||
TypeScriptWriter writer | ||
) { | ||
this.settings = settings; | ||
this.model = model; | ||
this.operation = operation; | ||
this.symbolProvider = symbolProvider; | ||
this.writer = writer; | ||
|
||
Symbol operationSymbol = symbolProvider.toSymbol(operation); | ||
operationIndex = OperationIndex.of(model); | ||
inputType = operationSymbol.expectProperty("inputType", Symbol.class); | ||
outputType = operationSymbol.expectProperty("outputType", Symbol.class); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
addInputAndOutputTypes(); | ||
} | ||
|
||
private void addInputAndOutputTypes() { | ||
writeInputType(inputType.getName(), operationIndex.getInput(operation)); | ||
writeOutputType(outputType.getName(), operationIndex.getOutput(operation)); | ||
writer.write(""); | ||
} | ||
|
||
// TODO: Flip these so that metadata is attached to input and streaming customization is attached to output. | ||
private void writeInputType(String typeName, Optional<StructureShape> inputShape) { | ||
if (inputShape.isPresent()) { | ||
StructureShape input = inputShape.get(); | ||
List<MemberShape> blobStreamingMembers = getBlobStreamingMembers(model, input); | ||
if (blobStreamingMembers.isEmpty()) { | ||
writer.write("export type $L = $T;", typeName, symbolProvider.toSymbol(input)); | ||
} else { | ||
writeStreamingMemberType(writer, symbolProvider.toSymbol(input), typeName, blobStreamingMembers.get(0)); | ||
} | ||
} else { | ||
// If the input is non-existent, then use an empty object. | ||
writer.write("export type $L = {}", typeName); | ||
} | ||
} | ||
|
||
private void writeOutputType(String typeName, Optional<StructureShape> outputShape) { | ||
// Output types should always be MetadataBearers, possibly in addition | ||
// to a defined output shape. | ||
writer.addImport("MetadataBearer", "__MetadataBearer", TypeScriptDependency.AWS_SDK_TYPES.packageName); | ||
if (outputShape.isPresent()) { | ||
writer.write("export type $L = $T & __MetadataBearer;", | ||
typeName, symbolProvider.toSymbol(outputShape.get())); | ||
} else { | ||
writer.write("export type $L = __MetadataBearer", typeName); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the association of the term command with this at all (but no, I don't have a better idea off the top of my head)