-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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
32 changes: 32 additions & 0 deletions
32
smithy-typescript-ssdk-codegen-test-utils/build.gradle.kts
This file contains 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,32 @@ | ||
extra["displayName"] = "Smithy :: Typescript :: SSDK :: Codegen :: Test :: Utils" | ||
extra["moduleName"] = "software.amazon.smithy.typescript.ssdk.codegen.test.utils" | ||
|
||
val smithyVersion: String by project | ||
|
||
buildscript { | ||
val smithyVersion: String by project | ||
|
||
repositories { | ||
mavenLocal() | ||
mavenCentral() | ||
} | ||
dependencies { | ||
"classpath"("software.amazon.smithy:smithy-cli:$smithyVersion") | ||
} | ||
} | ||
|
||
plugins { | ||
val smithyGradleVersion: String by project | ||
|
||
id("software.amazon.smithy").version(smithyGradleVersion) | ||
} | ||
|
||
repositories { | ||
mavenLocal() | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(project(":smithy-typescript-codegen")) | ||
implementation("software.amazon.smithy:smithy-model:$smithyVersion") | ||
} |
20 changes: 20 additions & 0 deletions
20
...src/main/java/software/amazon/smithy/typescript/ssdk/codegen/test/utils/AddProtocols.java
This file contains 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,20 @@ | ||
package software.amazon.smithy.typescript.ssdk.codegen.test.utils; | ||
|
||
import java.util.List; | ||
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; | ||
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; | ||
import software.amazon.smithy.utils.ListUtils; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Adds fake protocols. | ||
*/ | ||
@SmithyInternalApi | ||
public class AddProtocols implements TypeScriptIntegration { | ||
|
||
@Override | ||
public List<ProtocolGenerator> getProtocolGenerators() { | ||
return ListUtils.of(new TestProtocolGenerator()); | ||
} | ||
} | ||
|
155 changes: 155 additions & 0 deletions
155
...java/software/amazon/smithy/typescript/ssdk/codegen/test/utils/TestProtocolGenerator.java
This file contains 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,155 @@ | ||
package software.amazon.smithy.typescript.ssdk.codegen.test.utils; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import software.amazon.smithy.model.knowledge.HttpBinding; | ||
import software.amazon.smithy.model.shapes.OperationShape; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.shapes.StructureShape; | ||
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format; | ||
import software.amazon.smithy.typescript.codegen.TypeScriptDependency; | ||
import software.amazon.smithy.typescript.codegen.TypeScriptWriter; | ||
import software.amazon.smithy.typescript.codegen.integration.HttpBindingProtocolGenerator; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
|
||
/** | ||
* Protocol for SSDK codegen testing. | ||
*/ | ||
@SmithyInternalApi | ||
class TestProtocolGenerator extends HttpBindingProtocolGenerator { | ||
|
||
TestProtocolGenerator() { | ||
super(true); | ||
} | ||
|
||
@Override | ||
public ShapeId getProtocol() { | ||
return ShapeId.from("example.weather#fakeProtocol"); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "fakeProtocol"; | ||
} | ||
|
||
@Override | ||
protected String getDocumentContentType() { | ||
return "application/json"; | ||
} | ||
|
||
@Override | ||
public Format getDocumentTimestampFormat() { | ||
return Format.EPOCH_SECONDS; | ||
} | ||
|
||
@Override | ||
public boolean requiresNumericEpochSecondsInPayload() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean enableSerdeElision() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void deserializeErrorDocumentBody( | ||
GenerationContext context, | ||
StructureShape error, | ||
List<HttpBinding> documentBindings | ||
) {} | ||
|
||
@Override | ||
public void serializeErrorDocumentBody( | ||
GenerationContext context, | ||
StructureShape error, | ||
List<HttpBinding> documentBindings | ||
) {} | ||
|
||
@Override | ||
public void deserializeInputDocumentBody( | ||
GenerationContext context, | ||
OperationShape operation, | ||
List<HttpBinding> documentBindings | ||
) {} | ||
|
||
@Override | ||
public void serializeInputDocumentBody( | ||
GenerationContext context, | ||
OperationShape operation, | ||
List<HttpBinding> documentBindings | ||
) {} | ||
|
||
@Override | ||
public void deserializeOutputDocumentBody( | ||
GenerationContext context, | ||
OperationShape error, | ||
List<HttpBinding> documentBindings | ||
) {} | ||
|
||
@Override | ||
public void serializeOutputDocumentBody( | ||
GenerationContext context, | ||
OperationShape error, | ||
List<HttpBinding> documentBindings | ||
) {} | ||
|
||
@Override | ||
public void serializeInputEventDocumentPayload(GenerationContext context) {} | ||
|
||
@Override | ||
public void generateDocumentBodyShapeSerializers(GenerationContext context, Set<Shape> shapes) {} | ||
|
||
@Override | ||
public void generateDocumentBodyShapeDeserializers(GenerationContext context, Set<Shape> shapes) {} | ||
|
||
@Override | ||
public void writeErrorCodeParser(GenerationContext context) { | ||
TypeScriptWriter writer = context.getWriter(); | ||
writer.write("const errorCode = parseErrorCode(output, parsedOutput.body);"); | ||
} | ||
|
||
@Override | ||
public void generateProtocolTests(GenerationContext context) {} | ||
|
||
@Override | ||
public void generateSharedComponents(GenerationContext context) { | ||
super.generateSharedComponents(context); | ||
|
||
TypeScriptWriter writer = context.getWriter(); | ||
|
||
// Include a JSON body parser used to deserialize documents from HTTP responses. | ||
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES); | ||
writer.openBlock("const parseBody = (streamBody: any, context: __SerdeContext): " | ||
+ "any => collectBodyString(streamBody, context).then(encoded => {", "});", () -> { | ||
writer.openBlock("if (encoded.length) {", "}", () -> { | ||
writer.write("return JSON.parse(encoded);"); | ||
}); | ||
writer.write("return {};"); | ||
}); | ||
writer.write(""); | ||
|
||
// Include a JSON body parser. | ||
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES); | ||
writer.openBlock("const parseErrorBody = async (errorBody: any, context: __SerdeContext) => {", | ||
"}", () -> { | ||
writer.write("const value = await parseBody(errorBody, context);"); | ||
writer.write("value.message = value.message ?? value.Message;"); | ||
writer.write("return value;"); | ||
}); | ||
writer.write(""); | ||
|
||
// Include an error code parser. | ||
writer.openBlock("const parseErrorCode = (output: __HttpResponse, data: any): string | undefined => {", | ||
"}", () -> { | ||
writer.openBlock("if (output.headers[\"x-error\"]) {", "}", () -> { | ||
writer.write("return output.headers[\"x-error\"];"); | ||
}); | ||
writer.openBlock("if (data.code !== undefined) {", "}", () -> { | ||
writer.write("return data.code;"); | ||
}); | ||
}); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration
This file contains 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 @@ | ||
software.amazon.smithy.typescript.ssdk.codegen.test.utils.AddProtocols |