From 323b91946ce992ff0a4f863dceb2f0e97bbd2743 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 12 Jul 2024 16:43:37 -0700 Subject: [PATCH 01/26] Quick fixes to name discovery --- TestModels/aws-sdks/glue/Makefile | 2 ++ .../nameresolver/AwsSdkNativeV2.java | 32 ++++++------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/TestModels/aws-sdks/glue/Makefile b/TestModels/aws-sdks/glue/Makefile index 578beb9a3..cbb1478b0 100644 --- a/TestModels/aws-sdks/glue/Makefile +++ b/TestModels/aws-sdks/glue/Makefile @@ -21,6 +21,8 @@ AWS_SDK_CMD=--aws-sdk # This project has no dependencies # DEPENDENT-MODELS:= +POLYMORPH_OPTIONS=--generate project-files --generate client-constructors + # There is no wrapped target for aws-sdk types _polymorph_wrapped: ; _polymorph_wrapped_dafny: ; diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index dc2000a57..e220bc4f4 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -20,6 +20,7 @@ import software.amazon.polymorph.smithyjava.generator.CodegenSubject; import software.amazon.polymorph.smithyjava.generator.awssdk.v2.JavaAwsSdkV2; import software.amazon.polymorph.utils.AwsSdkNameResolverHelpers; +import software.amazon.smithy.aws.traits.ServiceTrait; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.MemberShape; import software.amazon.smithy.model.shapes.ServiceShape; @@ -97,24 +98,6 @@ private void checkForAwsServiceConstants() { /** Validates that Polymorph knows non-smithy modeled constants for an AWS Service */ private static void checkForAwsServiceConstants(String namespace) { - boolean knowBaseException = - AWS_SERVICE_NAMESPACE_TO_BASE_EXCEPTION.containsKey(namespace); - if (!knowBaseException) { - throw new IllegalArgumentException( - "Polymorph does not know this service's Base Exception: %s".formatted( - namespace - ) - ); - } - boolean knowClientInterface = - AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.containsKey(namespace); - if (!knowClientInterface) { - throw new IllegalArgumentException( - "Polymorph does not know this service's Client Interface: %s".formatted( - namespace - ) - ); - } } /** @@ -136,7 +119,8 @@ public static ClassName classNameForServiceClient(ServiceShape shape) { checkForAwsServiceConstants(awsServiceSmithyNamespace); return ClassName.get( packageNameForAwsSdkV2Shape(shape), - AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.get(awsServiceSmithyNamespace) + AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.getOrDefault(awsServiceSmithyNamespace, + shape.expectTrait(ServiceTrait.class).getSdkId() + "Client") ); } @@ -384,8 +368,9 @@ public ClassName classNameForService(final ServiceShape shape) { checkInServiceNamespace(shape.getId()); return ClassName.get( packageName, - AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.get( - serviceShape.getId().getNamespace() + AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.getOrDefault( + serviceShape.getId().getNamespace(), + shape.expectTrait(ServiceTrait.class).getSdkId() + "Client" ) ); } @@ -399,8 +384,9 @@ public ClassName classNameForService(final ServiceShape shape) { public ClassName baseErrorForService() { return ClassName.get( modelPackage, - AWS_SERVICE_NAMESPACE_TO_BASE_EXCEPTION.get( - serviceShape.getId().getNamespace() + AWS_SERVICE_NAMESPACE_TO_BASE_EXCEPTION.getOrDefault( + serviceShape.getId().getNamespace(), + serviceShape.expectTrait(ServiceTrait.class).getSdkId() + "Exception" ) ); } From 801808123f4ea4c3b17b872c92700def98842ab8 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Sun, 14 Jul 2024 10:01:20 -0700 Subject: [PATCH 02/26] Fix a case that needs pascalCase --- .../nameresolver/AwsSdkNativeV1.java | 4 ++-- .../nameresolver/AwsSdkNativeV2.java | 24 ++++++++++++++++--- .../smithyjava/nameresolver/Native.java | 14 +++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV1.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV1.java index 1c79f5428..3b0ee0ba0 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV1.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV1.java @@ -164,7 +164,7 @@ public ParameterizedTypeName typeForListSetOrMapNoEnum( }; } - public static ClassName classNameForAwsSdkShape(final Shape shape) { + public ClassName classNameForAwsSdkShape(final Shape shape) { return ClassName.get( defaultModelPackageName(packageNameForAwsSdkV1Shape(shape)), StringUtils.capitalize(shape.getId().getName()) @@ -187,7 +187,7 @@ public ClassName classNameForStructure(final Shape shape) { } // check if this Shape is in AWS SDK for Java V1 package if (AwsSdkNameResolverHelpers.isInAwsSdkNamespace(shape.getId())) { - AwsSdkNativeV1.classNameForAwsSdkShape(shape); + return classNameForAwsSdkShape(shape); } return super.classNameForStructure(shape); } diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index e220bc4f4..88e37cd25 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -17,11 +17,13 @@ import java.util.Set; import software.amazon.awssdk.codegen.model.service.ServiceModel; import software.amazon.awssdk.codegen.naming.DefaultNamingStrategy; +import software.amazon.awssdk.utils.internal.CodegenNamingUtils; import software.amazon.polymorph.smithyjava.generator.CodegenSubject; import software.amazon.polymorph.smithyjava.generator.awssdk.v2.JavaAwsSdkV2; import software.amazon.polymorph.utils.AwsSdkNameResolverHelpers; import software.amazon.smithy.aws.traits.ServiceTrait; import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.knowledge.OperationIndex; import software.amazon.smithy.model.shapes.MemberShape; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.Shape; @@ -42,6 +44,7 @@ public class AwsSdkNativeV2 extends Native { private final DefaultNamingStrategy awsSDKNaming; + private final OperationIndex operationIndex; public AwsSdkNativeV2(final ServiceShape serviceShape, final Model model) { super( @@ -53,6 +56,7 @@ public AwsSdkNativeV2(final ServiceShape serviceShape, final Model model) { ); checkForAwsServiceConstants(); awsSDKNaming = new DefaultNamingStrategy(new ServiceModel(), null); + operationIndex = new OperationIndex(model); } // The values of these maps are NOT in smithy models and thus must be hard-coded @@ -257,16 +261,30 @@ public CodeBlock fieldForSetMember(MemberShape shape) { return CodeBlock.of("$L", shape.getMemberName().toLowerCase()); } - return CodeBlock.of("$L", uncapitalize(shape.getMemberName())); + return CodeBlock.of("$L", CodegenNamingUtils.lowercaseFirstChar(CodegenNamingUtils.pascalCase(shape.getMemberName()))); } - public static ClassName classNameForAwsSdkShape(final Shape shape) { + public ClassName classNameForAwsSdkShape(final Shape shape) { // Assume that the shape is in the model package ClassName smithyName = ClassName.get( defaultModelPackageName(packageNameForAwsSdkV2Shape(shape)), StringUtils.capitalize(shape.getId().getName()) ); + if (operationIndex.isInputStructure(shape)) { + return ClassName.get( + defaultModelPackageName(packageNameForAwsSdkV2Shape(shape)), + CodegenNamingUtils.pascalCase(shape.getId().getName()) + ); + } + + if (operationIndex.isOutputStructure(shape)) { + return ClassName.get( + defaultModelPackageName(packageNameForAwsSdkV2Shape(shape)), + CodegenNamingUtils.pascalCase(shape.getId().getName()) + ); + } + if (smithyName.simpleName().endsWith("Input")) { return ClassName.get( smithyName.packageName(), @@ -336,7 +354,7 @@ public ClassName classNameForStructure(final Shape shape) { } // check if this Shape is in AWS SDK for Java V2 package if (AwsSdkNameResolverHelpers.isInAwsSdkNamespace(shape.getId())) { - AwsSdkNativeV2.classNameForAwsSdkShape(shape); + return classNameForAwsSdkShape(shape); } return super.classNameForStructure(shape); } diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java index 0de0dacbb..fa2a7129b 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java @@ -193,10 +193,7 @@ public ClassName classForStringOrEnum(final Shape shape) { // This case must be first because shape can be an @enum string, or a Smithy 2.0 enum if (shape.hasTrait(EnumTrait.class)) { if (AwsSdkNameResolverHelpers.isInAwsSdkNamespace(shape.getId())) { - return switch (awsSdkVersion) { - case V2 -> AwsSdkNativeV2.classNameForAwsSdkShape(shape); - case V1 -> AwsSdkNativeV1.classNameForAwsSdkShape(shape); - }; + return classNameForAwsSdkShape(shape); } return classForEnum(shape); } @@ -264,10 +261,7 @@ public ClassName classNameForStructure(Shape shape) { return classNameForInterfaceOrLocalService(rShape, this.awsSdkVersion); } if (AwsSdkNameResolverHelpers.isInAwsSdkNamespace(shape.getId())) { - return switch (awsSdkVersion) { - case V2 -> AwsSdkNativeV2.classNameForAwsSdkShape(shape); - case V1 -> AwsSdkNativeV1.classNameForAwsSdkShape(shape); - }; + return classNameForAwsSdkShape(shape); } if (isInServiceNameSpace(shape.getId())) { return ClassName.get(modelPackage, shape.getId().getName()); @@ -378,4 +372,8 @@ public ClassName baseErrorForService() { "Use `ClassName.get(RuntimeException.class)` instead." ); } + + protected ClassName classNameForAwsSdkShape(final Shape shape) { + throw new UnsupportedOperationException(); + } } From 19a7bdccceda9810d48a455c89ca078612230972 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Sat, 3 Aug 2024 10:52:23 -0700 Subject: [PATCH 03/26] Another case --- .../polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java index 2d7451444..ceed39e9d 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java @@ -9,6 +9,7 @@ import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.TypeName; +import software.amazon.awssdk.utils.internal.CodegenNamingUtils; import software.amazon.polymorph.smithydafny.DafnyNameResolver; import software.amazon.polymorph.smithydafny.DafnyVersion; import software.amazon.polymorph.smithyjava.generator.CodegenSubject; @@ -118,7 +119,7 @@ public CodeBlock methodForGetMember( return CodeBlock.of( "$L.$L()", variableName, - uncapitalize(memberShape.getMemberName()) + CodegenNamingUtils.lowercaseFirstChar(CodegenNamingUtils.pascalCase(memberShape.getMemberName())) ); } From 7c53e2a670d36189504100683f3fb6f4aceabf7f Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Sat, 3 Aug 2024 11:03:45 -0700 Subject: [PATCH 04/26] Avoid typeforListSetOrMapNoEnum for now --- .../polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java index 230aa1e15..f39828f85 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java @@ -546,7 +546,7 @@ protected MethodSpec modeledList(ListShape shape) { ParameterSpec parameterSpec = ParameterSpec .builder( - subject.nativeNameResolver.typeForListSetOrMapNoEnum(shape.getId()), + subject.nativeNameResolver.typeForShape(shape.getId()), "nativeValue" ) .build(); From ea422d1cf89b57d754fd60e76552668dd5e05805 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Sat, 3 Aug 2024 11:13:36 -0700 Subject: [PATCH 05/26] More correct unCapitalize --- .../polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java | 3 ++- .../polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java index ceed39e9d..c1f4bfb7c 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java @@ -9,6 +9,7 @@ import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.TypeName; +import software.amazon.awssdk.codegen.internal.Utils; import software.amazon.awssdk.utils.internal.CodegenNamingUtils; import software.amazon.polymorph.smithydafny.DafnyNameResolver; import software.amazon.polymorph.smithydafny.DafnyVersion; @@ -119,7 +120,7 @@ public CodeBlock methodForGetMember( return CodeBlock.of( "$L.$L()", variableName, - CodegenNamingUtils.lowercaseFirstChar(CodegenNamingUtils.pascalCase(memberShape.getMemberName())) + Utils.unCapitalize(memberShape.getMemberName()) ); } diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index 88e37cd25..c72a0bdad 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -15,6 +15,8 @@ import java.util.Map; import java.util.Optional; import java.util.Set; + +import software.amazon.awssdk.codegen.internal.Utils; import software.amazon.awssdk.codegen.model.service.ServiceModel; import software.amazon.awssdk.codegen.naming.DefaultNamingStrategy; import software.amazon.awssdk.utils.internal.CodegenNamingUtils; @@ -261,7 +263,7 @@ public CodeBlock fieldForSetMember(MemberShape shape) { return CodeBlock.of("$L", shape.getMemberName().toLowerCase()); } - return CodeBlock.of("$L", CodegenNamingUtils.lowercaseFirstChar(CodegenNamingUtils.pascalCase(shape.getMemberName()))); + return CodeBlock.of("$L", Utils.unCapitalize(shape.getMemberName())); } public ClassName classNameForAwsSdkShape(final Shape shape) { From a04a1bd058cea284500825c4e9d7d771695ba1ee Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Sat, 3 Aug 2024 11:15:23 -0700 Subject: [PATCH 06/26] Disable Input -> Request rules for now (should be looking at operation name instead) --- .../nameresolver/AwsSdkNativeV2.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index c72a0bdad..e663bdf02 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -286,26 +286,26 @@ public ClassName classNameForAwsSdkShape(final Shape shape) { CodegenNamingUtils.pascalCase(shape.getId().getName()) ); } - - if (smithyName.simpleName().endsWith("Input")) { - return ClassName.get( - smithyName.packageName(), - smithyName - .simpleName() - .substring(0, smithyName.simpleName().lastIndexOf("Input")) + - "Request" - ); - } - - if (smithyName.simpleName().endsWith("Output")) { - return ClassName.get( - smithyName.packageName(), - smithyName - .simpleName() - .substring(0, smithyName.simpleName().lastIndexOf("Output")) + - "Response" - ); - } +// +// if (smithyName.simpleName().endsWith("Input")) { +// return ClassName.get( +// smithyName.packageName(), +// smithyName +// .simpleName() +// .substring(0, smithyName.simpleName().lastIndexOf("Input")) + +// "Request" +// ); +// } +// +// if (smithyName.simpleName().endsWith("Output")) { +// return ClassName.get( +// smithyName.packageName(), +// smithyName +// .simpleName() +// .substring(0, smithyName.simpleName().lastIndexOf("Output")) + +// "Response" +// ); +// } if (shape.hasTrait(ErrorTrait.class)) { if (smithyName.simpleName().contains("KMS")) { From d1548c7b63b9ae1d25324684a17cfef41c58d030 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Tue, 6 Aug 2024 15:57:25 -0700 Subject: [PATCH 07/26] Enable glue and lakeformation in CI on Java --- .../software/amazon/polymorph/smithyjava/JavaTestModels.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithyjava/JavaTestModels.java b/codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithyjava/JavaTestModels.java index f6081c510..2726773d6 100644 --- a/codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithyjava/JavaTestModels.java +++ b/codegen/smithy-dafny-codegen-test/src/test/java/software/amazon/polymorph/smithyjava/JavaTestModels.java @@ -38,8 +38,6 @@ class JavaTestModels extends TestModelTest { DISABLED_TESTS.add("SimpleTypes/SimpleString"); DISABLED_TESTS.add("SimpleTypes/SimpleTimestamp"); DISABLED_TESTS.add("Union"); - DISABLED_TESTS.add("aws-sdks/glue"); - DISABLED_TESTS.add("aws-sdks/lakeformation"); DISABLED_TESTS.add("aws-sdks/kms-lite"); DISABLED_TESTS.add("aws-sdks/sqs"); DISABLED_TESTS.add("aws-sdks/sqs-via-cli"); From b997983f2dd9e7c084c2e94ec0ca3e4c1d22c5ca Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Tue, 6 Aug 2024 16:46:17 -0700 Subject: [PATCH 08/26] operation + Request/Response instead --- .../nameresolver/AwsSdkNativeV2.java | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index e663bdf02..2e1b09d33 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -274,38 +274,28 @@ public ClassName classNameForAwsSdkShape(final Shape shape) { ); if (operationIndex.isInputStructure(shape)) { + var operations = operationIndex.getInputBindings(shape); + if (operations.size() > 1) { + throw new IllegalArgumentException("Structures bound to more than one operation as input are not supported: " + shape); + } + var operation = operations.stream().findFirst().get(); return ClassName.get( - defaultModelPackageName(packageNameForAwsSdkV2Shape(shape)), - CodegenNamingUtils.pascalCase(shape.getId().getName()) + smithyName.packageName(), + CodegenNamingUtils.pascalCase(operation.getId().getName()) + "Request" ); } if (operationIndex.isOutputStructure(shape)) { + var operations = operationIndex.getOutputBindings(shape); + if (operations.size() > 1) { + throw new IllegalArgumentException("Structures bound to more than one operation as output are not supported: " + shape); + } + var operation = operations.stream().findFirst().get(); return ClassName.get( - defaultModelPackageName(packageNameForAwsSdkV2Shape(shape)), - CodegenNamingUtils.pascalCase(shape.getId().getName()) + smithyName.packageName(), + CodegenNamingUtils.pascalCase(operation.getId().getName()) + "Response" ); } -// -// if (smithyName.simpleName().endsWith("Input")) { -// return ClassName.get( -// smithyName.packageName(), -// smithyName -// .simpleName() -// .substring(0, smithyName.simpleName().lastIndexOf("Input")) + -// "Request" -// ); -// } -// -// if (smithyName.simpleName().endsWith("Output")) { -// return ClassName.get( -// smithyName.packageName(), -// smithyName -// .simpleName() -// .substring(0, smithyName.simpleName().lastIndexOf("Output")) + -// "Response" -// ); -// } if (shape.hasTrait(ErrorTrait.class)) { if (smithyName.simpleName().contains("KMS")) { From 9a2107f8d959b4f00a2ba0304b4952d3f79a52a0 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Tue, 6 Aug 2024 21:42:10 -0700 Subject: [PATCH 09/26] Try different solution to list of enums --- .../polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java | 2 +- .../polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java index f39828f85..230aa1e15 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2.java @@ -546,7 +546,7 @@ protected MethodSpec modeledList(ListShape shape) { ParameterSpec parameterSpec = ParameterSpec .builder( - subject.nativeNameResolver.typeForShape(shape.getId()), + subject.nativeNameResolver.typeForListSetOrMapNoEnum(shape.getId()), "nativeValue" ) .build(); diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index 2e1b09d33..d906c6068 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -152,7 +152,7 @@ private TypeName typeForShapeNoEnum(ShapeId shapeId) { final Shape shape = model.expectShape(shapeId); if (shape.hasTrait(EnumTrait.class)) { - if (shapeRequiresTypeConversionFromStringToStructure(shapeId)) { + if (true || shapeRequiresTypeConversionFromStringToStructure(shapeId)) { return classForEnum(shape); } From d9c08d255cb3ac131d1f9c09536f0409868497fd Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Wed, 7 Aug 2024 08:47:36 -0700 Subject: [PATCH 10/26] Fix most unit tests --- .../smithyjava/nameresolver/AwsSdkNativeV2.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index d906c6068..f020f5642 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -126,7 +126,7 @@ public static ClassName classNameForServiceClient(ServiceShape shape) { return ClassName.get( packageNameForAwsSdkV2Shape(shape), AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.getOrDefault(awsServiceSmithyNamespace, - shape.expectTrait(ServiceTrait.class).getSdkId() + "Client") + sdkId(shape) + "Client") ); } @@ -367,6 +367,12 @@ public final String v2FormattedEnumValue( return this.awsSDKNaming.getEnumValueName(enumValueName); } + private static String sdkId(ServiceShape serviceShape) { + return serviceShape.getTrait(ServiceTrait.class) + .map(serviceTrait -> serviceTrait.getSdkId()) + .orElse(serviceShape.getId().getName()); + } + /** * Returns the TypeName for an AWS Service's Client Interface. */ @@ -380,7 +386,7 @@ public ClassName classNameForService(final ServiceShape shape) { packageName, AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.getOrDefault( serviceShape.getId().getNamespace(), - shape.expectTrait(ServiceTrait.class).getSdkId() + "Client" + sdkId(serviceShape) + "Client" ) ); } @@ -396,7 +402,7 @@ public ClassName baseErrorForService() { modelPackage, AWS_SERVICE_NAMESPACE_TO_BASE_EXCEPTION.getOrDefault( serviceShape.getId().getNamespace(), - serviceShape.expectTrait(ServiceTrait.class).getSdkId() + "Exception" + sdkId(serviceShape) + "Exception" ) ); } From dd9f3088914ac95b34830f09db0f3977dea04c7c Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Thu, 3 Oct 2024 14:55:32 -0700 Subject: [PATCH 11/26] Makefile tweak --- SmithyDafnyMakefile.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/SmithyDafnyMakefile.mk b/SmithyDafnyMakefile.mk index 636b877fe..08bde70cb 100644 --- a/SmithyDafnyMakefile.mk +++ b/SmithyDafnyMakefile.mk @@ -525,6 +525,7 @@ transpile_test_java: _transpile_test_all _mv_test_java # To avoid `java/implementation-java` the code is generated and then moved. _mv_implementation_java: rm -rf runtimes/java/src/main/dafny-generated + mkdir -p runtimes/java/src/main mv runtimes/java/ImplementationFromDafny-java runtimes/java/src/main/dafny-generated _mv_test_java: rm -rf runtimes/java/src/test/dafny-generated From e5573806591e1d014b92bed758f9b71ab0c77af4 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Thu, 3 Oct 2024 21:33:01 -0700 Subject: [PATCH 12/26] Filter out operation that uses streaming, typo --- SmithyDafnyMakefile.mk | 2 +- TestModels/aws-sdks/lakeformation/Makefile | 7 ++ .../aws-sdks/lakeformation/build.gradle.kts | 70 +++++++++++++++++++ .../aws-sdks/lakeformation/gradle.properties | 3 + .../aws-sdks/lakeformation/smithy-build.json | 28 ++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 TestModels/aws-sdks/lakeformation/build.gradle.kts create mode 100644 TestModels/aws-sdks/lakeformation/gradle.properties create mode 100644 TestModels/aws-sdks/lakeformation/smithy-build.json diff --git a/SmithyDafnyMakefile.mk b/SmithyDafnyMakefile.mk index 08bde70cb..c984f5851 100644 --- a/SmithyDafnyMakefile.mk +++ b/SmithyDafnyMakefile.mk @@ -450,7 +450,7 @@ _polymorph_rust: $(if $(RUST_BENERATED), , _polymorph) ########################## .NET targets -net: polymorph_dafny transpile_net polymorph_net test_net +net: polymorph_dafny transpile_net polymorph_dotnet test_net transpile_net: $(if $(ENABLE_EXTERN_PROCESSING), _with_extern_pre_transpile, ) transpile_net: | transpile_implementation_net transpile_test_net transpile_dependencies_net diff --git a/TestModels/aws-sdks/lakeformation/Makefile b/TestModels/aws-sdks/lakeformation/Makefile index 82ec6a18f..9b69cc4fb 100644 --- a/TestModels/aws-sdks/lakeformation/Makefile +++ b/TestModels/aws-sdks/lakeformation/Makefile @@ -23,6 +23,13 @@ AWS_SDK_CMD=--aws-sdk POLYMORPH_OPTIONS=--generate project-files --generate client-constructors +# Override calling the polymorph CLI to invoke the Smithy build instead +_polymorph_dafny: + $(GRADLEW) polymorphDafny + +_polymorph_java: + $(GRADLEW) polymorphJava + # There is no wrapped target for aws-sdk types _polymorph_wrapped: ; _polymorph_wrapped_dafny: ; diff --git a/TestModels/aws-sdks/lakeformation/build.gradle.kts b/TestModels/aws-sdks/lakeformation/build.gradle.kts new file mode 100644 index 000000000..8a276c024 --- /dev/null +++ b/TestModels/aws-sdks/lakeformation/build.gradle.kts @@ -0,0 +1,70 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +plugins { + id("software.amazon.smithy").version("0.6.0") +} + +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + implementation("software.amazon.smithy:smithy-model:1.28.0") + implementation("software.amazon.smithy:smithy-aws-traits:1.28.0") + implementation("software.amazon.smithy:smithy-rules-engine:1.28.0") + + // Must be built and published to the local Maven repo + implementation("software.amazon.smithy.dafny:smithy-dafny-codegen:0.1.0") +} + +tasks.register("polymorphDafny") { + dependsOn("build") + doLast { + // if needed, specify a projection to use instead + // default (no projection) is "source" + val projectionName = "operation-subset" + copy { + from(layout.buildDirectory.dir("smithyprojections/" + project.name + "/" + projectionName + "/dafny-client-codegen/project.properties")) + into(".") + } + copy { + from(layout.buildDirectory.dir("smithyprojections/" + project.name + "/" + projectionName + "/dafny-client-codegen/Model/")) + into("model") + } + exec { + // need to adjust the relative import, since we're copying it away + // the commandLine method does not play nice with sed, + // so we have to execute it through bash :( + commandLine("bash", "-c", "sed '4s|../../../../../../../../dafny-dependencies/StandardLibrary/src/Index.dfy|../../../dafny-dependencies/StandardLibrary/src/Index.dfy|' model/ComAmazonawsLakeformationTypes.dfy > model/tmp && mv model/tmp model/ComAmazonawsLakeformationTypes.dfy") + } + } +} + +tasks.register("polymorphJava") { + dependsOn("build") + doLast { + // if needed, specify a projection to use instead + // default (no projection) is "source" + val projectionName = "operation-subset" + // We can't just copy runtimes/java over unfortunately, + // because we need a fresher software.amazon.awssdk:kms version + // than what's in the template. + copy { + from(layout.buildDirectory.dir("smithyprojections/" + project.name + "/" + projectionName + "/dafny-client-codegen/runtimes/java/src")) + into("runtimes/java/src") + } + } +} + +buildscript { + val smithyVersion: String by project + + repositories { + mavenCentral() + } + dependencies { + "classpath"("software.amazon.smithy:smithy-cli:$smithyVersion") + } +} diff --git a/TestModels/aws-sdks/lakeformation/gradle.properties b/TestModels/aws-sdks/lakeformation/gradle.properties new file mode 100644 index 000000000..e2e5d4198 --- /dev/null +++ b/TestModels/aws-sdks/lakeformation/gradle.properties @@ -0,0 +1,3 @@ +smithyVersion=1.27.2 +smithyGradleVersion=0.6.0 + diff --git a/TestModels/aws-sdks/lakeformation/smithy-build.json b/TestModels/aws-sdks/lakeformation/smithy-build.json new file mode 100644 index 000000000..e8e8cbed1 --- /dev/null +++ b/TestModels/aws-sdks/lakeformation/smithy-build.json @@ -0,0 +1,28 @@ +{ + "version": "1.0", + "projections": { + "operation-subset": { + "transforms": [ + { + "name": "excludeShapesBySelector", + "args": { + "selector": "operation [id|name = GetWorkUnitResults]" + } + }, + { + "name": "removeUnusedShapes", + "args": {} + } + ], + "plugins": { + "dafny-client-codegen": { + "edition": "2023", + "service": "com.amazonaws.lakeformation#AWSLakeFormation", + "dafnyVersion": "${DAFNY_VERSION}", + "targetLanguages": ["java"], + "includeDafnyFile": "../../dafny-dependencies/StandardLibrary/src/Index.dfy" + } + } + } + } +} From ec2b23dfbeb2eba71d951b8c1797415e913fb4d2 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 4 Oct 2024 09:40:51 -0700 Subject: [PATCH 13/26] Fix dotnet --- TestModels/aws-sdks/lakeformation/Makefile | 6 ++++++ .../aws-sdks/lakeformation/build.gradle.kts | 19 +++++++++++++++++++ .../aws-sdks/lakeformation/smithy-build.json | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/TestModels/aws-sdks/lakeformation/Makefile b/TestModels/aws-sdks/lakeformation/Makefile index 9b69cc4fb..bf3b1a8e7 100644 --- a/TestModels/aws-sdks/lakeformation/Makefile +++ b/TestModels/aws-sdks/lakeformation/Makefile @@ -18,6 +18,8 @@ SMITHY_DEPS=dafny-dependencies/Model/traits.smithy AWS_SDK_CMD=--aws-sdk +SMITHY_MODEL_ROOT := $(LIBRARY_ROOT)/model + # This project has no dependencies # DEPENDENT-MODELS:= @@ -27,10 +29,14 @@ POLYMORPH_OPTIONS=--generate project-files --generate client-constructors _polymorph_dafny: $(GRADLEW) polymorphDafny +_polymorph_dotnet: + $(GRADLEW) polymorphDotnet + _polymorph_java: $(GRADLEW) polymorphJava # There is no wrapped target for aws-sdk types +_polymorph: ; _polymorph_wrapped: ; _polymorph_wrapped_dafny: ; _polymorph_wrapped_net: ; diff --git a/TestModels/aws-sdks/lakeformation/build.gradle.kts b/TestModels/aws-sdks/lakeformation/build.gradle.kts index 8a276c024..f62169f71 100644 --- a/TestModels/aws-sdks/lakeformation/build.gradle.kts +++ b/TestModels/aws-sdks/lakeformation/build.gradle.kts @@ -42,6 +42,25 @@ tasks.register("polymorphDafny") { } } +tasks.register("polymorphDotnet") { + dependsOn("build") + doLast { + // if needed, specify a projection to use instead + // default (no projection) is "source" + val projectionName = "operation-subset" + copy { + from(layout.buildDirectory.dir("smithyprojections/" + project.name + "/" + projectionName + "/dafny-client-codegen/runtimes/net")) + into("runtimes/net") + } + exec { + // need to adjust the relative import, since we're copying it away + // the commandLine method does not play nice with sed, + // so we have to execute it through bash :( + commandLine("bash", "-c", "sed 's|../../../../../../../../../dafny-dependencies/StandardLibrary/runtimes/net/STD.csproj|../../../../dafny-dependencies/StandardLibrary/runtimes/net/STD.csproj|' runtimes/net/LakeFormation.csproj > runtimes/net/tmp && mv runtimes/net/tmp runtimes/net/LakeFormation.csproj") + } + } +} + tasks.register("polymorphJava") { dependsOn("build") doLast { diff --git a/TestModels/aws-sdks/lakeformation/smithy-build.json b/TestModels/aws-sdks/lakeformation/smithy-build.json index e8e8cbed1..e820c071d 100644 --- a/TestModels/aws-sdks/lakeformation/smithy-build.json +++ b/TestModels/aws-sdks/lakeformation/smithy-build.json @@ -19,7 +19,7 @@ "edition": "2023", "service": "com.amazonaws.lakeformation#AWSLakeFormation", "dafnyVersion": "${DAFNY_VERSION}", - "targetLanguages": ["java"], + "targetLanguages": ["dotnet", "java"], "includeDafnyFile": "../../dafny-dependencies/StandardLibrary/src/Index.dfy" } } From b0ae9f932018290bcc58ca08e895a2b488d12495 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 4 Oct 2024 10:16:14 -0700 Subject: [PATCH 14/26] Rename Model to model (1/2) --- .../lakeformation/{Model => Model-tmp}/lakeformation.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename TestModels/aws-sdks/lakeformation/{Model => Model-tmp}/lakeformation.json (100%) diff --git a/TestModels/aws-sdks/lakeformation/Model/lakeformation.json b/TestModels/aws-sdks/lakeformation/Model-tmp/lakeformation.json similarity index 100% rename from TestModels/aws-sdks/lakeformation/Model/lakeformation.json rename to TestModels/aws-sdks/lakeformation/Model-tmp/lakeformation.json From 0394f5329f08add61b4a693b3a5bd1df01f85486 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 4 Oct 2024 10:16:39 -0700 Subject: [PATCH 15/26] Rename Model to model (2/2) --- .../lakeformation/{Model-tmp => model}/lakeformation.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename TestModels/aws-sdks/lakeformation/{Model-tmp => model}/lakeformation.json (100%) diff --git a/TestModels/aws-sdks/lakeformation/Model-tmp/lakeformation.json b/TestModels/aws-sdks/lakeformation/model/lakeformation.json similarity index 100% rename from TestModels/aws-sdks/lakeformation/Model-tmp/lakeformation.json rename to TestModels/aws-sdks/lakeformation/model/lakeformation.json From 23089b550b61cc44823cb7c9033d5f4f5aedc0f2 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 4 Oct 2024 10:29:29 -0700 Subject: [PATCH 16/26] Fixes --- TestModels/aws-sdks/lakeformation/src/Index.dfy | 2 +- .../smithyjava/generator/awssdk/v2/ToDafnyAwsV2Constants.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/TestModels/aws-sdks/lakeformation/src/Index.dfy b/TestModels/aws-sdks/lakeformation/src/Index.dfy index 6d4f80d63..c0304b6d1 100644 --- a/TestModels/aws-sdks/lakeformation/src/Index.dfy +++ b/TestModels/aws-sdks/lakeformation/src/Index.dfy @@ -1,6 +1,6 @@ // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -include "../Model/ComAmazonawsLakeformationTypes.dfy" +include "../model/ComAmazonawsLakeformationTypes.dfy" module {:options "--function-syntax:4"}{:extern "software.amazon.cryptography.services.lakeformation.internaldafny"} Com.Amazonaws.LakeFormation refines AbstractComAmazonawsLakeformationService { diff --git a/codegen/smithy-dafny-codegen/src/test/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2Constants.java b/codegen/smithy-dafny-codegen/src/test/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2Constants.java index 585c0a10f..79c281cf8 100644 --- a/codegen/smithy-dafny-codegen/src/test/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2Constants.java +++ b/codegen/smithy-dafny-codegen/src/test/java/software/amazon/polymorph/smithyjava/generator/awssdk/v2/ToDafnyAwsV2Constants.java @@ -47,7 +47,7 @@ public static software.amazon.cryptography.services.kms.internaldafny.types.KeyU protected static String GENERATE_CONVERT_LIST = """ public static dafny.DafnySequence KeyUsageTypes ( - java.util.List nativeValue + java.util.List nativeValue ) { return software.amazon.smithy.dafny.conversion.ToDafny.Aggregate.GenericToSequence( nativeValue, From d94e8fdebd248d67fe019c055b37ba9a4a052b50 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 4 Oct 2024 10:30:19 -0700 Subject: [PATCH 17/26] Formatting --- .../nameresolver/AwsSdkDafnyV2.java | 2 +- .../nameresolver/AwsSdkNativeV2.java | 37 +++++++++++-------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java index c1f4bfb7c..6ad6fda12 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkDafnyV2.java @@ -120,7 +120,7 @@ public CodeBlock methodForGetMember( return CodeBlock.of( "$L.$L()", variableName, - Utils.unCapitalize(memberShape.getMemberName()) + Utils.unCapitalize(memberShape.getMemberName()) ); } diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index f020f5642..25b6cb318 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -15,7 +15,6 @@ import java.util.Map; import java.util.Optional; import java.util.Set; - import software.amazon.awssdk.codegen.internal.Utils; import software.amazon.awssdk.codegen.model.service.ServiceModel; import software.amazon.awssdk.codegen.naming.DefaultNamingStrategy; @@ -103,8 +102,7 @@ private void checkForAwsServiceConstants() { } /** Validates that Polymorph knows non-smithy modeled constants for an AWS Service */ - private static void checkForAwsServiceConstants(String namespace) { - } + private static void checkForAwsServiceConstants(String namespace) {} /** * Throws IllegalArgumentException if shapeId is not in namespace @@ -125,8 +123,10 @@ public static ClassName classNameForServiceClient(ServiceShape shape) { checkForAwsServiceConstants(awsServiceSmithyNamespace); return ClassName.get( packageNameForAwsSdkV2Shape(shape), - AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.getOrDefault(awsServiceSmithyNamespace, - sdkId(shape) + "Client") + AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.getOrDefault( + awsServiceSmithyNamespace, + sdkId(shape) + "Client" + ) ); } @@ -276,24 +276,30 @@ public ClassName classNameForAwsSdkShape(final Shape shape) { if (operationIndex.isInputStructure(shape)) { var operations = operationIndex.getInputBindings(shape); if (operations.size() > 1) { - throw new IllegalArgumentException("Structures bound to more than one operation as input are not supported: " + shape); + throw new IllegalArgumentException( + "Structures bound to more than one operation as input are not supported: " + + shape + ); } var operation = operations.stream().findFirst().get(); return ClassName.get( - smithyName.packageName(), - CodegenNamingUtils.pascalCase(operation.getId().getName()) + "Request" + smithyName.packageName(), + CodegenNamingUtils.pascalCase(operation.getId().getName()) + "Request" ); } if (operationIndex.isOutputStructure(shape)) { var operations = operationIndex.getOutputBindings(shape); if (operations.size() > 1) { - throw new IllegalArgumentException("Structures bound to more than one operation as output are not supported: " + shape); + throw new IllegalArgumentException( + "Structures bound to more than one operation as output are not supported: " + + shape + ); } var operation = operations.stream().findFirst().get(); return ClassName.get( - smithyName.packageName(), - CodegenNamingUtils.pascalCase(operation.getId().getName()) + "Response" + smithyName.packageName(), + CodegenNamingUtils.pascalCase(operation.getId().getName()) + "Response" ); } @@ -368,9 +374,10 @@ public final String v2FormattedEnumValue( } private static String sdkId(ServiceShape serviceShape) { - return serviceShape.getTrait(ServiceTrait.class) - .map(serviceTrait -> serviceTrait.getSdkId()) - .orElse(serviceShape.getId().getName()); + return serviceShape + .getTrait(ServiceTrait.class) + .map(serviceTrait -> serviceTrait.getSdkId()) + .orElse(serviceShape.getId().getName()); } /** @@ -386,7 +393,7 @@ public ClassName classNameForService(final ServiceShape shape) { packageName, AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.getOrDefault( serviceShape.getId().getNamespace(), - sdkId(serviceShape) + "Client" + sdkId(serviceShape) + "Client" ) ); } From 71107288ff15f3c7cc2d4ad611df890dc87b6c26 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 4 Oct 2024 11:40:03 -0700 Subject: [PATCH 18/26] Fix build file --- TestModels/aws-sdks/lakeformation/build.gradle.kts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TestModels/aws-sdks/lakeformation/build.gradle.kts b/TestModels/aws-sdks/lakeformation/build.gradle.kts index f62169f71..e73ba7e5a 100644 --- a/TestModels/aws-sdks/lakeformation/build.gradle.kts +++ b/TestModels/aws-sdks/lakeformation/build.gradle.kts @@ -71,8 +71,8 @@ tasks.register("polymorphJava") { // because we need a fresher software.amazon.awssdk:kms version // than what's in the template. copy { - from(layout.buildDirectory.dir("smithyprojections/" + project.name + "/" + projectionName + "/dafny-client-codegen/runtimes/java/src")) - into("runtimes/java/src") + from(layout.buildDirectory.dir("smithyprojections/" + project.name + "/" + projectionName + "/dafny-client-codegen/runtimes/java")) + into("runtimes/java") } } } From 496df4c3ddded139caf2e675c7800951315628d9 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 4 Oct 2024 13:27:01 -0700 Subject: [PATCH 19/26] Remove dead code --- .../nameresolver/AwsSdkNativeV2.java | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index 25b6cb318..b6d939fc9 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -152,11 +152,7 @@ private TypeName typeForShapeNoEnum(ShapeId shapeId) { final Shape shape = model.expectShape(shapeId); if (shape.hasTrait(EnumTrait.class)) { - if (true || shapeRequiresTypeConversionFromStringToStructure(shapeId)) { - return classForEnum(shape); - } - - return classForString(); + return classForEnum(shape); } if (SHAPE_TYPES_LIST_SET_MAP.contains(shape.getType())) { return typeForListSetOrMapNoEnum(shapeId); @@ -184,24 +180,6 @@ public TypeName typeForShape(final ShapeId shapeId) { return super.typeForShape(shapeId); } - /** - * Returns true if the provided ShapeId has type string in the Smithy model, but AWS SDK for - * Java V2 effectively expects type structure. - * @param shapeId - * @return true if AWS SDK for Java V2 expects this to have been modeled as a structure in Smithy - */ - protected boolean shapeRequiresTypeConversionFromStringToStructure( - ShapeId shapeId - ) { - return ( - shapeId - .toString() - .contains("com.amazonaws.kms#EncryptionAlgorithmSpec") || - shapeId.toString().contains("com.amazonaws.kms#SigningAlgorithmSpec") || - shapeId.toString().contains("com.amazonaws.kms#GrantOperation") - ); - } - @SuppressWarnings("OptionalGetWithoutIsPresent") public ParameterizedTypeName typeForListSetOrMapNoEnum( final ShapeId shapeId From 38193339e1526a9d2cf9796dc74808b3fe4a67ae Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Fri, 4 Oct 2024 13:33:59 -0700 Subject: [PATCH 20/26] Unused imports --- .../polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index b6d939fc9..4d6343097 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -5,7 +5,6 @@ import static software.amazon.polymorph.smithyjava.nameresolver.AwsSdkV2NameResolverUtils.isAttributeValueType; import static software.amazon.polymorph.smithyjava.nameresolver.AwsSdkV2NameResolverUtils.tokenToUncapitalizeInShape; import static software.amazon.polymorph.smithyjava.nameresolver.Constants.SHAPE_TYPES_LIST_SET_MAP; -import static software.amazon.smithy.utils.StringUtils.uncapitalize; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; @@ -33,7 +32,6 @@ import software.amazon.smithy.model.shapes.StructureShape; import software.amazon.smithy.model.traits.EnumTrait; import software.amazon.smithy.model.traits.ErrorTrait; -import software.amazon.smithy.model.traits.StringTrait; import software.amazon.smithy.model.traits.TitleTrait; import software.amazon.smithy.model.traits.TraitDefinition; import software.amazon.smithy.utils.StringUtils; From 0ff9a8ddf2f76d7a1bda0259a02b43fa30f31351 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Wed, 9 Oct 2024 08:56:54 -0700 Subject: [PATCH 21/26] Removing hooks for AWS service limitations These should be validations instead if we add some --- .../smithyjava/nameresolver/AwsSdkNativeV2.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java index 4d6343097..ff2a551ee 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/AwsSdkNativeV2.java @@ -53,7 +53,6 @@ public AwsSdkNativeV2(final ServiceShape serviceShape, final Model model) { defaultModelPackageName(packageNameForAwsSdkV2Shape(serviceShape)), CodegenSubject.AwsSdkVersion.V2 ); - checkForAwsServiceConstants(); awsSDKNaming = new DefaultNamingStrategy(new ServiceModel(), null); operationIndex = new OperationIndex(model); } @@ -93,15 +92,6 @@ public AwsSdkNativeV2(final ServiceShape serviceShape, final Model model) { ); } - /** Validates that Polymorph knows non-smithy modeled constants for an AWS Service */ - private void checkForAwsServiceConstants() { - String namespace = serviceShape.getId().getNamespace(); - checkForAwsServiceConstants(namespace); - } - - /** Validates that Polymorph knows non-smithy modeled constants for an AWS Service */ - private static void checkForAwsServiceConstants(String namespace) {} - /** * Throws IllegalArgumentException if shapeId is not in namespace */ @@ -118,7 +108,6 @@ private void checkInServiceNamespace(final ShapeId shapeId) { public static ClassName classNameForServiceClient(ServiceShape shape) { String awsServiceSmithyNamespace = shape.toShapeId().getNamespace(); - checkForAwsServiceConstants(awsServiceSmithyNamespace); return ClassName.get( packageNameForAwsSdkV2Shape(shape), AWS_SERVICE_NAMESPACE_TO_CLIENT_INTERFACE.getOrDefault( From f87f309c3d173b453159f7868ee7fe2f4f1bf69f Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Wed, 9 Oct 2024 09:02:38 -0700 Subject: [PATCH 22/26] Make Native abstract --- .../generator/library/JavaLibrary.java | 22 ++++++++++--------- .../smithyjava/nameresolver/Native.java | 6 ++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java index 41c9d5a7f..3cb305335 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java @@ -24,6 +24,8 @@ import software.amazon.polymorph.smithyjava.generator.awssdk.v2.ShimV2; import software.amazon.polymorph.smithyjava.generator.library.shims.ResourceShim; import software.amazon.polymorph.smithyjava.generator.library.shims.ServiceShim; +import software.amazon.polymorph.smithyjava.nameresolver.AwsSdkNativeV1; +import software.amazon.polymorph.smithyjava.nameresolver.AwsSdkNativeV2; import software.amazon.polymorph.smithyjava.nameresolver.Dafny; import software.amazon.polymorph.smithyjava.nameresolver.Native; import software.amazon.polymorph.traits.LocalServiceTrait; @@ -118,16 +120,16 @@ static Native initNative( ServiceShape serviceShape, AwsSdkVersion awsSdkVersion ) { - String packageName = NamespaceHelper.standardize( - serviceShape.getId().getNamespace() - ); - return new Native( - packageName, - serviceShape, - model, - packageName + ".model", - awsSdkVersion - ); + return switch (awsSdkVersion) { + case V1 -> new AwsSdkNativeV1( + serviceShape, + model + ); + case V2 -> new AwsSdkNativeV2( + serviceShape, + model + ); + }; } public static CodeBlock wrapAwsService( diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java index fa2a7129b..e7017793e 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java @@ -40,7 +40,7 @@ * model Shapes and generated identifiers in Java * for the native Java code. */ -public class Native extends NameResolver { +public abstract class Native extends NameResolver { protected static final Map< String, @@ -373,7 +373,5 @@ public ClassName baseErrorForService() { ); } - protected ClassName classNameForAwsSdkShape(final Shape shape) { - throw new UnsupportedOperationException(); - } + protected abstract ClassName classNameForAwsSdkShape(final Shape shape); } From 8a07215d805158a352d3d1756a319e16df54470b Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Wed, 9 Oct 2024 09:04:31 -0700 Subject: [PATCH 23/26] Formatting --- .../smithyjava/generator/library/JavaLibrary.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java index 3cb305335..10b670e31 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java @@ -121,14 +121,8 @@ static Native initNative( AwsSdkVersion awsSdkVersion ) { return switch (awsSdkVersion) { - case V1 -> new AwsSdkNativeV1( - serviceShape, - model - ); - case V2 -> new AwsSdkNativeV2( - serviceShape, - model - ); + case V1 -> new AwsSdkNativeV1(serviceShape, model); + case V2 -> new AwsSdkNativeV2(serviceShape, model); }; } From 69c149af89a71fcef28d997fc2a8a113077fdc57 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Wed, 9 Oct 2024 09:43:00 -0700 Subject: [PATCH 24/26] Revert "Make Native abstract" This reverts commit f87f309c3d173b453159f7868ee7fe2f4f1bf69f. # Conflicts: # codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java --- .../generator/library/JavaLibrary.java | 16 ++++++++++------ .../smithyjava/nameresolver/Native.java | 6 ++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java index 10b670e31..41c9d5a7f 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/generator/library/JavaLibrary.java @@ -24,8 +24,6 @@ import software.amazon.polymorph.smithyjava.generator.awssdk.v2.ShimV2; import software.amazon.polymorph.smithyjava.generator.library.shims.ResourceShim; import software.amazon.polymorph.smithyjava.generator.library.shims.ServiceShim; -import software.amazon.polymorph.smithyjava.nameresolver.AwsSdkNativeV1; -import software.amazon.polymorph.smithyjava.nameresolver.AwsSdkNativeV2; import software.amazon.polymorph.smithyjava.nameresolver.Dafny; import software.amazon.polymorph.smithyjava.nameresolver.Native; import software.amazon.polymorph.traits.LocalServiceTrait; @@ -120,10 +118,16 @@ static Native initNative( ServiceShape serviceShape, AwsSdkVersion awsSdkVersion ) { - return switch (awsSdkVersion) { - case V1 -> new AwsSdkNativeV1(serviceShape, model); - case V2 -> new AwsSdkNativeV2(serviceShape, model); - }; + String packageName = NamespaceHelper.standardize( + serviceShape.getId().getNamespace() + ); + return new Native( + packageName, + serviceShape, + model, + packageName + ".model", + awsSdkVersion + ); } public static CodeBlock wrapAwsService( diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java index e7017793e..fa2a7129b 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java @@ -40,7 +40,7 @@ * model Shapes and generated identifiers in Java * for the native Java code. */ -public abstract class Native extends NameResolver { +public class Native extends NameResolver { protected static final Map< String, @@ -373,5 +373,7 @@ public ClassName baseErrorForService() { ); } - protected abstract ClassName classNameForAwsSdkShape(final Shape shape); + protected ClassName classNameForAwsSdkShape(final Shape shape) { + throw new UnsupportedOperationException(); + } } From a8b7347832d198d102299a374ee203af2aa401e7 Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Wed, 9 Oct 2024 09:45:18 -0700 Subject: [PATCH 25/26] Add exception message instead --- .../amazon/polymorph/smithyjava/nameresolver/Native.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java index fa2a7129b..e931f52c9 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java @@ -374,6 +374,6 @@ public ClassName baseErrorForService() { } protected ClassName classNameForAwsSdkShape(final Shape shape) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("classNameForAwsSdkShape should only be called on AWS SDK-specific subclasses"); } } From 6edde6d95e6e24a04b35e297cb68e0f4cd9caf8f Mon Sep 17 00:00:00 2001 From: Robin Salkeld Date: Wed, 9 Oct 2024 09:49:09 -0700 Subject: [PATCH 26/26] Formatting --- .../amazon/polymorph/smithyjava/nameresolver/Native.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java index e931f52c9..01b313e68 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithyjava/nameresolver/Native.java @@ -374,6 +374,8 @@ public ClassName baseErrorForService() { } protected ClassName classNameForAwsSdkShape(final Shape shape) { - throw new UnsupportedOperationException("classNameForAwsSdkShape should only be called on AWS SDK-specific subclasses"); + throw new UnsupportedOperationException( + "classNameForAwsSdkShape should only be called on AWS SDK-specific subclasses" + ); } }