diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceGenerator.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceGenerator.java index ab4498aff..c8f42f114 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceGenerator.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceGenerator.java @@ -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; @@ -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() @@ -1147,17 +1149,25 @@ 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; @@ -1165,8 +1175,8 @@ void generateNativeResourceWrapper( clientResponse = "var native_response, native_error"; returnResponse = "%s(%snative_response)".formatted( - fromDafnyConvMethodNameForOutput, - deReferenceRequired ? "*" : "" + toDafnyConvMethodNameForOutput, + deReferenceRequired ? "*" : (referenceType ? "&" : "") ); } writer.write( diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java index af0894160..fb7d5e503 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java @@ -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, @@ -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) { diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java index a5426a070..80ad93bdc 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java @@ -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 (