Skip to content

Commit

Permalink
Change dafny client to native ones in native go land
Browse files Browse the repository at this point in the history
  • Loading branch information
rishav-karanjit committed Nov 4, 2024
1 parent 27a8eea commit aab2c4f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,7 @@ private String getDefaultShapeName(Shape shape) {
ServiceShape.class
);
if (shape.hasTrait(ServiceTrait.class)) {
return DafnyNameResolver.getDafnyClientName(
shape.expectTrait(ServiceTrait.class).getSdkId()
);
return SmithyNameResolver.getAwsServiceClientName();
}
return StringUtils.capitalize(
removeLeadingInvalidIdentCharacters(shape.getId().getName(serviceShape))
Expand Down Expand Up @@ -393,7 +391,7 @@ public Symbol mapShape(MapShape shape) {
private Symbol.Builder symbolBuilderFor(Shape shape, String typeName) {
final String namespace;
if (shape.hasTrait(ServiceTrait.class)) {
namespace = DafnyNameResolver.dafnyTypesNamespace(shape);
namespace = SmithyNameResolver.smithyTypesNamespaceAws(shape.expectTrait(ServiceTrait.class), false);
} else {
namespace = SmithyNameResolver.smithyTypesNamespace(shape);
}
Expand Down Expand Up @@ -584,11 +582,12 @@ public Symbol structureShape(StructureShape shape) {
);
var isResource = !shape.expectTrait(ReferenceTrait.class).isService();
if (isResource || referredShape.hasTrait(ServiceTrait.class)) {
final var typeName = referredShape.hasTrait(ServiceTrait.class) ? getDefaultShapeName(referredShape) : "I".concat(getDefaultShapeName(referredShape)) ;
builder.putProperty(
"Referred",
symbolBuilderFor(
referredShape,
"I".concat(getDefaultShapeName(referredShape))
typeName
)
.putProperty(SymbolUtils.POINTABLE, false)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SmithyNameResolver {
"bool" //boolean shape
)
);
private static String awsServiceClientName = "Client";

public static Boolean isShapeFromAWSSDK(final Shape shape) {
// Is there a better way to do this?
Expand Down Expand Up @@ -216,6 +217,10 @@ public static String getAwsServiceClient(final ServiceTrait serviceTrait) {
return SmithyNameResolver
.smithyTypesNamespaceAws(serviceTrait, false)
.concat(DOT)
.concat("Client");
.concat(getAwsServiceClientName());
}

public static String getAwsServiceClientName() {
return awsServiceClientName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ protected String referenceStructureShape(final StructureShape shape) {
),
DafnyNameResolver.dafnyTypesNamespace(serviceShape)
);
return "return %1$s.(%2$s)".formatted(
dataSource,
DafnyNameResolver.getDafnyInterfaceClient(
(ServiceShape) serviceShape,
serviceShape.expectTrait(ServiceTrait.class)
)
);
return """
shim, ok := %1$s.(*%2$swrapped.Shim)
if !ok {
panic("Not able to convert client to native")
}
return *shim.Client
""".formatted(dataSource, DafnyNameResolver.dafnyNamespace(
resourceOrService.expectTrait(ServiceTrait.class)
));
} else {
return "return %1$s{%2$s}".formatted(
namespace.concat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
import software.amazon.polymorph.traits.DafnyUtf8BytesTrait;
import software.amazon.polymorph.traits.ReferenceTrait;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.model.shapes.BlobShape;
Expand Down Expand Up @@ -132,8 +133,20 @@ protected String referenceStructureShape(final StructureShape shape) {
}

if (resourceOrService.asServiceShape().isPresent()) {
writer.addImportFromModule(
SmithyNameResolver.getGoModuleNameForSmithyNamespace(
resourceOrService.toShapeId().getNamespace()
),
DafnyNameResolver.dafnyTypesNamespace(resourceOrService)
);
final var shim = "%swrapped.Shim".formatted(
DafnyNameResolver.dafnyNamespace(
resourceOrService.expectTrait(ServiceTrait.class)
)
);
final var clientConversion = "&%s{Client: &%s}".formatted(shim, dataSource);
if (!this.isOptional) {
return dataSource;
return clientConversion;
} else {
final var goCodeBlock =
"""
Expand All @@ -143,7 +156,7 @@ protected String referenceStructureShape(final StructureShape shape) {
}
return Wrappers.Companion_Option_.Create_Some_(%s)
}()""";
return goCodeBlock.formatted(dataSource, dataSource);
return goCodeBlock.formatted(clientConversion, clientConversion);
}
}

Expand Down

0 comments on commit aab2c4f

Please sign in to comment.