From 78b79ae529c9093fc42ac3099e452c45af6dc450 Mon Sep 17 00:00:00 2001 From: Rishav karanjit Date: Tue, 12 Nov 2024 16:22:31 -0800 Subject: [PATCH] fix: change function name for positional trait (#714) --- .../smithygo/codegen/ValidationGenerator.java | 15 ++++++++-- ...fnyLocalServiceTypeConversionProtocol.java | 12 ++++++-- .../shapevisitor/ShapeVisitorHelper.java | 9 ++++-- .../polymorph/smithygo/utils/Constants.java | 30 ++++++++++++++++++- 4 files changed, 58 insertions(+), 8 deletions(-) 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 c93a45585..7d8d65c30 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 @@ -473,7 +473,8 @@ private void renderListShape( ) { final String funcName = Constants.funcNameGenerator( memberShape, - "Validate" + "Validate", + context.model() ); final String funcInput = dataSource.startsWith("input") ? "" : dataSource; if (!funcInput.isEmpty()) { @@ -567,7 +568,11 @@ private void renderMapShape( !validationFuncMap.containsKey(memberShape) && (!keyValidation.isEmpty() || !valueValidation.isEmpty()) ) { - final var funcName = Constants.funcNameGenerator(memberShape, "Validate"); + final var funcName = Constants.funcNameGenerator( + memberShape, + "Validate", + context.model() + ); final var funcInput = dataSource.startsWith("input") ? "" : dataSource; if (!funcInput.isEmpty()) { final var currServiceShapeNamespace = SmithyNameResolver.shapeNamespace( @@ -634,7 +639,11 @@ private void renderUnionShape( final StringBuilder validationCode, final String dataSource ) { - final var funcName = Constants.funcNameGenerator(memberShape, "Validate"); + final var funcName = Constants.funcNameGenerator( + memberShape, + "Validate", + context.model() + ); final var funcInput = dataSource.startsWith("input") ? "" : dataSource; var dataSourceForUnion = dataSource; final var currServiceShapeNamespace = 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 bdcf36826..b76381f46 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 @@ -1470,7 +1470,11 @@ private void generateSerializerFunctions( return $L } """, - Constants.funcNameGenerator(visitingMemberShape, "ToDafny"), + Constants.funcNameGenerator( + visitingMemberShape, + "ToDafny", + context.model() + ), inputType, outputType, SmithyToDafnyShapeVisitor.getConversionFunc(visitingMemberShape) @@ -1560,7 +1564,11 @@ private void generateDeserializerFunctions( func $L(input interface{})($L) { $L }""", - Constants.funcNameGenerator(visitingMemberShape, "FromDafny"), + Constants.funcNameGenerator( + visitingMemberShape, + "FromDafny", + context.model() + ), outputType, DafnyToSmithyShapeVisitor.getConversionFunc(visitingMemberShape) ); diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java index c20a0d92a..e69dfb1c4 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java @@ -94,7 +94,8 @@ public static String toNativeShapeVisitorWriter( } final String funcName = Constants.funcNameGenerator( memberShape, - "FromDafny" + "FromDafny", + context.model() ); return (funcName.concat("(").concat(dataSource).concat(")")); } @@ -145,7 +146,11 @@ public static String toDafnyShapeVisitorWriter( ) ); } - final String funcName = Constants.funcNameGenerator(memberShape, "ToDafny"); + final String funcName = Constants.funcNameGenerator( + memberShape, + "ToDafny", + context.model() + ); return (funcName.concat("(").concat(dataSource).concat(")")); } } diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/Constants.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/Constants.java index 17888034f..7cdba1496 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/Constants.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/Constants.java @@ -1,6 +1,10 @@ package software.amazon.polymorph.smithygo.utils; +import software.amazon.polymorph.traits.PositionalTrait; +import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.MemberShape; +import software.amazon.smithy.model.shapes.Shape; +import software.amazon.smithy.utils.StringUtils; public class Constants { @@ -9,7 +13,31 @@ public class Constants { // TODO: Is it possible to make this function name shorter and in camelCase? /** - * Generates a function name for shape visitors for AWS SDK and localservice. + * Generates a function name for memberShapes. + * Generates private function for all shape excepts memberShape whose containerShape has positional trait + * + * @param memberShape The visiting MemberShape + * @param suffix A string to be appended at the end of the generated function name + * @param model The smithy model being used + * @return A string representing the generated function name + */ + public static String funcNameGenerator( + final MemberShape memberShape, + final String suffix, + final Model model + ) { + String funcName = funcNameGenerator(memberShape, suffix); + final Shape containerShape = model.expectShape(memberShape.getContainer()); + // membershape inside a container shape with positional trait has to be exposed. + if (containerShape.hasTrait(PositionalTrait.class)) { + funcName = StringUtils.capitalize(funcName); + } + return funcName; + } + + /** + * Generates a function name for memberShapes. + * Always generates private function for all shape * * @param memberShape The visiting MemberShape * @param suffix A string to be appended at the end of the generated function name