From edf861c112b4d5c94d6cfe1ec859a1dde67c535c Mon Sep 17 00:00:00 2001 From: Rishav karanjit Date: Tue, 28 Jan 2025 16:29:02 -0800 Subject: [PATCH] fix(go): AWS SDK shape namespace (#769) --- .../smithygo/codegen/StructureGenerator.java | 7 +- .../smithygo/codegen/SymbolVisitor.java | 21 +-- .../smithygo/codegen/UnionGenerator.java | 5 +- .../smithygo/codegen/ValidationGenerator.java | 30 +-- .../DafnyLocalServiceGenerator.java | 82 ++++---- ...fnyLocalServiceTypeConversionProtocol.java | 175 +++++++++++++----- .../nameresolver/SmithyNameResolver.java | 34 +++- .../DafnyToSmithyShapeVisitor.java | 41 ++-- .../SmithyToDafnyShapeVisitor.java | 4 +- .../smithygo/utils/GoCodegenUtils.java | 12 +- 10 files changed, 276 insertions(+), 135 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/StructureGenerator.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/StructureGenerator.java index b507764b9..c0386f975 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/StructureGenerator.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/StructureGenerator.java @@ -115,7 +115,10 @@ public void renderStructure(Runnable runnable, boolean isInputStructure) { .getProperty(SymbolUtils.INPUT_VARIANT, Symbol.class) .orElse(memberSymbol); } - var namespace = SmithyNameResolver.smithyTypesNamespace(targetShape); + var namespace = SmithyNameResolver.smithyTypesNamespace( + targetShape, + model + ); if (targetShape.hasTrait(ReferenceTrait.class)) { memberSymbol = @@ -158,7 +161,7 @@ public void renderStructure(Runnable runnable, boolean isInputStructure) { targetShape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(targetShape) + SmithyNameResolver.smithyTypesNamespace(targetShape, model) ); } else if ( !member diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/SymbolVisitor.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/SymbolVisitor.java index 68e5b0825..35b12fbfa 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/SymbolVisitor.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/SymbolVisitor.java @@ -400,11 +400,10 @@ public Symbol mapShape(MapShape shape) { private Symbol.Builder symbolBuilderFor(Shape shape, String typeName) { final String namespace; - if (shape.hasTrait(ServiceTrait.class)) { - namespace = - shape.expectTrait(ServiceTrait.class).getSdkId().toLowerCase(); + if (SmithyNameResolver.isShapeFromAWSSDK(shape)) { + namespace = SmithyNameResolver.smithyTypesNamespace(shape, model); } else { - namespace = SmithyNameResolver.smithyTypesNamespace(shape); + namespace = SmithyNameResolver.smithyTypesNamespace(shape, model); } if (pointableIndex.isPointable(shape)) { return SymbolUtils.createPointableSymbolBuilder( @@ -541,11 +540,11 @@ public Symbol stringShape(StringShape shape) { return symbolBuilderFor( shape, name, - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, model) ) .definitionFile( "./%s/enums.go".formatted( - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, model) ) ) .build(); @@ -576,13 +575,13 @@ public Symbol structureShape(StructureShape shape) { if (shape.hasTrait(ErrorTrait.ID)) { builder.definitionFile( "./%s/errors.go".formatted( - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, model) ) ); } else { builder.definitionFile( "./%s/types.go".formatted( - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, model) ) ); } @@ -646,7 +645,7 @@ public Symbol unionShape(UnionShape shape) { return symbolBuilderFor( shape, name, - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, model) ) .definitionFile("./types/types.go") .build(); @@ -672,11 +671,11 @@ public Symbol intEnumShape(IntEnumShape shape) { return symbolBuilderFor( shape, name, - SmithyNameResolver.smithyTypesNamespace(settings.getService(model)) + SmithyNameResolver.smithyTypesNamespace(settings.getService(model), model) ) .definitionFile( "./%s/enums.go".formatted( - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, model) ) ) .build(); diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/UnionGenerator.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/UnionGenerator.java index 328743d0b..38ee96f7f 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/UnionGenerator.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/UnionGenerator.java @@ -89,7 +89,10 @@ public void generateUnion(GoWriter writer) { writer.write("Value $T", memberSymbol); } else { // Handling smithy-dafny Reference Trait begins - var namespace = SmithyNameResolver.smithyTypesNamespace(target); + var namespace = SmithyNameResolver.smithyTypesNamespace( + target, + model + ); var newMemberSymbol = memberSymbol; if (target.hasTrait(ReferenceTrait.class)) { newMemberSymbol = diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/ValidationGenerator.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/ValidationGenerator.java index b09f53964..bc9eb6839 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/ValidationGenerator.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/ValidationGenerator.java @@ -514,7 +514,8 @@ private void renderListShape( ); final var inputType = GoCodegenUtils.getType( symbolProvider.toSymbol(currentShape), - isExternalShape + isExternalShape, + context.model() ); if (isExternalShape) { if (SmithyNameResolver.isShapeFromAWSSDK(currentShape)) { @@ -523,14 +524,14 @@ private void renderListShape( currentShape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(currentShape) + SmithyNameResolver.smithyTypesNamespace(currentShape, model) ); } else { writer.addImportFromModule( SmithyNameResolver.getGoModuleNameForSmithyNamespace( currentShape.getId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(currentShape) + SmithyNameResolver.smithyTypesNamespace(currentShape, model) ); } } @@ -610,7 +611,8 @@ private void renderMapShape( ); final var inputType = GoCodegenUtils.getType( symbolProvider.toSymbol(currentShape), - isExternalShape + isExternalShape, + context.model() ); if (isExternalShape) { if (SmithyNameResolver.isShapeFromAWSSDK(currentShape)) { @@ -619,14 +621,14 @@ private void renderMapShape( currentShape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(currentShape) + SmithyNameResolver.smithyTypesNamespace(currentShape, model) ); } else { writer.addImportFromModule( SmithyNameResolver.getGoModuleNameForSmithyNamespace( currentShape.getId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(currentShape) + SmithyNameResolver.smithyTypesNamespace(currentShape, model) ); } } @@ -671,10 +673,12 @@ private void renderUnionShape( var dataSourceForUnion = dataSource; final var currServiceShapeNamespace = SmithyNameResolver.smithyTypesNamespace( - context.settings().getService(model) + context.settings().getService(model), + model ); final var currShapeNamespace = SmithyNameResolver.smithyTypesNamespace( - model.expectShape(memberShape.getTarget()) + model.expectShape(memberShape.getTarget()), + model ); if (!funcInput.isEmpty()) { final Boolean isExternalShape = @@ -682,7 +686,8 @@ private void renderUnionShape( !currShapeNamespace.startsWith("smithy"); final var inputType = GoCodegenUtils.getType( symbolProvider.toSymbol(currentShape), - isExternalShape + isExternalShape, + context.model() ); if (isExternalShape) { if (SmithyNameResolver.isShapeFromAWSSDK(currentShape)) { @@ -691,14 +696,14 @@ private void renderUnionShape( currentShape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(currentShape) + SmithyNameResolver.smithyTypesNamespace(currentShape, model) ); } else { writer.addImportFromModule( SmithyNameResolver.getGoModuleNameForSmithyNamespace( currentShape.getId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(currentShape) + SmithyNameResolver.smithyTypesNamespace(currentShape, model) ); } } @@ -721,7 +726,8 @@ private void renderUnionShape( ); for (final var memberInUnion : currentShape.getAllMembers().values()) { final var currMemberNamespace = SmithyNameResolver.smithyTypesNamespace( - currentShape + currentShape, + model ); final Boolean isExternalShape = !currServiceShapeNamespace.equals(currMemberNamespace) && 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 d087718ff..09516c0d0 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 @@ -81,8 +81,10 @@ void generateClient(GoWriter writer) { ); writerDelegator.useFileWriter( - "%s/types.go".formatted(SmithyNameResolver.smithyTypesNamespace(service)), - SmithyNameResolver.smithyTypesNamespace(service), + "%s/types.go".formatted( + SmithyNameResolver.smithyTypesNamespace(service, model) + ), + SmithyNameResolver.smithyTypesNamespace(service, model), writer1 -> { model .getUnionShapes() @@ -117,7 +119,7 @@ void generateClient(GoWriter writer) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( context.settings().getService().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(service) + SmithyNameResolver.smithyTypesNamespace(service, model) ); writer.addUseImports(SmithyGoDependency.CONTEXT); var configShape = configSymbol.expectProperty( @@ -134,7 +136,7 @@ void generateClient(GoWriter writer) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( configShape.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(configShape) + SmithyNameResolver.smithyTypesNamespace(configShape, model) ); writer.addImportFromModule( SmithyNameResolver.getGoModuleNameForSmithyNamespace( @@ -163,7 +165,7 @@ func NewClient(clientConfig $L) (*$T, error) { """, serviceSymbol, dafnyClient, - SmithyNameResolver.getSmithyType(configShape, configSymbol), + SmithyNameResolver.getSmithyType(configShape, configSymbol, model), serviceSymbol, SmithyNameResolver.getToDafnyMethodName( service, @@ -207,7 +209,7 @@ func NewClient(clientConfig $L) (*$T, error) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( outputShape.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(outputShape) + SmithyNameResolver.smithyTypesNamespace(outputShape, model) ); if ( !outputShape @@ -225,13 +227,13 @@ func NewClient(clientConfig $L) (*$T, error) { var inputType = inputShape.hasTrait(UnitTypeTrait.class) ? "" : ", params %s.%s".formatted( - SmithyNameResolver.smithyTypesNamespace(inputShape), + SmithyNameResolver.smithyTypesNamespace(inputShape, model), inputShape.toShapeId().getName() ); var outputType = outputShape.hasTrait(UnitTypeTrait.class) ? "" : "*%s.%s,".formatted( - SmithyNameResolver.smithyTypesNamespace(outputShape), + SmithyNameResolver.smithyTypesNamespace(outputShape, model), outputShape.toShapeId().getName() ); String baseClientCall; @@ -262,7 +264,8 @@ func NewClient(clientConfig $L) (*$T, error) { ", params %s".formatted( SmithyNameResolver.getSmithyType( inputShape, - symbolProvider.toSymbol(inputShape) + symbolProvider.toSymbol(inputShape), + model ) ); } else { @@ -319,7 +322,8 @@ func NewClient(clientConfig $L) (*$T, error) { SmithyNameResolver .getSmithyType( outputShape, - symbolProvider.toSymbol(outputShape) + symbolProvider.toSymbol(outputShape), + model ) .concat(","); GoCodegenUtils.importNamespace(outputShape, writer); @@ -340,7 +344,8 @@ func NewClient(clientConfig $L) (*$T, error) { return defaultVal,""".formatted( SmithyNameResolver.getSmithyType( outputShape, - symbolProvider.toSymbol(outputShape) + symbolProvider.toSymbol(outputShape), + model ) ); } else { @@ -385,7 +390,7 @@ func NewClient(clientConfig $L) (*$T, error) { %s opaqueErr } """.formatted( - SmithyNameResolver.smithyTypesNamespace(inputShape), + SmithyNameResolver.smithyTypesNamespace(inputShape, model), returnError ); } @@ -434,7 +439,7 @@ void generateShim() { SmithyNameResolver.getGoModuleNameForSmithyNamespace( context.settings().getService().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(service) + SmithyNameResolver.smithyTypesNamespace(service, model) ); writer.addUseImports(SmithyGoDependency.CONTEXT); writer.addImportFromModule( @@ -658,10 +663,10 @@ void shimErrors(GoWriter writer) { """, - SmithyNameResolver.smithyTypesNamespace(error), + SmithyNameResolver.smithyTypesNamespace(error, model), symbolProvider.toSymbol(error).getName(), SmithyNameResolver.getToDafnyMethodName(service, error, ""), - SmithyNameResolver.smithyTypesNamespace(error), + SmithyNameResolver.smithyTypesNamespace(error, model), symbolProvider.toSymbol(error).getName() ); } @@ -680,17 +685,27 @@ void resourceErrors(GoWriter writer) { """, - SmithyNameResolver.getSmithyType(error, symbolProvider.toSymbol(error)), + SmithyNameResolver.getSmithyType( + error, + symbolProvider.toSymbol(error), + model + ), SmithyNameResolver.getToDafnyMethodName(service, error, ""), - SmithyNameResolver.getSmithyType(error, symbolProvider.toSymbol(error)) + SmithyNameResolver.getSmithyType( + error, + symbolProvider.toSymbol(error), + model + ) ); } } void generateUnmodelledErrors(GenerationContext context) { writerDelegator.useFileWriter( - "%s/types.go".formatted(SmithyNameResolver.smithyTypesNamespace(service)), - SmithyNameResolver.smithyTypesNamespace(service), + "%s/types.go".formatted( + SmithyNameResolver.smithyTypesNamespace(service, model) + ), + SmithyNameResolver.smithyTypesNamespace(service, model), writer -> { writer.write( """ @@ -707,9 +722,9 @@ void generateUnmodelledErrors(GenerationContext context) { ); writerDelegator.useFileWriter( "%s/unmodelled_errors.go".formatted( - SmithyNameResolver.smithyTypesNamespace(service) + SmithyNameResolver.smithyTypesNamespace(service, model) ), - SmithyNameResolver.smithyTypesNamespace(service), + SmithyNameResolver.smithyTypesNamespace(service, model), writer -> { writer.addUseImports(SmithyGoDependency.FMT); writer.write( @@ -760,9 +775,9 @@ void generateReferencedResources(final GenerationContext context) { } writerDelegator.useFileWriter( "%s/types.go".formatted( - SmithyNameResolver.smithyTypesNamespace(service) + SmithyNameResolver.smithyTypesNamespace(service, model) ), - SmithyNameResolver.smithyTypesNamespace(service), + SmithyNameResolver.smithyTypesNamespace(service, model), writer -> { writer.write( """ @@ -829,7 +844,7 @@ void generateReferencedResources(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( context.settings().getService().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(service) + SmithyNameResolver.smithyTypesNamespace(service, model) ); writer.addImportFromModule( SmithyNameResolver.getGoModuleNameForSmithyNamespace( @@ -870,7 +885,8 @@ void generateReferencedResources(final GenerationContext context) { : "*%s,".formatted( SmithyNameResolver.getSmithyType( outputShape, - symbolProvider.toSymbol(outputShape) + symbolProvider.toSymbol(outputShape), + model ) ); @@ -898,7 +914,8 @@ void generateReferencedResources(final GenerationContext context) { "params %s".formatted( SmithyNameResolver.getSmithyType( inputShape, - symbolProvider.toSymbol(inputShape) + symbolProvider.toSymbol(inputShape), + model ) ); baseClientCall = @@ -958,7 +975,8 @@ void generateReferencedResources(final GenerationContext context) { SmithyNameResolver .getSmithyType( outputShape, - symbolProvider.toSymbol(outputShape) + symbolProvider.toSymbol(outputShape), + model ) .concat(","); final var typeAssertion = outputShape.isResourceShape() @@ -986,7 +1004,8 @@ void generateReferencedResources(final GenerationContext context) { return defaultVal,""".formatted( SmithyNameResolver.getSmithyType( outputShape, - symbolProvider.toSymbol(outputShape) + symbolProvider.toSymbol(outputShape), + model ) ); } else { @@ -1052,7 +1071,7 @@ void generateNativeResourceWrapper( writer -> { writer.addImportFromModule( context.settings().getModuleName(), - SmithyNameResolver.smithyTypesNamespace(service) + SmithyNameResolver.smithyTypesNamespace(service, model) ); writer.addImportFromModule(SMITHY_DAFNY_STD_LIB_GO, "Wrappers"); writer.addImportFromModule( @@ -1072,7 +1091,7 @@ void generateNativeResourceWrapper( resourceShape.getId().getName(), DafnyNameResolver.dafnyTypesNamespace(resourceShape), resourceShape.getId().getName(), - SmithyNameResolver.smithyTypesNamespace(resourceShape), + SmithyNameResolver.smithyTypesNamespace(resourceShape, model), resourceShape.getId().getName() ) ); @@ -1093,7 +1112,8 @@ void generateNativeResourceWrapper( : "*%s,".formatted( SmithyNameResolver.getSmithyType( outputShape, - symbolProvider.toSymbol(outputShape) + symbolProvider.toSymbol(outputShape), + model ) ); String fromDafnyConvMethodNameForInput = 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 3ee065964..91e664f9a 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 @@ -120,7 +120,7 @@ public void generateSerializers(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( input.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(input) + SmithyNameResolver.smithyTypesNamespace(input, model) ); writer.write( """ @@ -128,7 +128,7 @@ public void generateSerializers(final GenerationContext context) { ${C|} }""", inputToDafnyMethodName, - SmithyNameResolver.getSmithyType(input, inputSymbol), + SmithyNameResolver.getSmithyType(input, inputSymbol, model), outputType, writer.consumer(w -> generateRequestSerializer( @@ -179,7 +179,7 @@ public void generateSerializers(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( output.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(output) + SmithyNameResolver.smithyTypesNamespace(output, model) ); writer.write( """ @@ -187,7 +187,11 @@ public void generateSerializers(final GenerationContext context) { ${C|} }""", outputToDafnyMethodName, - SmithyNameResolver.getSmithyType(output, outputSymbol), + SmithyNameResolver.getSmithyType( + output, + outputSymbol, + model + ), DafnyNameResolver.getDafnyType(output, outputSymbol), writer.consumer(w -> generateResponseSerializer( @@ -281,7 +285,7 @@ public void generateSerializers(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( input.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(input) + SmithyNameResolver.smithyTypesNamespace(input, model) ); writer.write( """ @@ -289,7 +293,11 @@ public void generateSerializers(final GenerationContext context) { ${C|} }""", inputToDafnyMethodName, - SmithyNameResolver.getSmithyType(input, inputSymbol), + SmithyNameResolver.getSmithyType( + input, + inputSymbol, + model + ), outputType, writer.consumer(w -> generateRequestSerializer( @@ -344,7 +352,7 @@ public void generateSerializers(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( output.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(output) + SmithyNameResolver.smithyTypesNamespace(output, model) ); writer.write( """ @@ -352,7 +360,11 @@ public void generateSerializers(final GenerationContext context) { ${C|} }""", outputToDafnyMethodName, - SmithyNameResolver.getSmithyType(output, outputSymbol), + SmithyNameResolver.getSmithyType( + output, + outputSymbol, + model + ), DafnyNameResolver.getDafnyType(output, outputSymbol), writer.consumer(w -> generateResponseSerializer( @@ -408,7 +420,7 @@ public void generateSerializers(final GenerationContext context) { } """, resourceShape.getId().getName(), - SmithyNameResolver.smithyTypesNamespace(resourceShape), + SmithyNameResolver.smithyTypesNamespace(resourceShape, model), resourceShape.getId().getName(), DafnyNameResolver.dafnyTypesNamespace(resourceShape), resourceShape.getId().getName(), @@ -484,7 +496,8 @@ public void generateOrphanShapeSerializer( inputType = GoCodegenUtils.getType( context.symbolProvider().toSymbol(resourceOrService), - true + true, + context.model() ); } else { outputType = @@ -496,7 +509,7 @@ public void generateOrphanShapeSerializer( .concat(context.symbolProvider().toSymbol(serviceShape).getName()); } } else { - inputType = GoCodegenUtils.getType(curSymbol, true); + inputType = GoCodegenUtils.getType(curSymbol, true, context.model()); outputType = DafnyNameResolver.getDafnyType(shape, curSymbol); } writerDelegator.useFileWriter( @@ -510,7 +523,7 @@ public void generateOrphanShapeSerializer( SmithyNameResolver.getGoModuleNameForSmithyNamespace( shape.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); writer.write( """ @@ -613,7 +626,10 @@ public void generateDeserializers(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( input.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(input) + SmithyNameResolver.smithyTypesNamespace( + input, + context.model() + ) ); writer.write( @@ -623,7 +639,11 @@ public void generateDeserializers(final GenerationContext context) { }""", inputFromDafnyMethodName, inputType, - SmithyNameResolver.getSmithyType(input, inputSymbol), + SmithyNameResolver.getSmithyType( + input, + inputSymbol, + context.model() + ), writer.consumer(w -> generateRequestDeserializer( context, @@ -678,7 +698,10 @@ public void generateDeserializers(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( output.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(output) + SmithyNameResolver.smithyTypesNamespace( + output, + context.model() + ) ); writer.write( @@ -688,7 +711,11 @@ public void generateDeserializers(final GenerationContext context) { }""", outputFromDafnyMethodName, DafnyNameResolver.getDafnyType(output, outputSymbol), - SmithyNameResolver.getSmithyType(output, outputSymbol), + SmithyNameResolver.getSmithyType( + output, + outputSymbol, + context.model() + ), writer.consumer(w -> generateResponseDeserializer( context, @@ -786,7 +813,10 @@ public void generateDeserializers(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( input.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(input) + SmithyNameResolver.smithyTypesNamespace( + input, + context.model() + ) ); writer.write( @@ -796,7 +826,11 @@ public void generateDeserializers(final GenerationContext context) { }""", inputFromDafnyMethodName, inputType, - SmithyNameResolver.getSmithyType(input, inputSymbol), + SmithyNameResolver.getSmithyType( + input, + inputSymbol, + context.model() + ), writer.consumer(w -> generateRequestDeserializer( context, @@ -853,7 +887,10 @@ public void generateDeserializers(final GenerationContext context) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( output.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(output) + SmithyNameResolver.smithyTypesNamespace( + output, + context.model() + ) ); writer.write( @@ -863,7 +900,11 @@ public void generateDeserializers(final GenerationContext context) { }""", outputFromDafnyMethodName, DafnyNameResolver.getDafnyType(output, outputSymbol), - SmithyNameResolver.getSmithyType(output, outputSymbol), + SmithyNameResolver.getSmithyType( + output, + outputSymbol, + context.model() + ), writer.consumer(w -> generateResponseDeserializer( context, @@ -913,7 +954,10 @@ public void generateDeserializers(final GenerationContext context) { resourceShape.getId().getName(), DafnyNameResolver.dafnyTypesNamespace(resourceShape), resourceShape.getId().getName(), - SmithyNameResolver.smithyTypesNamespace(resourceShape), + SmithyNameResolver.smithyTypesNamespace( + resourceShape, + context.model() + ), resourceShape.getId().getName(), extendableResourceWrapperCheck, resourceShape.getId().getName() @@ -991,7 +1035,11 @@ public void generateOrphanShapeDeserializer( } } else { outputType = - GoCodegenUtils.getType(context.symbolProvider().toSymbol(shape), true); + GoCodegenUtils.getType( + context.symbolProvider().toSymbol(shape), + true, + context.model() + ); } writerDelegator.useFileWriter( "%s/%s".formatted( @@ -1004,7 +1052,7 @@ public void generateOrphanShapeDeserializer( SmithyNameResolver.getGoModuleNameForSmithyNamespace( shape.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); writer.write( """ @@ -1185,7 +1233,8 @@ private void generateConfigSerializer( getInputToDafnyMethodName, SmithyNameResolver.getSmithyType( configShape, - context.symbolProvider().toSymbol(configShape) + context.symbolProvider().toSymbol(configShape), + context.model() ), DafnyNameResolver.getDafnyType( configShape, @@ -1257,7 +1306,8 @@ private void generateErrorSerializer( getInputToDafnyMethodName, SmithyNameResolver.getSmithyType( errorShape, - context.symbolProvider().toSymbol(errorShape) + context.symbolProvider().toSymbol(errorShape), + context.model() ), DafnyNameResolver.getDafnyBaseErrorType(errorShape), writer.consumer(w -> { @@ -1306,10 +1356,16 @@ func CollectionOfErrors_Input_ToDafny(nativeInput $L.CollectionOfErrors)($L.Erro func OpaqueError_Input_ToDafny(nativeInput $L.OpaqueError)($L.Error) { return $L.Companion_Error_.Create_Opaque_(nativeInput.ErrObject) }""", - SmithyNameResolver.smithyTypesNamespace(serviceShape), + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ), DafnyNameResolver.dafnyTypesNamespace(serviceShape), DafnyNameResolver.dafnyTypesNamespace(serviceShape), - SmithyNameResolver.smithyTypesNamespace(serviceShape), + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ), DafnyNameResolver.dafnyTypesNamespace(serviceShape), DafnyNameResolver.dafnyTypesNamespace(serviceShape) ); @@ -1375,7 +1431,8 @@ func Error_ToDafny(err error)($L.Error) { context.model().expectShape(error), context .symbolProvider() - .toSymbol(context.model().expectShape(error)) + .toSymbol(context.model().expectShape(error)), + context.model() ), SmithyNameResolver.getToDafnyMethodName( serviceShape, @@ -1386,7 +1443,8 @@ func Error_ToDafny(err error)($L.Error) { context.model().expectShape(error), context .symbolProvider() - .toSymbol(context.model().expectShape(error)) + .toSymbol(context.model().expectShape(error)), + context.model() ) ); } @@ -1403,9 +1461,18 @@ func Error_ToDafny(err error)($L.Error) { handleDepErrorSerializer(context, w, dependencies); } }), - SmithyNameResolver.smithyTypesNamespace(serviceShape), - SmithyNameResolver.smithyTypesNamespace(serviceShape), - SmithyNameResolver.smithyTypesNamespace(serviceShape) + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ), + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ), + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ) ); } ); @@ -1482,7 +1549,7 @@ private void handleDepErrorSerializer( SmithyNameResolver.getGoModuleNameForSmithyNamespace( depShape.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(depShape) + SmithyNameResolver.smithyTypesNamespace(depShape, context.model()) ); w.addImportFromModule( SmithyNameResolver.getGoModuleNameForSmithyNamespace( @@ -1495,7 +1562,7 @@ private void handleDepErrorSerializer( case $L.$LBaseException: return $L.Create_$L_($L.Error_ToDafny(err)) """, - SmithyNameResolver.smithyTypesNamespace(depShape), + SmithyNameResolver.smithyTypesNamespace(depShape, context.model()), dep.getName(), DafnyNameResolver.getDafnyErrorCompanion(serviceShape), DafnyNameResolver.dafnyDependentErrorName(depShape), @@ -1550,7 +1617,10 @@ private void generateConfigDeserializer( SmithyNameResolver.getGoModuleNameForSmithyNamespace( configShape.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(configShape) + SmithyNameResolver.smithyTypesNamespace( + configShape, + context.model() + ) ); writer.write( """ @@ -1564,7 +1634,8 @@ private void generateConfigDeserializer( ), SmithyNameResolver.getSmithyType( configShape, - context.symbolProvider().toSymbol(configShape) + context.symbolProvider().toSymbol(configShape), + context.model() ), writer.consumer(w -> { final String output = configShape.accept( @@ -1633,7 +1704,8 @@ private void generateErrorDeserializer( DafnyNameResolver.getDafnyBaseErrorType(errorShape), SmithyNameResolver.getSmithyType( errorShape, - context.symbolProvider().toSymbol(errorShape) + context.symbolProvider().toSymbol(errorShape), + context.model() ), writer.consumer(w -> { final String output = errorShape.accept( @@ -1704,12 +1776,24 @@ func OpaqueError_Output_FromDafny(dafnyOutput $L.Error)($L.OpaqueError) { } }""", DafnyNameResolver.dafnyTypesNamespace(serviceShape), - SmithyNameResolver.smithyTypesNamespace(serviceShape), - SmithyNameResolver.smithyTypesNamespace(serviceShape), + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ), + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ), DafnyNameResolver.dafnyTypesNamespace(serviceShape), DafnyNameResolver.dafnyTypesNamespace(serviceShape), - SmithyNameResolver.smithyTypesNamespace(serviceShape), - SmithyNameResolver.smithyTypesNamespace(serviceShape) + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ), + SmithyNameResolver.smithyTypesNamespace( + serviceShape, + context.model() + ) ); } ); @@ -1853,7 +1937,8 @@ private void generateSerializerFunctions( inputType = GoCodegenUtils.getType( context.symbolProvider().toSymbol(visitingShape), - true + true, + context.model() ); Boolean isPointable = context .symbolProvider() @@ -1885,7 +1970,8 @@ private void generateSerializerFunctions( inputType = GoCodegenUtils.getType( context.symbolProvider().toSymbol(resourceOrService), - true + true, + context.model() ); } else { outputType = @@ -1955,7 +2041,8 @@ private void generateDeserializerFunctions( alreadyVisited.add(visitingMemberShape.toShapeId()); var outputType = GoCodegenUtils.getType( context.symbolProvider().toSymbol(visitingShape), - true + true, + context.model() ); Boolean isPointable = context .symbolProvider() diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/nameresolver/SmithyNameResolver.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/nameresolver/SmithyNameResolver.java index 566ada9ce..c704231e5 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/nameresolver/SmithyNameResolver.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/nameresolver/SmithyNameResolver.java @@ -8,6 +8,8 @@ import java.util.Map; import software.amazon.smithy.aws.traits.ServiceTrait; import software.amazon.smithy.codegen.core.Symbol; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.Shape; @@ -61,7 +63,10 @@ public static String shapeNamespace(final Shape shape) { .concat("smithygenerated"); } - public static String smithyTypesNamespace(final Shape shape) { + public static String smithyTypesNamespace( + final Shape shape, + final Model model + ) { final String shapeNameSpace = shape.toShapeId().getNamespace(); if (isShapeFromAWSSDK(shape)) { final String sdkName = shapeNameSpace @@ -70,9 +75,18 @@ public static String smithyTypesNamespace(final Shape shape) { if (shape.hasTrait(ServiceTrait.class)) { return sdkName; } - return sdkName.concat("types"); + // Boolean to hold if shape is input or output of any operation + boolean isTopLevelShape = model + .shapes(OperationShape.class) + .anyMatch(op -> + op.getInput().filter(shape.getId()::equals).isPresent() || + op.getOutput().filter(shape.getId()::equals).isPresent() + ); + return isTopLevelShape ? sdkName : sdkName.concat("types"); } - return shapeNameSpace + return shape + .toShapeId() + .getNamespace() .replace(DOT, BLANK) .toLowerCase() .concat("smithygeneratedtypes"); @@ -94,7 +108,11 @@ public static String smithyTypesNamespaceAws( return serviceTrait.getSdkId().toLowerCase(); } - public static String getSmithyType(final Shape shape, final Symbol symbol) { + public static String getSmithyType( + final Shape shape, + final Symbol symbol, + final Model model + ) { if ( symbol.getNamespace().contains("smithy.") || symbol.getNamespace().equals("smithyapitypes") || @@ -104,7 +122,7 @@ public static String getSmithyType(final Shape shape, final Symbol symbol) { } if (shape.isResourceShape()) { return SmithyNameResolver - .smithyTypesNamespace(shape) + .smithyTypesNamespace(shape, model) .concat(DOT) .concat("I") .concat(shape.toShapeId().getName()); @@ -114,14 +132,14 @@ public static String getSmithyType(final Shape shape, final Symbol symbol) { return "time".concat(DOT).concat(symbol.getName()); } return SmithyNameResolver - .smithyTypesNamespace(shape) + .smithyTypesNamespace(shape, model) .concat(DOT) .concat(symbol.getName()); } - public static String getSmithyType(final Shape shape) { + public static String getSmithyType(final Shape shape, Model model) { return SmithyNameResolver - .smithyTypesNamespace(shape) + .smithyTypesNamespace(shape, model) .concat(DOT) .concat(shape.toShapeId().getName()); } diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/DafnyToSmithyShapeVisitor.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/DafnyToSmithyShapeVisitor.java index 0b9d36725..5e518ba66 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/DafnyToSmithyShapeVisitor.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/DafnyToSmithyShapeVisitor.java @@ -235,7 +235,7 @@ private String referencedResourceShape(final Shape resourceOrService) { SmithyNameResolver.getGoModuleNameForSmithyNamespace( resourceOrService.toShapeId().getNamespace() ), - SmithyNameResolver.smithyTypesNamespace(resourceShape) + SmithyNameResolver.smithyTypesNamespace(resourceShape, context.model()) ); namespace = SmithyNameResolver.shapeNamespace(resourceOrService).concat("."); @@ -253,7 +253,7 @@ private String referencedResourceShape(final Shape resourceOrService) { } return %s }()""".formatted( - SmithyNameResolver.smithyTypesNamespace(resourceShape), + SmithyNameResolver.smithyTypesNamespace(resourceShape, context.model()), resourceShape.getId().getName(), dataSource, "%s_FromDafny(%s.(%s.I%s))".formatted( @@ -285,7 +285,7 @@ public String blobShape(final BlobShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } writer.addImportFromModule(DAFNY_RUNTIME_GO_LIBRARY_MODULE, "dafny"); @@ -315,7 +315,7 @@ public String structureShape(final StructureShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } if (shape.hasTrait(ReferenceTrait.class)) { @@ -350,7 +350,7 @@ public String structureShape(final StructureShape shape) { "return %1$s%2$s{".formatted( referenceType, SmithyNameResolver - .smithyTypesNamespace(shape) + .smithyTypesNamespace(shape, context.model()) .concat(".") .concat(shape.getId().getName()) ) @@ -430,7 +430,7 @@ public String listShape(final ListShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } final StringBuilder typeConversionMethodBuilder = new StringBuilder(); @@ -459,7 +459,7 @@ public String listShape(final ListShape shape) { } fieldValue = append(fieldValue, %s)} """.formatted( - GoCodegenUtils.getType(symbol, true), + GoCodegenUtils.getType(symbol, true, context.model()), dataSource, ShapeVisitorHelper.toNativeShapeVisitorWriter( memberShape, @@ -485,7 +485,7 @@ public String mapShape(final MapShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } final StringBuilder typeConversionMethodBuilder = new StringBuilder(); @@ -496,7 +496,8 @@ public String mapShape(final MapShape shape) { .expectShape(valueMemberShape.getTarget()); final var type = SmithyNameResolver.getSmithyType( valueTargetShape, - context.symbolProvider().toSymbol(valueTargetShape) + context.symbolProvider().toSymbol(valueTargetShape), + context.model() ); final String valueDataSource = "(*val.(dafny.Tuple).IndexInt(1))"; typeConversionMethodBuilder.append( @@ -550,7 +551,7 @@ public String booleanShape(final BooleanShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } if (this.isOptional) { @@ -584,7 +585,7 @@ public String stringShape(final StringShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } if (shape.hasTrait(EnumTrait.class)) { @@ -622,9 +623,9 @@ public String stringShape(final StringShape shape) { return &u.Values()[index] }()""".formatted( - SmithyNameResolver.smithyTypesNamespace(shape), + SmithyNameResolver.smithyTypesNamespace(shape, context.model()), context.symbolProvider().toSymbol(shape).getName(), - SmithyNameResolver.smithyTypesNamespace(shape), + SmithyNameResolver.smithyTypesNamespace(shape, context.model()), context.symbolProvider().toSymbol(shape).getName(), dataSource, dataSource, @@ -659,9 +660,9 @@ public String stringShape(final StringShape shape) { return u.Values()[index] }()""".formatted( - SmithyNameResolver.smithyTypesNamespace(shape), + SmithyNameResolver.smithyTypesNamespace(shape, context.model()), context.symbolProvider().toSymbol(shape).getName(), - SmithyNameResolver.smithyTypesNamespace(shape), + SmithyNameResolver.smithyTypesNamespace(shape, context.model()), context.symbolProvider().toSymbol(shape).getName(), dataSource, DafnyNameResolver.getDafnyType( @@ -738,7 +739,7 @@ public String integerShape(final IntegerShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } if (isOptional) { @@ -771,7 +772,7 @@ public String longShape(final LongShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } if (isOptional) { @@ -805,7 +806,7 @@ public String doubleShape(final DoubleShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } writer.addUseImports(SmithyGoDependency.MATH); @@ -850,7 +851,7 @@ public String unionShape(final UnionShape shape) { shape.getId().getNamespace() ), "types", - SmithyNameResolver.smithyTypesNamespace(shape) + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) ); } @@ -923,7 +924,7 @@ public String unionShape(final UnionShape shape) { """.formatted( isMemberCheck, wrappedDataSource, - SmithyNameResolver.smithyTypesNamespace(shape), + SmithyNameResolver.smithyTypesNamespace(shape, context.model()), memberName, pointerForPointableShape, ShapeVisitorHelper.toNativeShapeVisitorWriter( diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/SmithyToDafnyShapeVisitor.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/SmithyToDafnyShapeVisitor.java index 10bc55e32..ee1702762 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/SmithyToDafnyShapeVisitor.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/SmithyToDafnyShapeVisitor.java @@ -752,14 +752,14 @@ public String unionShape(final UnionShape shape) { var inputToConversion = %s return %s """.formatted( - SmithyNameResolver.smithyTypesNamespace(shape), + SmithyNameResolver.smithyTypesNamespace(shape, context.model()), context.symbolProvider().toMemberName(member), ShapeVisitorHelper.toDafnyShapeVisitorWriter( member, context, dataSource + ".(*" + - SmithyNameResolver.smithyTypesNamespace(shape) + + SmithyNameResolver.smithyTypesNamespace(shape, context.model()) + "." + context.symbolProvider().toMemberName(member) + ").Value", diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/GoCodegenUtils.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/GoCodegenUtils.java index 3d2347077..9dccbb2e1 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/GoCodegenUtils.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/GoCodegenUtils.java @@ -54,7 +54,8 @@ public static String getType( public static String getType( final Symbol symbol, - final Boolean includeNamespace + final Boolean includeNamespace, + final Model model ) { if ( symbol.getProperty(SymbolUtils.GO_ELEMENT_TYPE, Symbol.class).isEmpty() @@ -62,13 +63,15 @@ public static String getType( return includeNamespace ? SmithyNameResolver.getSmithyType( symbol.expectProperty(SymbolUtils.SHAPE, Shape.class), - symbol + symbol, + model ) : symbol.getName(); } var type = getType( symbol.expectProperty(SymbolUtils.GO_ELEMENT_TYPE, Symbol.class), - includeNamespace + includeNamespace, + model ); if (symbol.getProperty(SymbolUtils.GO_MAP).isPresent()) { type = "map[string]" + type; @@ -180,7 +183,8 @@ private static String getInputOrOutputName( return ( SmithyNameResolver.getSmithyType( curShape, - symbolProvider.toSymbol(curShape) + symbolProvider.toSymbol(curShape), + model ) ); } else {