Skip to content

Commit 70832e6

Browse files
authored
Update smithy-client import strategy (#135)
This commit updates the SymbolVisitor's import strategy to use only specific imports instead of a full package aliased import. It also updates other instances that relied on the full import to specify their own directly. The import type for many has been updated to a USE style import to support composed symbols for collection and map shapes.
1 parent f0bb068 commit 70832e6

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructureGenerator.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void run() {
6868
* <p>The following TypeScript is rendered:
6969
*
7070
* <pre>{@code
71-
* import * as _smithy from "@aws-sdk/smithy-client";
71+
* import { isa as __isa } from "@aws-sdk/smithy-client";
7272
*
7373
* export interface Person {
7474
* __type?: "Person";
@@ -79,7 +79,7 @@ public void run() {
7979
* export namespace Person {
8080
* export const ID = "smithy.example#Person";
8181
* export function isa(o: any): o is Person {
82-
* return _smithy.isa(o, ID);
82+
* return __isa(o, ID);
8383
* }
8484
* }
8585
* }</pre>
@@ -128,17 +128,20 @@ private void renderNonErrorStructure() {
128128
* <p>The following TypeScript is generated:
129129
*
130130
* <pre>{@code
131-
* import * as _smithy from "@aws-sdk/smithy-client";
131+
* import {
132+
* SmithyException as __SmithyException,
133+
* isa as __isa
134+
* } from "@aws-sdk/smithy-client";
132135
*
133-
* export interface NoSuchResource extends _smithy.SmithyException, $MetadataBearer {
136+
* export interface NoSuchResource extends __SmithyException, $MetadataBearer {
134137
* name: "NoSuchResource";
135138
* $fault: "client";
136139
* resourceType: string | undefined;
137140
* }
138141
*
139142
* export namespace Person {
140143
* export function isa(o: any): o is NoSuchResource {
141-
* return _smithy.isa(o, "NoSuchResource");
144+
* return __isa(o, "NoSuchResource");
142145
* }
143146
* }
144147
* }</pre>
@@ -149,8 +152,9 @@ private void renderErrorStructure() {
149152
writer.writeShapeDocs(shape);
150153

151154
// Find symbol references with the "extends" property, and add SmithyException.
155+
writer.addImport("SmithyException", "__SmithyException", "@aws-sdk/smithy-client");
152156
String extendsFrom = Stream.concat(
153-
Stream.of("_smithy.SmithyException"),
157+
Stream.of("__SmithyException"),
154158
symbol.getReferences().stream()
155159
.filter(ref -> ref.getProperty(SymbolVisitor.IMPLEMENTS_INTERFACE_PROPERTY).isPresent())
156160
.map(SymbolReference::getAlias)
@@ -168,10 +172,11 @@ private void renderErrorStructure() {
168172
}
169173

170174
private void renderStructureNamespace() {
175+
writer.addImport("isa", "__isa", "@aws-sdk/smithy-client");
171176
Symbol symbol = symbolProvider.toSymbol(shape);
172177
writer.openBlock("export namespace $L {", "}", symbol.getName(), () -> {
173178
writer.openBlock("export function isa(o: any): o is $L {", "}", symbol.getName(), () -> {
174-
writer.write("return _smithy.isa(o, $S);", shape.getId().getName());
179+
writer.write("return __isa(o, $S);", shape.getId().getName());
175180
});
176181
});
177182
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SymbolVisitor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ private Symbol createBigJsSymbol(Shape shape) {
219219

220220
@Override
221221
public Symbol documentShape(DocumentShape shape) {
222-
return addSmithyImport(createSymbolBuilder(shape, "_smithy.DocumentType.Value")).build();
222+
Symbol.Builder builder = createSymbolBuilder(shape, "__DocumentType.Value");
223+
return addSmithyUseImport(builder, "DocumentType", "__DocumentType").build();
223224
}
224225

225226
@Override
@@ -247,7 +248,8 @@ public Symbol stringShape(StringShape shape) {
247248
if (mediaTypeTrait.isPresent()) {
248249
String mediaType = mediaTypeTrait.get().getValue();
249250
if (CodegenUtils.isJsonMediaType(mediaType)) {
250-
return addSmithyImport(createSymbolBuilder(shape, "_smithy.LazyJsonString | string")).build();
251+
Symbol.Builder builder = createSymbolBuilder(shape, "__LazyJsonString | string");
252+
return addSmithyUseImport(builder, "LazyJsonString", "__LazyJsonString").build();
251253
} else {
252254
LOGGER.warning(() -> "Found unsupported mediatype " + mediaType + " on String shape: " + shape);
253255
}
@@ -277,7 +279,6 @@ public Symbol serviceShape(ServiceShape shape) {
277279
@Override
278280
public Symbol structureShape(StructureShape shape) {
279281
Symbol.Builder builder = createObjectSymbolBuilder(shape);
280-
addSmithyImport(builder);
281282

282283
if (outputShapes.contains(shape)) {
283284
SymbolReference reference = SymbolReference.builder()
@@ -293,15 +294,15 @@ public Symbol structureShape(StructureShape shape) {
293294
return builder.build();
294295
}
295296

296-
private Symbol.Builder addSmithyImport(Symbol.Builder builder) {
297+
private Symbol.Builder addSmithyUseImport(Symbol.Builder builder, String name, String as) {
297298
Symbol importSymbol = Symbol.builder()
298-
.name("*")
299+
.name(name)
299300
.namespace("@aws-sdk/smithy-client", "/")
300301
.build();
301302
SymbolReference reference = SymbolReference.builder()
302303
.symbol(importSymbol)
303-
.alias("_smithy")
304-
.options(SymbolReference.ContextOption.DECLARE)
304+
.alias(as)
305+
.options(SymbolReference.ContextOption.USE)
305306
.build();
306307
return builder.addReference(reference);
307308
}

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class StructureGeneratorTest {
1717
@Test
1818
public void properlyGeneratesEmptyMessageMemberOfException() {
1919
testErrorStructureCodegen("error-test-empty.smithy",
20-
"export interface Err extends _smithy.SmithyException, $MetadataBearer {\n"
20+
"export interface Err extends __SmithyException, $MetadataBearer {\n"
2121
+ " name: \"Err\";\n"
2222
+ " $fault: \"client\";\n"
2323
+ "}");
@@ -26,7 +26,7 @@ public void properlyGeneratesEmptyMessageMemberOfException() {
2626
@Test
2727
public void properlyGeneratesOptionalMessageMemberOfException() {
2828
testErrorStructureCodegen("error-test-optional-message.smithy",
29-
"export interface Err extends _smithy.SmithyException, $MetadataBearer {\n"
29+
"export interface Err extends __SmithyException, $MetadataBearer {\n"
3030
+ " name: \"Err\";\n"
3131
+ " $fault: \"client\";\n"
3232
+ " message?: string;\n"
@@ -36,7 +36,7 @@ public void properlyGeneratesOptionalMessageMemberOfException() {
3636
@Test
3737
public void properlyGeneratesRequiredMessageMemberOfException() {
3838
testErrorStructureCodegen("error-test-required-message.smithy",
39-
"export interface Err extends _smithy.SmithyException, $MetadataBearer {\n"
39+
"export interface Err extends __SmithyException, $MetadataBearer {\n"
4040
+ " name: \"Err\";\n"
4141
+ " $fault: \"client\";\n"
4242
+ " message: string | undefined;\n"
@@ -62,10 +62,12 @@ public void testErrorStructureCodegen(String file, String expectedType) {
6262
new TypeScriptCodegenPlugin().execute(context);
6363
String contents = manifest.getFileString("/models/index.ts").get();
6464

65+
assertThat(contents, containsString("as __isa"));
66+
assertThat(contents, containsString("as __SmithyException"));
6567
assertThat(contents, containsString(expectedType));
6668
assertThat(contents, containsString("namespace Err {\n"
6769
+ " export function isa(o: any): o is Err {\n"
68-
+ " return _smithy.isa(o, \"Err\");\n"
70+
+ " return __isa(o, \"Err\");\n"
6971
+ " }\n"
7072
+ "}"));
7173
}
@@ -81,13 +83,14 @@ public void generatesNonErrorStructures() {
8183
new StructureGenerator(model, TypeScriptCodegenPlugin.createSymbolProvider(model), writer, struct).run();
8284
String output = writer.toString();
8385

86+
assertThat(output, containsString("as __isa"));
8487
assertThat(output, containsString("export interface Bar {"));
8588
assertThat(output, containsString("__type?: \"Bar\";"));
8689
assertThat(output, containsString("foo?: string;"));
8790
assertThat(output, containsString("export namespace Bar {"));
8891
assertThat(output, containsString(
8992
"export function isa(o: any): o is Bar {\n"
90-
+ " return _smithy.isa(o, \"Bar\");\n"
93+
+ " return __isa(o, \"Bar\");\n"
9194
+ " }"));
9295
}
9396

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,6 @@ public void usesLazyJsonStringForJsonMediaType() {
163163
SymbolProvider provider = TypeScriptCodegenPlugin.createSymbolProvider(model);
164164
Symbol memberSymbol = provider.toSymbol(member);
165165

166-
assertThat(memberSymbol.getName(), equalTo("_smithy.LazyJsonString | string"));
166+
assertThat(memberSymbol.getName(), equalTo("__LazyJsonString | string"));
167167
}
168168
}

0 commit comments

Comments
 (0)