From ee6ff333d686f4762f37e70fe8631696073a33cf Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Wed, 15 Jan 2025 15:51:08 -0800 Subject: [PATCH 01/12] handle unhandled orphanShapes --- .../DafnyLocalServiceDirectedCodegen.java | 4 +- ...fnyLocalServiceTypeConversionProtocol.java | 194 +++++++++++++++++- 2 files changed, 194 insertions(+), 4 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceDirectedCodegen.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceDirectedCodegen.java index e470c92ee..6f038102f 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceDirectedCodegen.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceDirectedCodegen.java @@ -213,7 +213,9 @@ protected void generateOrphanedShapesForService( ShapeType.INTEGER, ShapeType.UNION, ShapeType.STRING, - ShapeType.LONG + ShapeType.LONG, + ShapeType.MAP, + ShapeType.LIST ); for (final var shapeToGenerate : orderedShapes) { 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 fb7d5e503..8aa36009e 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 @@ -410,13 +410,109 @@ public void generateSerializers(final GenerationContext context) { }); } } - generateErrorSerializer(context); + generateErrorSerializer(context, alreadyVisited); if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigSerializer(context); } generateSerializerFunctions(context, alreadyVisited); } + public void generateOrphanShapeSerializer( + final GenerationContext context, + final Shape currentShape, + final Set alreadyVisited, + ServiceShape serviceShape + ) { + final Shape shape; + if (currentShape.hasTrait(ReferenceTrait.class)) { + shape = + context + .model() + .expectShape( + currentShape.expectTrait(ReferenceTrait.class).getReferentId() + ); + } else { + shape = currentShape; + } + if (alreadyVisited.contains(shape.toShapeId())) { + return; + } + // We don't need type conversion for operation shape and resource shape + if (shape.isOperationShape() || shape.isResourceShape()) { + return; + } + if (shape.hasTrait(UnitTypeTrait.class)) { + return; + } + final var inputToDafnyMethodName = SmithyNameResolver.getToDafnyMethodName( + serviceShape, + shape, + "" + ); + final var writerDelegator = context.writerDelegator(); + final String outputType; + final var inputSymbol = context.symbolProvider().toSymbol(shape); + alreadyVisited.add(shape.toShapeId()); + if (shape.hasTrait(PositionalTrait.class)) { + // Output type in To Dafny should be unwrapped + Shape shapeForPositional = context + .model() + .expectShape( + shape.getAllMembers().values().stream().findFirst().get().getTarget() + ); + Symbol symbolForPositional = context + .symbolProvider() + .toSymbol(shapeForPositional); + outputType = + DafnyNameResolver.getDafnyType(shapeForPositional, symbolForPositional); + } else { + outputType = DafnyNameResolver.getDafnyType(shape, inputSymbol); + } + writerDelegator.useFileWriter( + "%s/%s".formatted( + SmithyNameResolver.shapeNamespace(serviceShape), + TO_DAFNY + ), + SmithyNameResolver.shapeNamespace(serviceShape), + writer -> { + writer.addImportFromModule( + SmithyNameResolver.getGoModuleNameForSmithyNamespace( + shape.toShapeId().getNamespace() + ), + SmithyNameResolver.smithyTypesNamespace(shape) + ); + writer.write( + """ + func $L(nativeInput $L)($L) { + ${C|} + }""", + inputToDafnyMethodName, + GoCodegenUtils.getType(inputSymbol, shape, true), + // SmithyNameResolver.getSmithyType(shape, inputSymbol), + outputType, + writer.consumer(w -> { + final String shapeVisitorOutput = shape.accept( + new SmithyToDafnyShapeVisitor( + context, + "nativeInput", + writer, + false, + false, + false + ) + ); + writer.write( + """ + return $L + """, + shapeVisitorOutput + ); + }) + ); + } + ); + } + @Override public void generateDeserializers(final GenerationContext context) { final Set alreadyVisited = new HashSet<>(); @@ -797,6 +893,96 @@ public void generateDeserializers(final GenerationContext context) { generateDeserializerFunctions(context, alreadyVisited); } + public void generateOrphanShapeDeserializer( + final GenerationContext context, + final Shape currentShape, + final Set alreadyVisited, + ServiceShape serviceShape + ) { + final Shape shape; + if (currentShape.hasTrait(ReferenceTrait.class)) { + shape = + context + .model() + .expectShape( + currentShape.expectTrait(ReferenceTrait.class).getReferentId() + ); + } else { + shape = currentShape; + } + if (alreadyVisited.contains(shape.toShapeId())) { + return; + } + if (shape.isOperationShape() || shape.isResourceShape()) { + return; + } + + if (shape.hasTrait(UnitTypeTrait.class)) { + return; + } + final var writerDelegator = context.writerDelegator(); + final var inputFromDafnyMethodName = + SmithyNameResolver.getFromDafnyMethodName(serviceShape, shape, ""); + final var inputSymbol = context.symbolProvider().toSymbol(shape); + final String inputType; + if (shape.hasTrait(PositionalTrait.class)) { + // shape in To native should be unwrapped + Shape inputForPositional = context + .model() + .expectShape( + shape.getAllMembers().values().stream().findFirst().get().getTarget() + ); + Symbol symbolForPositional = context + .symbolProvider() + .toSymbol(inputForPositional); + inputType = + DafnyNameResolver.getDafnyType(inputForPositional, symbolForPositional); + } else { + inputType = DafnyNameResolver.getDafnyType(shape, inputSymbol); + } + writerDelegator.useFileWriter( + "%s/%s".formatted( + SmithyNameResolver.shapeNamespace(serviceShape), + TO_NATIVE + ), + SmithyNameResolver.shapeNamespace(serviceShape), + writer -> { + writer.addImportFromModule( + SmithyNameResolver.getGoModuleNameForSmithyNamespace( + shape.toShapeId().getNamespace() + ), + SmithyNameResolver.smithyTypesNamespace(shape) + ); + + writer.write( + """ + func $L(dafnyInput $L)($L) { + ${C|} + }""", + inputFromDafnyMethodName, + inputType, + SmithyNameResolver.getSmithyType(shape, inputSymbol), + writer.consumer(w -> { + final var shapeVisitorOutput = shape.accept( + new DafnyToSmithyShapeVisitor( + context, + "dafnyInput", + writer, + false + ) + ); + writer.write( + """ + $L + """, + shapeVisitorOutput + ); + }) + ); + } + ); + } + private void generateRequestSerializer( final GenerationContext context, final OperationShape operation, @@ -971,8 +1157,10 @@ private void generateConfigSerializer(final GenerationContext context) { ); } - private void generateErrorSerializer(final GenerationContext context) { - final Set alreadyVisited = new HashSet<>(); + private void generateErrorSerializer( + final GenerationContext context, + final Set alreadyVisited + ) { final var serviceShape = context.settings().getService(context.model()); final var errorShapes = context .model() From be67b5cf91e06fff9b09f3aac2a086ab5a0ae94b Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Wed, 15 Jan 2025 19:22:51 -0800 Subject: [PATCH 02/12] auto commit --- ...fnyLocalServiceTypeConversionProtocol.java | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) 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 8aa36009e..507ffe2da 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 @@ -414,6 +414,11 @@ public void generateSerializers(final GenerationContext context) { if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigSerializer(context); } + final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService(serviceShape,model); + // Loop through each Shape in orphanShapes + for (Shape shape : orphanShapes) { + generateOrphanShapeSerializer(context, shape, alreadyVisited, serviceShape); + } generateSerializerFunctions(context, alreadyVisited); } @@ -425,12 +430,7 @@ public void generateOrphanShapeSerializer( ) { final Shape shape; if (currentShape.hasTrait(ReferenceTrait.class)) { - shape = - context - .model() - .expectShape( - currentShape.expectTrait(ReferenceTrait.class).getReferentId() - ); + return; } else { shape = currentShape; } @@ -890,6 +890,11 @@ public void generateDeserializers(final GenerationContext context) { if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigDeserializer(context); } + final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService(serviceShape,context.model()); + // Loop through each Shape in orphanShapes + for (Shape shape : orphanShapes) { + generateOrphanShapeDeserializer(context, shape, alreadyVisited, serviceShape); + } generateDeserializerFunctions(context, alreadyVisited); } @@ -901,12 +906,7 @@ public void generateOrphanShapeDeserializer( ) { final Shape shape; if (currentShape.hasTrait(ReferenceTrait.class)) { - shape = - context - .model() - .expectShape( - currentShape.expectTrait(ReferenceTrait.class).getReferentId() - ); + return; } else { shape = currentShape; } @@ -1106,6 +1106,7 @@ private void generateResponseDeserializer( } private void generateConfigSerializer(final GenerationContext context) { + final var service = context.settings().getService(context.model()); final var localServiceTrait = service.expectTrait(LocalServiceTrait.class); final var configShape = context @@ -1113,7 +1114,14 @@ private void generateConfigSerializer(final GenerationContext context) { .expectShape(localServiceTrait.getConfigId(), StructureShape.class); final var getInputToDafnyMethodName = SmithyNameResolver.getToDafnyMethodName(service, configShape, ""); - + if ( + !configShape + .toShapeId() + .getNamespace() + .equals(service.toShapeId().getNamespace()) + ) { + return; + } context .writerDelegator() .useFileWriter( @@ -1462,6 +1470,14 @@ private void generateConfigDeserializer(final GenerationContext context) { final var configShape = context .model() .expectShape(localServiceTrait.getConfigId(), StructureShape.class); + if ( + !configShape + .toShapeId() + .getNamespace() + .equals(serviceShape.toShapeId().getNamespace()) + ) { + return; + } final var getOutputFromDafnyMethodName = SmithyNameResolver.getFromDafnyMethodName(serviceShape, configShape, ""); From 503ac14ef79a14803a3de7f8080326c159c6baeb Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Thu, 16 Jan 2025 09:53:23 -0800 Subject: [PATCH 03/12] auto commit --- .../DafnyLocalServiceDirectedCodegen.java | 4 ++- ...fnyLocalServiceTypeConversionProtocol.java | 30 ++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceDirectedCodegen.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceDirectedCodegen.java index 6f038102f..f04f970af 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceDirectedCodegen.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceDirectedCodegen.java @@ -215,7 +215,9 @@ protected void generateOrphanedShapesForService( ShapeType.STRING, ShapeType.LONG, ShapeType.MAP, - ShapeType.LIST + ShapeType.LIST, + ShapeType.BOOLEAN, + ShapeType.BLOB ); for (final var shapeToGenerate : orderedShapes) { 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 507ffe2da..15fdea45b 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 @@ -412,7 +412,7 @@ public void generateSerializers(final GenerationContext context) { } generateErrorSerializer(context, alreadyVisited); if (serviceShape.hasTrait(LocalServiceTrait.class)) { - generateConfigSerializer(context); + generateConfigSerializer(context, alreadyVisited); } final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService(serviceShape,model); // Loop through each Shape in orphanShapes @@ -886,9 +886,9 @@ public void generateDeserializers(final GenerationContext context) { }); } } - generateErrorDeserializer(context); + generateErrorDeserializer(context, alreadyVisited); if (serviceShape.hasTrait(LocalServiceTrait.class)) { - generateConfigDeserializer(context); + generateConfigDeserializer(context, alreadyVisited); } final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService(serviceShape,context.model()); // Loop through each Shape in orphanShapes @@ -953,20 +953,23 @@ public void generateOrphanShapeDeserializer( ), SmithyNameResolver.smithyTypesNamespace(shape) ); - writer.write( """ - func $L(dafnyInput $L)($L) { + func $L(input interface{})($L) { ${C|} }""", inputFromDafnyMethodName, - inputType, - SmithyNameResolver.getSmithyType(shape, inputSymbol), + // inputType, + GoCodegenUtils.getType( + context.symbolProvider().toSymbol(shape), + shape, + true + ), writer.consumer(w -> { final var shapeVisitorOutput = shape.accept( new DafnyToSmithyShapeVisitor( context, - "dafnyInput", + "input", writer, false ) @@ -1105,8 +1108,7 @@ private void generateResponseDeserializer( ); } - private void generateConfigSerializer(final GenerationContext context) { - + private void generateConfigSerializer(final GenerationContext context, final Set alreadyVisited) { final var service = context.settings().getService(context.model()); final var localServiceTrait = service.expectTrait(LocalServiceTrait.class); final var configShape = context @@ -1122,6 +1124,7 @@ private void generateConfigSerializer(final GenerationContext context) { ) { return; } + alreadyVisited.add(configShape.getId()); context .writerDelegator() .useFileWriter( @@ -1462,7 +1465,7 @@ private void handleDepErrorSerializer( } } - private void generateConfigDeserializer(final GenerationContext context) { + private void generateConfigDeserializer(final GenerationContext context, final Set alreadyVisited) { final var serviceShape = context.settings().getService(context.model()); final var localServiceTrait = serviceShape.expectTrait( LocalServiceTrait.class @@ -1480,7 +1483,7 @@ private void generateConfigDeserializer(final GenerationContext context) { } final var getOutputFromDafnyMethodName = SmithyNameResolver.getFromDafnyMethodName(serviceShape, configShape, ""); - + alreadyVisited.add(configShape.getId()); context .writerDelegator() .useFileWriter( @@ -1531,8 +1534,7 @@ private void generateConfigDeserializer(final GenerationContext context) { ); } - private void generateErrorDeserializer(final GenerationContext context) { - final Set alreadyVisited = new HashSet<>(); + private void generateErrorDeserializer(final GenerationContext context, final Set alreadyVisited) { final var serviceShape = context.settings().getService(context.model()); final var errorShapes = context .model() From 0d7c6cf1d4832f1d426fba1f74182d571afb75ac Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Thu, 16 Jan 2025 11:38:33 -0800 Subject: [PATCH 04/12] auto commit --- ...fnyLocalServiceTypeConversionProtocol.java | 134 +++++++++++++----- 1 file changed, 95 insertions(+), 39 deletions(-) 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 368380795..e9283c937 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 @@ -432,16 +432,10 @@ public void generateSerializers(final GenerationContext context) { public void generateOrphanShapeSerializer( final GenerationContext context, - final Shape currentShape, + final Shape shape, final Set alreadyVisited, ServiceShape serviceShape ) { - final Shape shape; - if (currentShape.hasTrait(ReferenceTrait.class)) { - return; - } else { - shape = currentShape; - } if (alreadyVisited.contains(shape.toShapeId())) { return; } @@ -458,8 +452,14 @@ public void generateOrphanShapeSerializer( "" ); final var writerDelegator = context.writerDelegator(); - final String outputType; + String outputType; final var inputSymbol = context.symbolProvider().toSymbol(shape); + String inputType = GoCodegenUtils.getType(inputSymbol, shape, true); + Boolean isPointable = context + .symbolProvider() + .toSymbol(shape) + .getProperty(POINTABLE, Boolean.class) + .orElse(false); alreadyVisited.add(shape.toShapeId()); if (shape.hasTrait(PositionalTrait.class)) { // Output type in To Dafny should be unwrapped @@ -476,6 +476,57 @@ public void generateOrphanShapeSerializer( } else { outputType = DafnyNameResolver.getDafnyType(shape, inputSymbol); } + + if (shape.hasTrait(ReferenceTrait.class)) { + final var referenceTrait = shape.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 = + DafnyNameResolver.getDafnyInterfaceClient( + resourceOrService.asServiceShape().get(), + resourceOrService.getTrait(ServiceTrait.class).get() + ); + inputType = + GoCodegenUtils.getType( + context.symbolProvider().toSymbol(resourceOrService), + resourceOrService, + true + ); + } else { + outputType = + DafnyNameResolver.getDafnyInterfaceClient( + resourceOrService + ); + System.out.println("Here"); + System.out.println(DafnyNameResolver.getDafnyInterfaceClient( + resourceOrService + )); + System.out.println(inputToDafnyMethodName); + inputType = + SmithyNameResolver + .shapeNamespace(resourceOrService) + .concat(".") + .concat( + context.symbolProvider().toSymbol(serviceShape).getName() + ); + } + } + } + + inputType = isPointable ? "*".concat(inputType) : inputType; + final var iptype = inputType; + final var optype = outputType; writerDelegator.useFileWriter( "%s/%s".formatted( SmithyNameResolver.shapeNamespace(serviceShape), @@ -495,9 +546,9 @@ public void generateOrphanShapeSerializer( ${C|} }""", inputToDafnyMethodName, - GoCodegenUtils.getType(inputSymbol, shape, true), + iptype, // SmithyNameResolver.getSmithyType(shape, inputSymbol), - outputType, + optype, writer.consumer(w -> { final String shapeVisitorOutput = shape.accept( new SmithyToDafnyShapeVisitor( @@ -915,16 +966,10 @@ public void generateDeserializers(final GenerationContext context) { public void generateOrphanShapeDeserializer( final GenerationContext context, - final Shape currentShape, + final Shape shape, final Set alreadyVisited, ServiceShape serviceShape ) { - final Shape shape; - if (currentShape.hasTrait(ReferenceTrait.class)) { - return; - } else { - shape = currentShape; - } if (alreadyVisited.contains(shape.toShapeId())) { return; } @@ -936,25 +981,41 @@ public void generateOrphanShapeDeserializer( return; } final var writerDelegator = context.writerDelegator(); + var outputType = GoCodegenUtils.getType( + context.symbolProvider().toSymbol(shape), + shape, + true + ); final var inputFromDafnyMethodName = SmithyNameResolver.getFromDafnyMethodName(serviceShape, shape, ""); - final var inputSymbol = context.symbolProvider().toSymbol(shape); - final String inputType; - if (shape.hasTrait(PositionalTrait.class)) { - // shape in To native should be unwrapped - Shape inputForPositional = context - .model() - .expectShape( - shape.getAllMembers().values().stream().findFirst().get().getTarget() + if (shape.hasTrait(ReferenceTrait.class)) { + final var referenceTrait = shape.expectTrait( + ReferenceTrait.class ); - Symbol symbolForPositional = context - .symbolProvider() - .toSymbol(inputForPositional); - inputType = - DafnyNameResolver.getDafnyType(inputForPositional, symbolForPositional); - } else { - inputType = DafnyNameResolver.getDafnyType(shape, inputSymbol); - } + final var resourceOrService = context + .model() + .expectShape(referenceTrait.getReferentId()); + if (resourceOrService.isServiceShape()) { + if (resourceOrService.hasTrait(ServiceTrait.class)) { + outputType = + SmithyNameResolver.getAwsServiceClient( + resourceOrService.expectTrait(ServiceTrait.class) + ); + } else { + final var namespace = SmithyNameResolver + .shapeNamespace(resourceOrService) + .concat("."); + outputType = + "*".concat(namespace.concat( + context + .symbolProvider() + .toSymbol(resourceOrService) + .getName() + )); + } + } + } + final var op = outputType; writerDelegator.useFileWriter( "%s/%s".formatted( SmithyNameResolver.shapeNamespace(serviceShape), @@ -974,12 +1035,7 @@ public void generateOrphanShapeDeserializer( ${C|} }""", inputFromDafnyMethodName, - // inputType, - GoCodegenUtils.getType( - context.symbolProvider().toSymbol(shape), - shape, - true - ), + op, writer.consumer(w -> { final var shapeVisitorOutput = shape.accept( new DafnyToSmithyShapeVisitor( From 958063643dbf71f215b1875c4041a2868f4502f8 Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Thu, 16 Jan 2025 13:14:28 -0800 Subject: [PATCH 05/12] formatting and nits --- ...fnyLocalServiceTypeConversionProtocol.java | 250 +++++++++--------- 1 file changed, 118 insertions(+), 132 deletions(-) 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 e9283c937..139096e52 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 @@ -422,10 +422,19 @@ public void generateSerializers(final GenerationContext context) { if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigSerializer(context, alreadyVisited); } - final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService(serviceShape,model); + final var orphanShapes = + ModelUtils.getTopologicallyOrderedOrphanedShapesForService( + serviceShape, + model + ); // Loop through each Shape in orphanShapes for (Shape shape : orphanShapes) { - generateOrphanShapeSerializer(context, shape, alreadyVisited, serviceShape); + generateOrphanShapeSerializer( + context, + shape, + alreadyVisited, + serviceShape + ); } generateSerializerFunctions(context, alreadyVisited); } @@ -453,80 +462,44 @@ public void generateOrphanShapeSerializer( ); final var writerDelegator = context.writerDelegator(); String outputType; - final var inputSymbol = context.symbolProvider().toSymbol(shape); - String inputType = GoCodegenUtils.getType(inputSymbol, shape, true); - Boolean isPointable = context - .symbolProvider() - .toSymbol(shape) - .getProperty(POINTABLE, Boolean.class) - .orElse(false); + final var curSymbol = context.symbolProvider().toSymbol(shape); + final String inputType; alreadyVisited.add(shape.toShapeId()); - if (shape.hasTrait(PositionalTrait.class)) { - // Output type in To Dafny should be unwrapped - Shape shapeForPositional = context - .model() - .expectShape( - shape.getAllMembers().values().stream().findFirst().get().getTarget() - ); - Symbol symbolForPositional = context - .symbolProvider() - .toSymbol(shapeForPositional); - outputType = - DafnyNameResolver.getDafnyType(shapeForPositional, symbolForPositional); - } else { - outputType = DafnyNameResolver.getDafnyType(shape, inputSymbol); - } - if (shape.hasTrait(ReferenceTrait.class)) { - final var referenceTrait = shape.expectTrait( - ReferenceTrait.class - ); + final var referenceTrait = shape.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 = - DafnyNameResolver.getDafnyInterfaceClient( - resourceOrService.asServiceShape().get(), - resourceOrService.getTrait(ServiceTrait.class).get() - ); - inputType = - GoCodegenUtils.getType( - context.symbolProvider().toSymbol(resourceOrService), - resourceOrService, - true - ); - } else { - outputType = - DafnyNameResolver.getDafnyInterfaceClient( - resourceOrService - ); - System.out.println("Here"); - System.out.println(DafnyNameResolver.getDafnyInterfaceClient( - resourceOrService - )); - System.out.println(inputToDafnyMethodName); - inputType = - SmithyNameResolver - .shapeNamespace(resourceOrService) - .concat(".") - .concat( - context.symbolProvider().toSymbol(serviceShape).getName() - ); - } + if (resourceOrService.isResourceShape()) { + throw new IllegalStateException( + "Reference to resource shapes are already handled in generateDeserializers function." + ); } + if (resourceOrService.hasTrait(ServiceTrait.class)) { + outputType = + DafnyNameResolver.getDafnyInterfaceClient( + resourceOrService.asServiceShape().get(), + resourceOrService.getTrait(ServiceTrait.class).get() + ); + inputType = + GoCodegenUtils.getType( + context.symbolProvider().toSymbol(resourceOrService), + resourceOrService, + true + ); + } else { + outputType = + DafnyNameResolver.getDafnyInterfaceClient(resourceOrService); + inputType = + SmithyNameResolver + .shapeNamespace(resourceOrService) + .concat(".") + .concat(context.symbolProvider().toSymbol(serviceShape).getName()); + } + } else { + inputType = GoCodegenUtils.getType(curSymbol, shape, true); + outputType = DafnyNameResolver.getDafnyType(shape, curSymbol); } - - inputType = isPointable ? "*".concat(inputType) : inputType; - final var iptype = inputType; - final var optype = outputType; writerDelegator.useFileWriter( "%s/%s".formatted( SmithyNameResolver.shapeNamespace(serviceShape), @@ -546,9 +519,8 @@ public void generateOrphanShapeSerializer( ${C|} }""", inputToDafnyMethodName, - iptype, - // SmithyNameResolver.getSmithyType(shape, inputSymbol), - optype, + inputType, + outputType, writer.consumer(w -> { final String shapeVisitorOutput = shape.accept( new SmithyToDafnyShapeVisitor( @@ -956,10 +928,19 @@ public void generateDeserializers(final GenerationContext context) { if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigDeserializer(context, alreadyVisited); } - final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService(serviceShape,context.model()); + final var orphanShapes = + ModelUtils.getTopologicallyOrderedOrphanedShapesForService( + serviceShape, + context.model() + ); // Loop through each Shape in orphanShapes for (Shape shape : orphanShapes) { - generateOrphanShapeDeserializer(context, shape, alreadyVisited, serviceShape); + generateOrphanShapeDeserializer( + context, + shape, + alreadyVisited, + serviceShape + ); } generateDeserializerFunctions(context, alreadyVisited); } @@ -976,46 +957,47 @@ public void generateOrphanShapeDeserializer( if (shape.isOperationShape() || shape.isResourceShape()) { return; } - if (shape.hasTrait(UnitTypeTrait.class)) { return; } final var writerDelegator = context.writerDelegator(); - var outputType = GoCodegenUtils.getType( - context.symbolProvider().toSymbol(shape), - shape, - true - ); + final String outputType; final var inputFromDafnyMethodName = SmithyNameResolver.getFromDafnyMethodName(serviceShape, shape, ""); - if (shape.hasTrait(ReferenceTrait.class)) { - final var referenceTrait = shape.expectTrait( - ReferenceTrait.class + if (shape.hasTrait(ReferenceTrait.class)) { + final var referenceTrait = shape.expectTrait(ReferenceTrait.class); + final var resourceOrService = context + .model() + .expectShape(referenceTrait.getReferentId()); + if (resourceOrService.isResourceShape()) { + throw new IllegalStateException( + "Reference to resource shapes are already handled in generateDeserializers function." ); - final var resourceOrService = context - .model() - .expectShape(referenceTrait.getReferentId()); - if (resourceOrService.isServiceShape()) { - if (resourceOrService.hasTrait(ServiceTrait.class)) { - outputType = - SmithyNameResolver.getAwsServiceClient( - resourceOrService.expectTrait(ServiceTrait.class) - ); - } else { - final var namespace = SmithyNameResolver - .shapeNamespace(resourceOrService) - .concat("."); - outputType = - "*".concat(namespace.concat( - context - .symbolProvider() - .toSymbol(resourceOrService) - .getName() - )); - } - } } - final var op = outputType; + if (resourceOrService.hasTrait(ServiceTrait.class)) { + outputType = + SmithyNameResolver.getAwsServiceClient( + resourceOrService.expectTrait(ServiceTrait.class) + ); + } else { + final var namespace = SmithyNameResolver + .shapeNamespace(resourceOrService) + .concat("."); + outputType = + "*".concat( + namespace.concat( + context.symbolProvider().toSymbol(resourceOrService).getName() + ) + ); + } + } else { + outputType = + GoCodegenUtils.getType( + context.symbolProvider().toSymbol(shape), + shape, + true + ); + } writerDelegator.useFileWriter( "%s/%s".formatted( SmithyNameResolver.shapeNamespace(serviceShape), @@ -1035,15 +1017,10 @@ public void generateOrphanShapeDeserializer( ${C|} }""", inputFromDafnyMethodName, - op, + outputType, writer.consumer(w -> { final var shapeVisitorOutput = shape.accept( - new DafnyToSmithyShapeVisitor( - context, - "input", - writer, - false - ) + new DafnyToSmithyShapeVisitor(context, "input", writer, false) ); writer.write( """ @@ -1179,7 +1156,10 @@ private void generateResponseDeserializer( ); } - private void generateConfigSerializer(final GenerationContext context, final Set alreadyVisited) { + private void generateConfigSerializer( + final GenerationContext context, + final Set alreadyVisited + ) { final var service = context.settings().getService(context.model()); final var localServiceTrait = service.expectTrait(LocalServiceTrait.class); final var configShape = context @@ -1187,14 +1167,14 @@ private void generateConfigSerializer(final GenerationContext context, final Set .expectShape(localServiceTrait.getConfigId(), StructureShape.class); final var getInputToDafnyMethodName = SmithyNameResolver.getToDafnyMethodName(service, configShape, ""); - if ( - !configShape - .toShapeId() - .getNamespace() - .equals(service.toShapeId().getNamespace()) - ) { - return; - } + if ( + !configShape + .toShapeId() + .getNamespace() + .equals(service.toShapeId().getNamespace()) + ) { + return; + } alreadyVisited.add(configShape.getId()); context .writerDelegator() @@ -1540,7 +1520,10 @@ private void handleDepErrorSerializer( } } - private void generateConfigDeserializer(final GenerationContext context, final Set alreadyVisited) { + private void generateConfigDeserializer( + final GenerationContext context, + final Set alreadyVisited + ) { final var serviceShape = context.settings().getService(context.model()); final var localServiceTrait = serviceShape.expectTrait( LocalServiceTrait.class @@ -1548,14 +1531,14 @@ private void generateConfigDeserializer(final GenerationContext context, final S final var configShape = context .model() .expectShape(localServiceTrait.getConfigId(), StructureShape.class); - if ( - !configShape - .toShapeId() - .getNamespace() - .equals(serviceShape.toShapeId().getNamespace()) - ) { - return; - } + if ( + !configShape + .toShapeId() + .getNamespace() + .equals(serviceShape.toShapeId().getNamespace()) + ) { + return; + } final var getOutputFromDafnyMethodName = SmithyNameResolver.getFromDafnyMethodName(serviceShape, configShape, ""); alreadyVisited.add(configShape.getId()); @@ -1609,7 +1592,10 @@ private void generateConfigDeserializer(final GenerationContext context, final S ); } - private void generateErrorDeserializer(final GenerationContext context, final Set alreadyVisited) { + private void generateErrorDeserializer( + final GenerationContext context, + final Set alreadyVisited + ) { final var serviceShape = context.settings().getService(context.model()); final var errorShapes = context .model() From 24d0ac92d21ae07e159c65c7efb4f6f69074b2b3 Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Thu, 16 Jan 2025 13:27:43 -0800 Subject: [PATCH 06/12] add already visited to refresource --- .../localservice/DafnyLocalServiceTypeConversionProtocol.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 139096e52..d8513eba1 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 @@ -214,6 +214,7 @@ public void generateSerializers(final GenerationContext context) { final var resource = refResource .expectTrait(ReferenceTrait.class) .getReferentId(); + alreadyVisited.add(resource); if (!refResource.expectTrait(ReferenceTrait.class).isService()) { final var resourceShape = model.expectShape( resource, @@ -715,7 +716,7 @@ public void generateDeserializers(final GenerationContext context) { final var resource = refResource .expectTrait(ReferenceTrait.class) .getReferentId(); - + alreadyVisited.add(resource); if (!refResource.expectTrait(ReferenceTrait.class).isService()) { final var resourceShape = context .model() From 30fa504d6409289b1a48468a61ee0cda8d6a8b84 Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Thu, 16 Jan 2025 13:55:24 -0800 Subject: [PATCH 07/12] auto commit --- .../go/ImplementationFromDafny-go/go.mod | 6 +++--- .../ExternDefinitions/extern.go | 18 ++++++++++-------- .../runtimes/go/TestsFromDafny-go/go.mod | 7 +++---- ...afnyLocalServiceTypeConversionProtocol.java | 6 +++--- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/TestModels/OrphanedShapes/runtimes/go/ImplementationFromDafny-go/go.mod b/TestModels/OrphanedShapes/runtimes/go/ImplementationFromDafny-go/go.mod index 7b5f8db98..0b329403c 100644 --- a/TestModels/OrphanedShapes/runtimes/go/ImplementationFromDafny-go/go.mod +++ b/TestModels/OrphanedShapes/runtimes/go/ImplementationFromDafny-go/go.mod @@ -3,8 +3,8 @@ module github.com/smithy-lang/smithy-dafny/TestModels/OrphanedShapes go 1.23.0 require ( - github.com/dafny-lang/DafnyRuntimeGo/v4 v4.9.1 - github.com/dafny-lang/DafnyStandardLibGo v0.0.0 + github.com/dafny-lang/DafnyRuntimeGo/v4 v4.9.2 + github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library v0.0.0 ) -replace github.com/dafny-lang/DafnyStandardLibGo => ../../../../dafny-dependencies/StandardLibrary/runtimes/go/ImplementationFromDafny-go/ +replace github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library => ../../../../dafny-dependencies/StandardLibrary/runtimes/go/ImplementationFromDafny-go/ diff --git a/TestModels/OrphanedShapes/runtimes/go/TestsFromDafny-go/ExternDefinitions/extern.go b/TestModels/OrphanedShapes/runtimes/go/TestsFromDafny-go/ExternDefinitions/extern.go index 6b1a3bbda..e2028a060 100644 --- a/TestModels/OrphanedShapes/runtimes/go/TestsFromDafny-go/ExternDefinitions/extern.go +++ b/TestModels/OrphanedShapes/runtimes/go/TestsFromDafny-go/ExternDefinitions/extern.go @@ -1,9 +1,7 @@ package ExternDefinitions import ( - "fmt" - - Wrappers "github.com/dafny-lang/DafnyStandardLibGo/Wrappers" + Wrappers "github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library/Wrappers" OrphanedResource "github.com/smithy-lang/smithy-dafny/TestModels/OrphanedShapes/OrphanedResource" SimpleOrphanedTypes "github.com/smithy-lang/smithy-dafny/TestModels/OrphanedShapes/SimpleOrphanedTypes" simpleorphanedsmithygenerated "github.com/smithy-lang/smithy-dafny/TestModels/OrphanedShapes/simpleorphanedsmithygenerated" @@ -12,12 +10,16 @@ import ( var _ Wrappers.Dummy__ -// TODO: Finish implementation. -// This is missing structure converter. +// TODO: Remove this once Dafny bug is fixed. +// Dafny bug: https://t.corp.amazon.com/9a9028fd-2711-4843-b8f0-09965f7e61a7/communication#f03694be-7410-47f9-866d-e43093b018f9 +var m_ExternDefinitions = CompanionStruct_Default___{} func (CompanionStruct_Default___) InitializeOrphanedStructure(input SimpleOrphanedTypes.OrphanedStructure) SimpleOrphanedTypes.OrphanedStructure { - // Missing Structure converter - return input + nativeInput := simpleorphanedsmithygenerated.OrphanedStructure_FromDafny(input) + stringToReplace := "the extern MUST use Smithy-generated conversions to set this value in the native structure" + nativeInput.StringValue = &stringToReplace + dafnyInitializedStructure := simpleorphanedsmithygenerated.OrphanedStructure_ToDafny(nativeInput) + return dafnyInitializedStructure } func (CompanionStruct_Default___) CallNativeOrphanedResource(input *OrphanedResource.OrphanedResource) Wrappers.Result { @@ -32,7 +34,7 @@ func (CompanionStruct_Default___) CallNativeOrphanedResource(input *OrphanedReso } func (CompanionStruct_Default___) CallNativeOrphanedError(input SimpleOrphanedTypes.Error) SimpleOrphanedTypes.Error { - native_error := simpleorphanedsmithygenerated.Error_FromDafny(input) + native_error := simpleorphanedsmithygenerated.Error_FromDafny(input).(simpleorphanedsmithygeneratedtypes.OrphanedError) native_error.Message = "the extern MUST set this string using the catch-all error converter, NOT the orphaned error-specific converter" dafny_error_again := simpleorphanedsmithygenerated.Error_ToDafny(native_error) return dafny_error_again diff --git a/TestModels/OrphanedShapes/runtimes/go/TestsFromDafny-go/go.mod b/TestModels/OrphanedShapes/runtimes/go/TestsFromDafny-go/go.mod index 349df89cc..92c300db6 100644 --- a/TestModels/OrphanedShapes/runtimes/go/TestsFromDafny-go/go.mod +++ b/TestModels/OrphanedShapes/runtimes/go/TestsFromDafny-go/go.mod @@ -2,13 +2,12 @@ module github.com/smithy-lang/smithy-dafny/TestModels/OrphanedShapes/test go 1.23.0 -require github.com/dafny-lang/DafnyStandardLibGo v0.0.0 - require ( - github.com/dafny-lang/DafnyRuntimeGo/v4 v4.9.1 + github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library v0.0.0 + github.com/dafny-lang/DafnyRuntimeGo/v4 v4.9.2 github.com/smithy-lang/smithy-dafny/TestModels/OrphanedShapes v0.0.0 ) replace github.com/smithy-lang/smithy-dafny/TestModels/OrphanedShapes v0.0.0 => ../ImplementationFromDafny-go -replace github.com/dafny-lang/DafnyStandardLibGo => ../../../../dafny-dependencies/StandardLibrary/runtimes/go/ImplementationFromDafny-go/ +replace github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library => ../../../../dafny-dependencies/StandardLibrary/runtimes/go/ImplementationFromDafny-go/ 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 d8513eba1..d2a1dd25d 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 @@ -214,7 +214,7 @@ public void generateSerializers(final GenerationContext context) { final var resource = refResource .expectTrait(ReferenceTrait.class) .getReferentId(); - alreadyVisited.add(resource); + alreadyVisited.add(refResource.toShapeId()); if (!refResource.expectTrait(ReferenceTrait.class).isService()) { final var resourceShape = model.expectShape( resource, @@ -473,7 +473,7 @@ public void generateOrphanShapeSerializer( .expectShape(referenceTrait.getReferentId()); if (resourceOrService.isResourceShape()) { throw new IllegalStateException( - "Reference to resource shapes are already handled in generateDeserializers function." + "Reference to resource shapes are already handled in generateSerializers function." ); } if (resourceOrService.hasTrait(ServiceTrait.class)) { @@ -716,7 +716,7 @@ public void generateDeserializers(final GenerationContext context) { final var resource = refResource .expectTrait(ReferenceTrait.class) .getReferentId(); - alreadyVisited.add(resource); + alreadyVisited.add(refResource.toShapeId()); if (!refResource.expectTrait(ReferenceTrait.class).isService()) { final var resourceShape = context .model() From 941a9dc73b6ec21b8e29f710d2f3dd3b5573f0cd Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Thu, 16 Jan 2025 13:56:03 -0800 Subject: [PATCH 08/12] auto commit --- .../java/software/amazon/polymorph/smithygo/GoTestModels.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithygo/GoTestModels.java b/codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithygo/GoTestModels.java index 47bb0fecd..2bb352aa8 100644 --- a/codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithygo/GoTestModels.java +++ b/codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithygo/GoTestModels.java @@ -21,8 +21,6 @@ class GoTestModels extends TestModelTest { DISABLED_TESTS.add("AggregateReferences"); DISABLED_TESTS.add("Documentation"); DISABLED_TESTS.add("LanguageSpecificLogic"); - // Needs work to generate some missing orphaned shape conversion methods - DISABLED_TESTS.add("OrphanedShapes"); DISABLED_TESTS.add("SimpleTypes/BigDecimal"); DISABLED_TESTS.add("SimpleTypes/BigInteger"); DISABLED_TESTS.add("SimpleTypes/SimpleByte"); From ca63226d3dd07a47428e129171615de8c0977d33 Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Thu, 16 Jan 2025 16:27:11 -0800 Subject: [PATCH 09/12] auto commit --- .../localservice/DafnyLocalServiceTypeConversionProtocol.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 d2a1dd25d..aa4186013 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 @@ -423,6 +423,7 @@ public void generateSerializers(final GenerationContext context) { if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigSerializer(context, alreadyVisited); } + generateSerializerFunctions(context, alreadyVisited); final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService( serviceShape, @@ -437,7 +438,6 @@ public void generateSerializers(final GenerationContext context) { serviceShape ); } - generateSerializerFunctions(context, alreadyVisited); } public void generateOrphanShapeSerializer( @@ -929,6 +929,7 @@ public void generateDeserializers(final GenerationContext context) { if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigDeserializer(context, alreadyVisited); } + generateDeserializerFunctions(context, alreadyVisited); final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService( serviceShape, @@ -943,7 +944,6 @@ public void generateDeserializers(final GenerationContext context) { serviceShape ); } - generateDeserializerFunctions(context, alreadyVisited); } public void generateOrphanShapeDeserializer( From 07f9ba6898c8bc1db39aa8cfe11ca2bbb6581ebe Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Thu, 16 Jan 2025 16:34:00 -0800 Subject: [PATCH 10/12] Revert "auto commit" This reverts commit ca63226d3dd07a47428e129171615de8c0977d33. --- .../localservice/DafnyLocalServiceTypeConversionProtocol.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 aa4186013..d2a1dd25d 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 @@ -423,7 +423,6 @@ public void generateSerializers(final GenerationContext context) { if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigSerializer(context, alreadyVisited); } - generateSerializerFunctions(context, alreadyVisited); final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService( serviceShape, @@ -438,6 +437,7 @@ public void generateSerializers(final GenerationContext context) { serviceShape ); } + generateSerializerFunctions(context, alreadyVisited); } public void generateOrphanShapeSerializer( @@ -929,7 +929,6 @@ public void generateDeserializers(final GenerationContext context) { if (serviceShape.hasTrait(LocalServiceTrait.class)) { generateConfigDeserializer(context, alreadyVisited); } - generateDeserializerFunctions(context, alreadyVisited); final var orphanShapes = ModelUtils.getTopologicallyOrderedOrphanedShapesForService( serviceShape, @@ -944,6 +943,7 @@ public void generateDeserializers(final GenerationContext context) { serviceShape ); } + generateDeserializerFunctions(context, alreadyVisited); } public void generateOrphanShapeDeserializer( From 5d56ad94cb72c6042cc0aad3af1e08ccccbf15b9 Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Fri, 17 Jan 2025 09:13:44 -0800 Subject: [PATCH 11/12] auto commit --- ...fnyLocalServiceTypeConversionProtocol.java | 11 ++----- .../smithygo/utils/GoCodegenUtils.java | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) 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 d2a1dd25d..e1f68a0c0 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 @@ -446,11 +446,7 @@ public void generateOrphanShapeSerializer( final Set alreadyVisited, ServiceShape serviceShape ) { - if (alreadyVisited.contains(shape.toShapeId())) { - return; - } - // We don't need type conversion for operation shape and resource shape - if (shape.isOperationShape() || shape.isResourceShape()) { + if (GoCodegenUtils.shapeShouldHaveConversionFunction(shape) == false || alreadyVisited.contains(shape.toShapeId())) { return; } if (shape.hasTrait(UnitTypeTrait.class)) { @@ -952,10 +948,7 @@ public void generateOrphanShapeDeserializer( final Set alreadyVisited, ServiceShape serviceShape ) { - if (alreadyVisited.contains(shape.toShapeId())) { - return; - } - if (shape.isOperationShape() || shape.isResourceShape()) { + if (GoCodegenUtils.shapeShouldHaveConversionFunction(shape) == false || alreadyVisited.contains(shape.toShapeId())) { return; } if (shape.hasTrait(UnitTypeTrait.class)) { 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 00fc41ffa..9b56d02ed 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 @@ -6,6 +6,8 @@ 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.smithy.model.traits.ErrorTrait; import software.amazon.polymorph.traits.PositionalTrait; import software.amazon.polymorph.traits.ReferenceTrait; import software.amazon.smithy.aws.traits.ServiceTrait; @@ -188,4 +190,34 @@ private static String getInputOrOutputName( ); } } + + /** + * Returns true if a conversion function should be written for the shape, false otherwise. + * Conversion functions are only written for "complex" shapes: + * - StructureShapes ("complex" because StructureShapes can be recursive) + * - except for non-AWS SDK StructureShapes with ErrorTrait; these aren't "complex" + * - UnionShapes ("complex" because the conversion is not a one-liner) + * - EnumShapes or StringShapes with EnumTrait ("complex" because the conversion is not a one-liner) + * @param shape + * @return + */ + public static boolean shapeShouldHaveConversionFunction(Shape shape) { + if (shape.isStructureShape()) { + if ( + !SmithyNameResolver.isShapeFromAWSSDK(shape) && + shape.hasTrait(ErrorTrait.class) + ) { + return false; + } + return true; + } else if (shape.isUnionShape()) { + return true; + } else if ( + (shape.isStringShape() && shape.hasTrait(EnumTrait.class)) || + shape.isEnumShape() + ) { + return true; + } + return false; + } } From 9aeeae64ec3049104f136ef81fc227ae3e1b6952 Mon Sep 17 00:00:00 2001 From: rishav-karanjit Date: Fri, 17 Jan 2025 11:37:21 -0800 Subject: [PATCH 12/12] formatting --- .../DafnyLocalServiceTypeConversionProtocol.java | 10 ++++++++-- .../polymorph/smithygo/utils/GoCodegenUtils.java | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) 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 e1f68a0c0..fa4660f9b 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 @@ -446,7 +446,10 @@ public void generateOrphanShapeSerializer( final Set alreadyVisited, ServiceShape serviceShape ) { - if (GoCodegenUtils.shapeShouldHaveConversionFunction(shape) == false || alreadyVisited.contains(shape.toShapeId())) { + if ( + GoCodegenUtils.shapeShouldHaveConversionFunction(shape) == false || + alreadyVisited.contains(shape.toShapeId()) + ) { return; } if (shape.hasTrait(UnitTypeTrait.class)) { @@ -948,7 +951,10 @@ public void generateOrphanShapeDeserializer( final Set alreadyVisited, ServiceShape serviceShape ) { - if (GoCodegenUtils.shapeShouldHaveConversionFunction(shape) == false || alreadyVisited.contains(shape.toShapeId())) { + if ( + GoCodegenUtils.shapeShouldHaveConversionFunction(shape) == false || + alreadyVisited.contains(shape.toShapeId()) + ) { return; } if (shape.hasTrait(UnitTypeTrait.class)) { 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 9b56d02ed..8715cbf9b 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 @@ -7,7 +7,6 @@ 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.smithy.model.traits.ErrorTrait; import software.amazon.polymorph.traits.PositionalTrait; import software.amazon.polymorph.traits.ReferenceTrait; import software.amazon.smithy.aws.traits.ServiceTrait; @@ -22,6 +21,7 @@ import software.amazon.smithy.model.shapes.Shape; import software.amazon.smithy.model.shapes.ShapeType; import software.amazon.smithy.model.traits.EnumTrait; +import software.amazon.smithy.model.traits.ErrorTrait; import software.amazon.smithy.model.traits.UnitTypeTrait; public class GoCodegenUtils { @@ -190,7 +190,7 @@ private static String getInputOrOutputName( ); } } - + /** * Returns true if a conversion function should be written for the shape, false otherwise. * Conversion functions are only written for "complex" shapes: