Skip to content

Commit

Permalink
fix(Go): fix Nativewrapper on positional trait (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishav-karanjit authored Dec 23, 2024
1 parent f67c210 commit bf06e19
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package software.amazon.polymorph.smithygo.localservice;

import static software.amazon.polymorph.smithygo.codegen.SymbolUtils.POINTABLE;
import static software.amazon.polymorph.smithygo.utils.Constants.DAFNY_RUNTIME_GO_LIBRARY_MODULE;

import software.amazon.polymorph.smithygo.codegen.GenerationContext;
Expand Down Expand Up @@ -1125,13 +1126,14 @@ void generateNativeResourceWrapper(
"dafny"
);
} else {
String fromDafnyConvMethodNameForOutput =
String toDafnyConvMethodNameForOutput =
SmithyNameResolver.getToDafnyMethodName(
service,
outputShape,
""
);
boolean deReferenceRequired = true;
boolean referenceType = false;
if (outputShape.hasTrait(PositionalTrait.class)) {
final MemberShape postionalMemShape = outputShape
.getAllMembers()
Expand All @@ -1147,26 +1149,34 @@ void generateNativeResourceWrapper(
.expectTrait(ReferenceTrait.class)
.getReferentId()
);
// If shape is pointer type, we need to fetch its address
// because conversion function will have pointer as input
referenceType =
context
.symbolProvider()
.toSymbol(outputShape)
.getProperty(POINTABLE, Boolean.class)
.orElse(false);
}
fromDafnyConvMethodNameForOutput =
toDafnyConvMethodNameForOutput =
outputShape.isResourceShape()
? SmithyNameResolver.getFromDafnyMethodName(
? SmithyNameResolver.getToDafnyMethodName(
service,
outputShape,
""
)
: Constants.funcNameGenerator(
postionalMemShape,
"FromDafny",
"ToDafny",
model
);
deReferenceRequired = false;
}
clientResponse = "var native_response, native_error";
returnResponse =
"%s(%snative_response)".formatted(
fromDafnyConvMethodNameForOutput,
deReferenceRequired ? "*" : ""
toDafnyConvMethodNameForOutput,
deReferenceRequired ? "*" : (referenceType ? "&" : "")
);
}
writer.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1575,9 +1575,10 @@ private void generateSerializerFunctions(
}
alreadyVisited.add(visitingMemberShape.toShapeId());
String inputType;
final var outputType = ShapeVisitorHelper.isToDafnyShapeOptional(
visitingMemberShape
)
final Boolean isOptional = ShapeVisitorHelper.isToDafnyShapeOptional(
visitingMemberShape
);
var outputType = isOptional
? "Wrappers.Option"
: DafnyNameResolver.getDafnyType(
visitingShape,
Expand All @@ -1589,15 +1590,57 @@ private void generateSerializerFunctions(
visitingShape,
true
);
if (
context
.symbolProvider()
.toSymbol(visitingMemberShape)
.getProperty(POINTABLE, Boolean.class)
.orElse(false)
) {
inputType = "*".concat(inputType);
Boolean isPointable = context
.symbolProvider()
.toSymbol(visitingMemberShape)
.getProperty(POINTABLE, Boolean.class)
.orElse(false);
if (visitingShape.hasTrait(ReferenceTrait.class)) {
final var referenceTrait = visitingShape.expectTrait(
ReferenceTrait.class
);
final var resourceOrService = context
.model()
.expectShape(referenceTrait.getReferentId());
isPointable =
context
.symbolProvider()
.toSymbol(resourceOrService)
.getProperty(POINTABLE, Boolean.class)
.orElse(false);
if (resourceOrService.isServiceShape()) {
if (resourceOrService.hasTrait(ServiceTrait.class)) {
outputType =
isOptional
? "Wrappers.Option"
: DafnyNameResolver.getDafnyInterfaceClient(
resourceOrService.asServiceShape().get(),
resourceOrService.getTrait(ServiceTrait.class).get()
);
inputType =
GoCodegenUtils.getType(
context.symbolProvider().toSymbol(resourceOrService),
resourceOrService,
true
);
} else {
outputType =
isOptional
? "Wrappers.Option"
: DafnyNameResolver.getDafnyInterfaceClient(
resourceOrService
);
inputType =
SmithyNameResolver
.shapeNamespace(resourceOrService)
.concat(".")
.concat(
context.symbolProvider().toSymbol(serviceShape).getName()
);
}
}
}
inputType = isPointable ? "*".concat(inputType) : inputType;
writer.write(
"""
func $L(input $L)($L) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,26 @@ public static String toDafnyShapeVisitorWriter(
final Shape targetShape = context
.model()
.expectShape(memberShape.getTarget());
// Resource shape already goes into a function
if (targetShape.hasTrait(ReferenceTrait.class)) {
return targetShape.accept(
new SmithyToDafnyShapeVisitor(
context,
dataSource,
writer,
isConfigShape,
isOptional,
isPointerType
)
final ReferenceTrait referenceTrait = targetShape.expectTrait(
ReferenceTrait.class
);
final Shape resourceOrService = context
.model()
.expectShape(referenceTrait.getReferentId());
if (resourceOrService.isResourceShape()) {
return targetShape.accept(
new SmithyToDafnyShapeVisitor(
context,
dataSource,
writer,
isConfigShape,
isOptional,
isPointerType
)
);
}
}
final String funcDataSource = "input";
if (
Expand Down

0 comments on commit bf06e19

Please sign in to comment.