Skip to content
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

fix(go): fix imports for aws-sdk shapes #777

Open
wants to merge 5 commits into
base: main-1.x
Choose a base branch
from
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 @@ -19,6 +19,7 @@
import java.util.Set;
import software.amazon.polymorph.smithygo.localservice.nameresolver.DafnyNameResolver;
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
import software.amazon.polymorph.smithygo.utils.GoCodegenUtils;
import software.amazon.polymorph.traits.ReferenceTrait;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.Symbol;
Expand Down Expand Up @@ -156,13 +157,7 @@ public void renderStructure(Runnable runnable, boolean isInputStructure) {
}
} else {
if (SmithyNameResolver.isShapeFromAWSSDK(targetShape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
targetShape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(targetShape, model)
);
GoCodegenUtils.importAwsSDKShape(targetShape, model, writer);
} else if (
!member
.toShapeId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,7 @@ private void renderListShape(
);
if (isExternalShape) {
if (SmithyNameResolver.isShapeFromAWSSDK(currentShape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
currentShape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(currentShape, model)
);
GoCodegenUtils.importAwsSDKShape(currentShape, model, writer);
} else {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSmithyNamespace(
Expand Down Expand Up @@ -616,13 +610,7 @@ private void renderMapShape(
);
if (isExternalShape) {
if (SmithyNameResolver.isShapeFromAWSSDK(currentShape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
currentShape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(currentShape, model)
);
GoCodegenUtils.importAwsSDKShape(currentShape, model, writer);
} else {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSmithyNamespace(
Expand Down Expand Up @@ -691,13 +679,7 @@ private void renderUnionShape(
);
if (isExternalShape) {
if (SmithyNameResolver.isShapeFromAWSSDK(currentShape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
currentShape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(currentShape, model)
);
GoCodegenUtils.importAwsSDKShape(currentShape, model, writer);
} else {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSmithyNamespace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,7 @@ protected String getDefault(final Shape shape) {
@Override
public String blobShape(final BlobShape shape) {
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
// Blob shape is inherently value type
Expand All @@ -310,13 +304,7 @@ public String blobShape(final BlobShape shape) {
@Override
public String structureShape(final StructureShape shape) {
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
if (shape.hasTrait(ReferenceTrait.class)) {
return referenceStructureShape(shape);
Expand Down Expand Up @@ -425,13 +413,7 @@ public String structureShape(final StructureShape shape) {
public String listShape(final ListShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
final StringBuilder typeConversionMethodBuilder = new StringBuilder();
final MemberShape memberShape = shape.getMember();
Expand Down Expand Up @@ -480,13 +462,7 @@ public String listShape(final ListShape shape) {
public String mapShape(final MapShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
final StringBuilder typeConversionMethodBuilder = new StringBuilder();
final MemberShape keyMemberShape = shape.getKey();
Expand Down Expand Up @@ -546,13 +522,7 @@ public String mapShape(final MapShape shape) {
public String booleanShape(final BooleanShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
if (this.isOptional) {
return """
Expand Down Expand Up @@ -580,13 +550,7 @@ public String booleanShape(final BooleanShape shape) {
public String stringShape(final StringShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
if (shape.hasTrait(EnumTrait.class)) {
if (
Expand Down Expand Up @@ -734,13 +698,7 @@ public String stringShape(final StringShape shape) {
public String integerShape(final IntegerShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
if (isOptional) {
return (
Expand All @@ -767,13 +725,7 @@ public String integerShape(final IntegerShape shape) {
public String longShape(final LongShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
if (isOptional) {
return (
Expand Down Expand Up @@ -801,13 +753,7 @@ public String longShape(final LongShape shape) {
public String doubleShape(final DoubleShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}
writer.addUseImports(SmithyGoDependency.MATH);
if (isOptional) {
Expand Down Expand Up @@ -846,13 +792,7 @@ public String doubleShape(final DoubleShape shape) {
public String unionShape(final UnionShape shape) {
writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny");
if (SmithyNameResolver.isShapeFromAWSSDK(shape)) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
SmithyNameResolver.smithyTypesNamespace(shape, context.model())
);
GoCodegenUtils.importAwsSDKShape(shape, context.model(), writer);
}

String nilCheck = "";
Expand Down
Copy link
Member Author

@rishav-karanjit rishav-karanjit Jan 29, 2025

Choose a reason for hiding this comment

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

diff in this file adds a function to import AWS SDK Shape. Rest of the diff is just about calling this function.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import software.amazon.polymorph.smithygo.codegen.SymbolUtils;
import software.amazon.polymorph.smithygo.localservice.nameresolver.DafnyNameResolver;
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
import software.amazon.polymorph.smithypython.awssdk.nameresolver.AwsSdkNameResolver;
import software.amazon.polymorph.traits.PositionalTrait;
import software.amazon.polymorph.traits.ReferenceTrait;
import software.amazon.smithy.aws.traits.ServiceTrait;
Expand Down Expand Up @@ -225,4 +224,30 @@ public static boolean shapeShouldHaveConversionFunction(Shape shape) {
}
return false;
}

public static void importAwsSDKShape(
final Shape shape,
final Model model,
final GoWriter writer
) {
final var typesNamespace = SmithyNameResolver.smithyTypesNamespace(
shape,
model
);
if (typesNamespace.endsWith("types")) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
),
"types",
typesNamespace
);
} else {
writer.addImport(
SmithyNameResolver.getGoModuleNameForSdkNamespace(
shape.getId().getNamespace()
)
);
}
}
}
Loading