From 349eed84fb12c8d0587f03aed3016c46e2dee60b Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Sat, 24 Dec 2022 02:09:45 +0100 Subject: [PATCH 01/16] feat(codegen): allow module version in settings --- .../amazon/smithy/go/codegen/GoSettings.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java index 8c98eb602..88134a182 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java @@ -17,6 +17,7 @@ import java.util.Arrays; import java.util.Objects; +import java.util.Optional; import java.util.Set; import software.amazon.smithy.codegen.core.CodegenException; import software.amazon.smithy.model.Model; @@ -33,10 +34,12 @@ public final class GoSettings { private static final String SERVICE = "service"; private static final String MODULE_NAME = "module"; private static final String MODULE_DESCRIPTION = "moduleDescription"; + private static final String MODULE_VERSION = "moduleVersion"; private ShapeId service; private String moduleName; private String moduleDescription = ""; + private String moduleVersion; private ShapeId protocol; /** @@ -47,13 +50,13 @@ public final class GoSettings { */ public static GoSettings from(ObjectNode config) { GoSettings settings = new GoSettings(); - config.warnIfAdditionalProperties(Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION)); + config.warnIfAdditionalProperties(Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION)); settings.setService(config.expectStringMember(SERVICE).expectShapeId()); settings.setModuleName(config.expectStringMember(MODULE_NAME).getValue()); settings.setModuleDescription(config.getStringMemberOrDefault( MODULE_DESCRIPTION, settings.getModuleName() + " client")); - + settings.setModuleVersion(config.getStringMemberOrDefault(MODULE_VERSION, null)); return settings; } @@ -129,6 +132,26 @@ public void setModuleDescription(String moduleDescription) { this.moduleDescription = Objects.requireNonNull(moduleDescription); } + /** + * Gets the optional module version for the module that will be generated. + * + * @return Returns the module version. + */ + public Optional getModuleVersion() { + return Optional.ofNullable(moduleVersion); + } + + /** + * Sets the version of the module to generate. + * + * @param moduleVersion The version of the module to generate. + */ + public void setModuleVersion(String moduleVersion) { + if (moduleVersion != null) { + this.moduleVersion = moduleVersion; + } + } + /** * Gets the configured protocol to generate. * From d06e8c54a7aad9e7951494caea8d6bf9fccb4044 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Sat, 24 Dec 2022 02:11:18 +0100 Subject: [PATCH 02/16] feat(codegen): create go.mod file automatically --- .../smithy/go/codegen/ManifestWriter.java | 59 +++++++++++++++++-- .../amazon/smithy/go/codegen/go.mod.template | 7 +++ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java index a33713ee2..fd66463cd 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java @@ -38,6 +38,7 @@ import software.amazon.smithy.model.node.StringNode; import software.amazon.smithy.model.traits.UnstableTrait; import software.amazon.smithy.utils.BuilderRef; +import software.amazon.smithy.utils.IoUtils; import software.amazon.smithy.utils.SmithyBuilder; /** @@ -46,12 +47,17 @@ */ public final class ManifestWriter { private static final String GENERATED_JSON = "generated.json"; + private static final String GENERATED_GO_MOD = "go.mod"; + private static final String DEFAULT_MINIMUM_GO_VERSION = "1.15"; private final String moduleName; private final FileManifest fileManifest; private final List dependencies; private final Optional minimumGoVersion; private final boolean isUnstable; + private final boolean isGoModule; + + private Map minimumDependencies; private ManifestWriter(Builder builder) { moduleName = SmithyBuilder.requiredState("moduleName", builder.moduleName); @@ -59,6 +65,7 @@ private ManifestWriter(Builder builder) { dependencies = builder.dependencies.copy(); minimumGoVersion = builder.minimumGoVersion; isUnstable = builder.isUnstable; + isGoModule = builder.isGoModule; } /** @@ -80,6 +87,7 @@ public static void writeManifest( .fileManifest(fileManifest) .dependencies(dependencies) .isUnstable(settings.getService(model).getTrait(UnstableTrait.class).isPresent()) + .isGoModule(settings.getModuleVersion().isPresent()) .build() .writeManifest(); } @@ -100,12 +108,30 @@ public void writeManifest() { } fileManifest.addFile(manifestFile); - Node generatedJson = buildManifestFile(); + String minimumGoVersion = prepareDependencies(); + + Node generatedJson = buildManifestFile(minimumGoVersion); fileManifest.writeFile(manifestFile.toString(), Node.prettyPrintJson(generatedJson) + "\n"); + if (isGoModule) { + Path moduleFile = fileManifest.getBaseDir().resolve(GENERATED_GO_MOD); + + if (Files.exists(moduleFile)) { + try { + Files.delete(moduleFile); + } catch (IOException e) { + throw new CodegenException("Failed to delete existing " + GENERATED_GO_MOD + " file", e); + } + } + fileManifest.addFile(moduleFile); + + String moduleContent = buildModuleFile(minimumGoVersion); + fileManifest.writeFile(moduleFile.toString(), moduleContent); + } + } - private Node buildManifestFile() { + private String prepareDependencies() { List nonStdLib = new ArrayList<>(); Optional minimumGoVersion = this.minimumGoVersion; @@ -124,11 +150,13 @@ private Node buildManifestFile() { minimumGoVersion = Optional.of(otherVersion); } } + minimumDependencies = gatherMinimumDependencies(nonStdLib.stream()); + return minimumGoVersion.orElse(DEFAULT_MINIMUM_GO_VERSION); + } + private Node buildManifestFile(String minimumGoVersion) { Map manifestNodes = new HashMap<>(); - Map minimumDependencies = gatherMinimumDependencies(nonStdLib.stream()); - Map dependencyNodes = new HashMap<>(); for (Map.Entry entry : minimumDependencies.entrySet()) { dependencyNodes.put(StringNode.from(entry.getKey()), StringNode.from(entry.getValue())); @@ -142,8 +170,7 @@ private Node buildManifestFile() { generatedFiles = generatedFiles.stream().sorted().collect(Collectors.toList()); manifestNodes.put(StringNode.from("module"), StringNode.from(moduleName)); - minimumGoVersion.ifPresent(version -> manifestNodes.put(StringNode.from("go"), - StringNode.from(version))); + manifestNodes.put(StringNode.from("go"), StringNode.from(minimumGoVersion)); manifestNodes.put(StringNode.from("dependencies"), ObjectNode.objectNode(dependencyNodes)); manifestNodes.put(StringNode.from("files"), ArrayNode.fromStrings(generatedFiles)); manifestNodes.put(StringNode.from("unstable"), BooleanNode.from(isUnstable)); @@ -151,6 +178,20 @@ private Node buildManifestFile() { return ObjectNode.objectNode(manifestNodes).withDeepSortedKeys(); } + private String buildModuleFile(String minimumGoVersion) { + String template = IoUtils.readUtf8Resource(getClass(), "go.mod.template"); + List dependencies = new ArrayList<>(); + + for (Map.Entry entry : minimumDependencies.entrySet()) { + dependencies.add(String.format("%s %s", entry.getKey(), entry.getValue())); + } + + return template + .replace("${moduleName}", moduleName) + .replace("${minimumGoVersion}", minimumGoVersion) + .replace("${dependencies}", String.join("\n", dependencies)); + } + private static Map gatherMinimumDependencies( Stream symbolStream ) { @@ -170,6 +211,7 @@ public static class Builder implements SmithyBuilder { private final BuilderRef> dependencies = BuilderRef.forList(); private Optional minimumGoVersion = Optional.empty(); private boolean isUnstable; + private boolean isGoModule; public Builder moduleName(String moduleName) { this.moduleName = moduleName; @@ -197,6 +239,11 @@ public Builder isUnstable(boolean isUnstable) { return this; } + public Builder isGoModule(boolean isGoModule) { + this.isGoModule = isGoModule; + return this; + } + @Override public ManifestWriter build() { return new ManifestWriter(this); diff --git a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template new file mode 100644 index 000000000..460d52ef4 --- /dev/null +++ b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template @@ -0,0 +1,7 @@ +module ${moduleName} + +go ${minimumGoVersion} + +require ( +${dependencies} +) From 11418e52b8406741f8934fa5ae33c87988670083 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Sat, 24 Dec 2022 02:12:57 +0100 Subject: [PATCH 03/16] feat(codegen): create module metadata file --- .../integration/AddGoModuleMetadata.java | 40 +++++++++++++++++++ ...mithy.go.codegen.integration.GoIntegration | 1 + 2 files changed, 41 insertions(+) create mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/AddGoModuleMetadata.java diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/AddGoModuleMetadata.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/AddGoModuleMetadata.java new file mode 100644 index 000000000..cb48e3a21 --- /dev/null +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/AddGoModuleMetadata.java @@ -0,0 +1,40 @@ +/* + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + + package software.amazon.smithy.go.codegen.integration; + +import java.util.Optional; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.go.codegen.GoDelegator; +import software.amazon.smithy.go.codegen.GoSettings; +import software.amazon.smithy.model.Model; + +public class AddGoModuleMetadata implements GoIntegration { + @Override + public void writeAdditionalFiles( + GoSettings settings, + Model model, + SymbolProvider symbolProvider, + GoDelegator goDelegator + ) { + Optional moduleVersion = settings.getModuleVersion(); + + if (moduleVersion.isPresent()) { + goDelegator.useFileWriter("go_module_metadata.go", settings.getModuleName(), writer -> { + writer.write("const goModuleVersion = \"$L\"", moduleVersion.get()); + }); + } + } +} diff --git a/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration b/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration index f493b3283..2ba059814 100644 --- a/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration +++ b/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration @@ -8,3 +8,4 @@ software.amazon.smithy.go.codegen.integration.OperationInterfaceGenerator software.amazon.smithy.go.codegen.integration.Paginators software.amazon.smithy.go.codegen.integration.Waiters software.amazon.smithy.go.codegen.integration.ClientLogger +software.amazon.smithy.go.codegen.integration.AddGoModuleMetadata From d727b293efe5a19628e7ea272e1e250cfc448c14 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Sat, 24 Dec 2022 02:24:14 +0100 Subject: [PATCH 04/16] chore: include tab formatting to go mod file --- .../java/software/amazon/smithy/go/codegen/ManifestWriter.java | 2 +- .../resources/software/amazon/smithy/go/codegen/go.mod.template | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java index fd66463cd..0b8fdd258 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java @@ -189,7 +189,7 @@ private String buildModuleFile(String minimumGoVersion) { return template .replace("${moduleName}", moduleName) .replace("${minimumGoVersion}", minimumGoVersion) - .replace("${dependencies}", String.join("\n", dependencies)); + .replace("${dependencies}", String.join("\n\t", dependencies)); } private static Map gatherMinimumDependencies( diff --git a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template index 460d52ef4..141218cf0 100644 --- a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template +++ b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template @@ -3,5 +3,5 @@ module ${moduleName} go ${minimumGoVersion} require ( -${dependencies} + ${dependencies} ) From cd3703d596441d4c2f142445df9302baf54c8006 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Wed, 18 Jan 2023 17:00:09 +0100 Subject: [PATCH 05/16] Revert "feat(codegen): create module metadata file" This reverts commit 11418e52b8406741f8934fa5ae33c87988670083. --- .../integration/AddGoModuleMetadata.java | 40 ------------------- ...mithy.go.codegen.integration.GoIntegration | 1 - 2 files changed, 41 deletions(-) delete mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/AddGoModuleMetadata.java diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/AddGoModuleMetadata.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/AddGoModuleMetadata.java deleted file mode 100644 index cb48e3a21..000000000 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/AddGoModuleMetadata.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - - package software.amazon.smithy.go.codegen.integration; - -import java.util.Optional; -import software.amazon.smithy.codegen.core.SymbolProvider; -import software.amazon.smithy.go.codegen.GoDelegator; -import software.amazon.smithy.go.codegen.GoSettings; -import software.amazon.smithy.model.Model; - -public class AddGoModuleMetadata implements GoIntegration { - @Override - public void writeAdditionalFiles( - GoSettings settings, - Model model, - SymbolProvider symbolProvider, - GoDelegator goDelegator - ) { - Optional moduleVersion = settings.getModuleVersion(); - - if (moduleVersion.isPresent()) { - goDelegator.useFileWriter("go_module_metadata.go", settings.getModuleName(), writer -> { - writer.write("const goModuleVersion = \"$L\"", moduleVersion.get()); - }); - } - } -} diff --git a/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration b/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration index 2ba059814..f493b3283 100644 --- a/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration +++ b/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration @@ -8,4 +8,3 @@ software.amazon.smithy.go.codegen.integration.OperationInterfaceGenerator software.amazon.smithy.go.codegen.integration.Paginators software.amazon.smithy.go.codegen.integration.Waiters software.amazon.smithy.go.codegen.integration.ClientLogger -software.amazon.smithy.go.codegen.integration.AddGoModuleMetadata From 7bed7a07edd5a98fa2e091754c56cf2c1c343378 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Wed, 18 Jan 2023 17:14:43 +0100 Subject: [PATCH 06/16] Revert "chore: include tab formatting to go mod file" This reverts commit d727b293efe5a19628e7ea272e1e250cfc448c14. --- .../java/software/amazon/smithy/go/codegen/ManifestWriter.java | 2 +- .../resources/software/amazon/smithy/go/codegen/go.mod.template | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java index 0b8fdd258..fd66463cd 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java @@ -189,7 +189,7 @@ private String buildModuleFile(String minimumGoVersion) { return template .replace("${moduleName}", moduleName) .replace("${minimumGoVersion}", minimumGoVersion) - .replace("${dependencies}", String.join("\n\t", dependencies)); + .replace("${dependencies}", String.join("\n", dependencies)); } private static Map gatherMinimumDependencies( diff --git a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template index 141218cf0..460d52ef4 100644 --- a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template +++ b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template @@ -3,5 +3,5 @@ module ${moduleName} go ${minimumGoVersion} require ( - ${dependencies} +${dependencies} ) From 91802c0a09be4e101d77e68a8dc102095ddd7be2 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Wed, 18 Jan 2023 17:15:05 +0100 Subject: [PATCH 07/16] Revert "feat(codegen): create go.mod file automatically" This reverts commit d06e8c54a7aad9e7951494caea8d6bf9fccb4044. --- .../smithy/go/codegen/ManifestWriter.java | 59 ++----------------- .../amazon/smithy/go/codegen/go.mod.template | 7 --- 2 files changed, 6 insertions(+), 60 deletions(-) delete mode 100644 codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java index fd66463cd..a33713ee2 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java @@ -38,7 +38,6 @@ import software.amazon.smithy.model.node.StringNode; import software.amazon.smithy.model.traits.UnstableTrait; import software.amazon.smithy.utils.BuilderRef; -import software.amazon.smithy.utils.IoUtils; import software.amazon.smithy.utils.SmithyBuilder; /** @@ -47,17 +46,12 @@ */ public final class ManifestWriter { private static final String GENERATED_JSON = "generated.json"; - private static final String GENERATED_GO_MOD = "go.mod"; - private static final String DEFAULT_MINIMUM_GO_VERSION = "1.15"; private final String moduleName; private final FileManifest fileManifest; private final List dependencies; private final Optional minimumGoVersion; private final boolean isUnstable; - private final boolean isGoModule; - - private Map minimumDependencies; private ManifestWriter(Builder builder) { moduleName = SmithyBuilder.requiredState("moduleName", builder.moduleName); @@ -65,7 +59,6 @@ private ManifestWriter(Builder builder) { dependencies = builder.dependencies.copy(); minimumGoVersion = builder.minimumGoVersion; isUnstable = builder.isUnstable; - isGoModule = builder.isGoModule; } /** @@ -87,7 +80,6 @@ public static void writeManifest( .fileManifest(fileManifest) .dependencies(dependencies) .isUnstable(settings.getService(model).getTrait(UnstableTrait.class).isPresent()) - .isGoModule(settings.getModuleVersion().isPresent()) .build() .writeManifest(); } @@ -108,30 +100,12 @@ public void writeManifest() { } fileManifest.addFile(manifestFile); - String minimumGoVersion = prepareDependencies(); - - Node generatedJson = buildManifestFile(minimumGoVersion); + Node generatedJson = buildManifestFile(); fileManifest.writeFile(manifestFile.toString(), Node.prettyPrintJson(generatedJson) + "\n"); - if (isGoModule) { - Path moduleFile = fileManifest.getBaseDir().resolve(GENERATED_GO_MOD); - - if (Files.exists(moduleFile)) { - try { - Files.delete(moduleFile); - } catch (IOException e) { - throw new CodegenException("Failed to delete existing " + GENERATED_GO_MOD + " file", e); - } - } - fileManifest.addFile(moduleFile); - - String moduleContent = buildModuleFile(minimumGoVersion); - fileManifest.writeFile(moduleFile.toString(), moduleContent); - } - } - private String prepareDependencies() { + private Node buildManifestFile() { List nonStdLib = new ArrayList<>(); Optional minimumGoVersion = this.minimumGoVersion; @@ -150,13 +124,11 @@ private String prepareDependencies() { minimumGoVersion = Optional.of(otherVersion); } } - minimumDependencies = gatherMinimumDependencies(nonStdLib.stream()); - return minimumGoVersion.orElse(DEFAULT_MINIMUM_GO_VERSION); - } - private Node buildManifestFile(String minimumGoVersion) { Map manifestNodes = new HashMap<>(); + Map minimumDependencies = gatherMinimumDependencies(nonStdLib.stream()); + Map dependencyNodes = new HashMap<>(); for (Map.Entry entry : minimumDependencies.entrySet()) { dependencyNodes.put(StringNode.from(entry.getKey()), StringNode.from(entry.getValue())); @@ -170,7 +142,8 @@ private Node buildManifestFile(String minimumGoVersion) { generatedFiles = generatedFiles.stream().sorted().collect(Collectors.toList()); manifestNodes.put(StringNode.from("module"), StringNode.from(moduleName)); - manifestNodes.put(StringNode.from("go"), StringNode.from(minimumGoVersion)); + minimumGoVersion.ifPresent(version -> manifestNodes.put(StringNode.from("go"), + StringNode.from(version))); manifestNodes.put(StringNode.from("dependencies"), ObjectNode.objectNode(dependencyNodes)); manifestNodes.put(StringNode.from("files"), ArrayNode.fromStrings(generatedFiles)); manifestNodes.put(StringNode.from("unstable"), BooleanNode.from(isUnstable)); @@ -178,20 +151,6 @@ private Node buildManifestFile(String minimumGoVersion) { return ObjectNode.objectNode(manifestNodes).withDeepSortedKeys(); } - private String buildModuleFile(String minimumGoVersion) { - String template = IoUtils.readUtf8Resource(getClass(), "go.mod.template"); - List dependencies = new ArrayList<>(); - - for (Map.Entry entry : minimumDependencies.entrySet()) { - dependencies.add(String.format("%s %s", entry.getKey(), entry.getValue())); - } - - return template - .replace("${moduleName}", moduleName) - .replace("${minimumGoVersion}", minimumGoVersion) - .replace("${dependencies}", String.join("\n", dependencies)); - } - private static Map gatherMinimumDependencies( Stream symbolStream ) { @@ -211,7 +170,6 @@ public static class Builder implements SmithyBuilder { private final BuilderRef> dependencies = BuilderRef.forList(); private Optional minimumGoVersion = Optional.empty(); private boolean isUnstable; - private boolean isGoModule; public Builder moduleName(String moduleName) { this.moduleName = moduleName; @@ -239,11 +197,6 @@ public Builder isUnstable(boolean isUnstable) { return this; } - public Builder isGoModule(boolean isGoModule) { - this.isGoModule = isGoModule; - return this; - } - @Override public ManifestWriter build() { return new ManifestWriter(this); diff --git a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template deleted file mode 100644 index 460d52ef4..000000000 --- a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/go.mod.template +++ /dev/null @@ -1,7 +0,0 @@ -module ${moduleName} - -go ${minimumGoVersion} - -require ( -${dependencies} -) From 21d79b40804f1432c6f324629729eb30745effae Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Wed, 18 Jan 2023 17:34:48 +0100 Subject: [PATCH 08/16] feat: allow generate go mod in settings --- .../amazon/smithy/go/codegen/GoSettings.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java index 88134a182..4cd287ac3 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java @@ -35,11 +35,13 @@ public final class GoSettings { private static final String MODULE_NAME = "module"; private static final String MODULE_DESCRIPTION = "moduleDescription"; private static final String MODULE_VERSION = "moduleVersion"; + private static final String GENERATE_GO_MOD = "generateGoMod"; private ShapeId service; private String moduleName; private String moduleDescription = ""; private String moduleVersion; + private Boolean generateGoMod = false; private ShapeId protocol; /** @@ -50,13 +52,15 @@ public final class GoSettings { */ public static GoSettings from(ObjectNode config) { GoSettings settings = new GoSettings(); - config.warnIfAdditionalProperties(Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION)); + config.warnIfAdditionalProperties( + Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION, GENERATE_GO_MOD)); settings.setService(config.expectStringMember(SERVICE).expectShapeId()); settings.setModuleName(config.expectStringMember(MODULE_NAME).getValue()); settings.setModuleDescription(config.getStringMemberOrDefault( MODULE_DESCRIPTION, settings.getModuleName() + " client")); settings.setModuleVersion(config.getStringMemberOrDefault(MODULE_VERSION, null)); + settings.setGenerateGoMod(config.getBooleanMemberOrDefault(MODULE_VERSION, false)); return settings; } @@ -152,6 +156,24 @@ public void setModuleVersion(String moduleVersion) { } } + /** + * Gets the flag for generating go.mod file. + * + * @return Returns if go.mod will be generated (true) or not (false) + */ + public Boolean getGenerateGoMod() { + return generateGoMod; + } + + /** + * Sets the flag for generating go.mod file. + * + * @param generateGoMod If go.mod will be generated (true) or not (false) + */ + public void setGenerateGoMod(Boolean generateGoMod) { + this.generateGoMod = Objects.requireNonNull(generateGoMod); + } + /** * Gets the configured protocol to generate. * From c1cd2d5276a0ed11462472681feb826d787fc3b3 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Wed, 18 Jan 2023 17:35:47 +0100 Subject: [PATCH 09/16] chore: put back original go mod generator --- .../smithy/go/codegen/GoModGenerator.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java new file mode 100644 index 000000000..60ff26186 --- /dev/null +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java @@ -0,0 +1,78 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.smithy.go.codegen; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.TreeMap; +import java.util.stream.Collectors; +import software.amazon.smithy.build.FileManifest; +import software.amazon.smithy.codegen.core.CodegenException; +import software.amazon.smithy.codegen.core.SymbolDependency; + +/** + * Generates a go.mod file for the project. + * + *

See here for more information on the format: https://github.com/golang/go/wiki/Modules#gomod + */ +final class GoModGenerator { + + private GoModGenerator() {} + + static void writeGoMod( + GoSettings settings, + FileManifest manifest, + Map> dependencies + ) { + Boolean generateGoMod = settings.getGenerateGoMod(); + if (!generateGoMod) { + return; + } + + Path goModFile = manifest.getBaseDir().resolve("go.mod"); + + // `go mod init` will fail if the `go.mod` already exists, so this deletes + // it if it's present in the output. While it's technically possible + // to simply edit the file, it's easier to just start fresh. + if (Files.exists(goModFile)) { + try { + Files.delete(goModFile); + } catch (IOException e) { + throw new CodegenException("Failed to delete existing go.mod file", e); + } + } + CodegenUtils.runCommand("go mod init " + settings.getModuleName(), manifest.getBaseDir()); + + Map externalDependencies = getExternalDependencies(dependencies); + for (Map.Entry dependency : externalDependencies.entrySet()) { + CodegenUtils.runCommand( + String.format("go mod edit -require=%s@%s", dependency.getKey(), dependency.getValue()), + manifest.getBaseDir()); + } + } + + private static Map getExternalDependencies( + Map> dependencies + ) { + return dependencies.entrySet().stream() + .filter(entry -> !entry.getKey().equals("stdlib")) + .flatMap(entry -> entry.getValue().entrySet().stream()) + .collect(Collectors.toMap( + Map.Entry::getKey, entry -> entry.getValue().getVersion(), (a, b) -> b, TreeMap::new)); + } +} From 7cd07d1afb3549f9029c67d32ac3108ebb9bac7e Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Wed, 18 Jan 2023 21:25:38 +0100 Subject: [PATCH 10/16] feat: add go mod generator to codegen visitor --- .../software/amazon/smithy/go/codegen/CodegenVisitor.java | 3 +++ .../software/amazon/smithy/go/codegen/GoModGenerator.java | 5 +++++ .../java/software/amazon/smithy/go/codegen/GoSettings.java | 2 +- .../software/amazon/smithy/go/codegen/ManifestWriter.java | 6 ++++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java index d0316db10..4ad84ed98 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java @@ -255,6 +255,9 @@ void execute() { List dependencies = writers.getDependencies(); writers.flushWriters(); + LOGGER.fine("Generating go.mod file"); + GoModGenerator.writeGoMod(settings, fileManifest, SymbolDependency.gatherDependencies(dependencies.stream())); + LOGGER.fine("Generating build manifest file"); ManifestWriter.writeManifest(settings, model, fileManifest, dependencies); } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java index 60ff26186..87a63eba6 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.logging.Logger; import java.util.Map; import java.util.TreeMap; import java.util.stream.Collectors; @@ -32,6 +33,8 @@ */ final class GoModGenerator { + private static final Logger LOGGER = Logger.getLogger(GoModGenerator.class.getName()); + private GoModGenerator() {} static void writeGoMod( @@ -45,6 +48,7 @@ static void writeGoMod( } Path goModFile = manifest.getBaseDir().resolve("go.mod"); + LOGGER.fine("Creating go.mod at path " + goModFile.toString()); // `go mod init` will fail if the `go.mod` already exists, so this deletes // it if it's present in the output. While it's technically possible @@ -56,6 +60,7 @@ static void writeGoMod( throw new CodegenException("Failed to delete existing go.mod file", e); } } + manifest.addFile(goModFile); CodegenUtils.runCommand("go mod init " + settings.getModuleName(), manifest.getBaseDir()); Map externalDependencies = getExternalDependencies(dependencies); diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java index 4cd287ac3..cd6bb0949 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java @@ -60,7 +60,7 @@ public static GoSettings from(ObjectNode config) { settings.setModuleDescription(config.getStringMemberOrDefault( MODULE_DESCRIPTION, settings.getModuleName() + " client")); settings.setModuleVersion(config.getStringMemberOrDefault(MODULE_VERSION, null)); - settings.setGenerateGoMod(config.getBooleanMemberOrDefault(MODULE_VERSION, false)); + settings.setGenerateGoMod(config.getBooleanMemberOrDefault(GENERATE_GO_MOD, false)); return settings; } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java index a33713ee2..6fba6142d 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Optional; import java.util.TreeMap; +import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.Stream; import software.amazon.smithy.build.FileManifest; @@ -45,6 +46,9 @@ * and minimum dependencies required. */ public final class ManifestWriter { + + private static final Logger LOGGER = Logger.getLogger(ManifestWriter.class.getName()); + private static final String GENERATED_JSON = "generated.json"; private final String moduleName; @@ -100,6 +104,8 @@ public void writeManifest() { } fileManifest.addFile(manifestFile); + LOGGER.fine("Creating manifest at path " + manifestFile.toString()); + Node generatedJson = buildManifestFile(); fileManifest.writeFile(manifestFile.toString(), Node.prettyPrintJson(generatedJson) + "\n"); From 591109992e82f071c394e1109ddde06c000afb81 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Wed, 18 Jan 2023 21:25:47 +0100 Subject: [PATCH 11/16] chore: include unit tests for gomod generator --- .../smithy/go/codegen/GoModGeneratorTest.java | 56 +++++++++++++++++++ .../amazon/smithy/go/codegen/TestUtils.java | 11 +++- 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/GoModGeneratorTest.java diff --git a/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/GoModGeneratorTest.java b/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/GoModGeneratorTest.java new file mode 100644 index 000000000..26dc2f452 --- /dev/null +++ b/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/GoModGeneratorTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.smithy.go.codegen; + +import java.util.logging.Logger; +import org.junit.jupiter.api.Test; + +import software.amazon.smithy.build.MockManifest; +import software.amazon.smithy.build.PluginContext; +import software.amazon.smithy.go.codegen.GoCodegenPlugin; +import software.amazon.smithy.model.Model; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import static software.amazon.smithy.go.codegen.TestUtils.buildMockPluginContext; +import static software.amazon.smithy.go.codegen.TestUtils.loadSmithyModelFromResource; + + +public class GoModGeneratorTest { + private static final Logger LOGGER = Logger.getLogger(GoModGeneratorTest.class.getName()); + + @Test + public void testGoModGenerator() { + + // Arrange + Model model = + loadSmithyModelFromResource("enum-shape-test"); + MockManifest manifest = + new MockManifest(); + PluginContext context = + buildMockPluginContext(model, manifest, "smithy.example#Example"); + + // Act + (new GoCodegenPlugin()).execute(context); + LOGGER.info(manifest.toString()); + + // Assert + assertThat(manifest.hasFile("go.mod"), + is(true)); + } + +} diff --git a/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/TestUtils.java b/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/TestUtils.java index dcf521121..ce9958d2b 100644 --- a/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/TestUtils.java +++ b/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/TestUtils.java @@ -71,7 +71,8 @@ public static PluginContext buildMockPluginContext( manifest, serviceShapeId, "example", - "0.0.1"); + "0.0.1", + true); } public static PluginContext buildPluginContext( @@ -79,7 +80,8 @@ public static PluginContext buildPluginContext( FileManifest manifest, String serviceShapeId, String moduleName, - String moduleVersion + String moduleVersion, + Boolean generateGoMod ) { return PluginContext.builder() .model(model) @@ -88,6 +90,7 @@ public static PluginContext buildPluginContext( serviceShapeId, moduleName, moduleVersion, + generateGoMod, "Example")) .build(); } @@ -96,12 +99,14 @@ public static ObjectNode getSettingsNode( String serviceShapeId, String moduleName, String moduleVersion, + Boolean generateGoMod, String sdkId ) { return Node.objectNodeBuilder() .withMember("service", Node.from(serviceShapeId)) .withMember("module", Node.from(moduleName)) .withMember("moduleVersion", Node.from(moduleVersion)) + .withMember("generateGoMod", Node.from(generateGoMod)) .withMember("homepage", Node.from("https://docs.amplify.aws/")) .withMember("sdkId", Node.from(sdkId)) .withMember("author", Node.from("Amazon Web Services")) @@ -157,4 +162,4 @@ public static void makeGoModule(Path path) throws Exception { public static void testGoModule(Path path) throws Exception { execute(path.toFile(), "go", "test", "-v", "./..."); } -} \ No newline at end of file +} From 7c44a305ebfe6f88b3175b9066880f3aece3899e Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Wed, 18 Jan 2023 22:03:26 +0100 Subject: [PATCH 12/16] chore: unable to make unit test pass --- .../smithy-go-codegen-test/smithy-build.json | 3 +- .../smithy/go/codegen/GoModGenerator.java | 2 +- .../smithy/go/codegen/GoModGeneratorTest.java | 56 ------------------- .../amazon/smithy/go/codegen/TestUtils.java | 2 +- 4 files changed, 4 insertions(+), 59 deletions(-) delete mode 100644 codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/GoModGeneratorTest.java diff --git a/codegen/smithy-go-codegen-test/smithy-build.json b/codegen/smithy-go-codegen-test/smithy-build.json index 1330bacdc..cc4ddd088 100644 --- a/codegen/smithy-go-codegen-test/smithy-build.json +++ b/codegen/smithy-go-codegen-test/smithy-build.json @@ -4,7 +4,8 @@ "go-codegen": { "service": "example.weather#Weather", "module": "github.com/aws/smithy-go/internal/tests/service/weather", - "moduleVersion": "0.0.1" + "moduleVersion": "0.0.1", + "generateGoMod": true } } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java index 87a63eba6..c7d4e77dc 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java @@ -18,9 +18,9 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.logging.Logger; import java.util.Map; import java.util.TreeMap; +import java.util.logging.Logger; import java.util.stream.Collectors; import software.amazon.smithy.build.FileManifest; import software.amazon.smithy.codegen.core.CodegenException; diff --git a/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/GoModGeneratorTest.java b/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/GoModGeneratorTest.java deleted file mode 100644 index 26dc2f452..000000000 --- a/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/GoModGeneratorTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - -package software.amazon.smithy.go.codegen; - -import java.util.logging.Logger; -import org.junit.jupiter.api.Test; - -import software.amazon.smithy.build.MockManifest; -import software.amazon.smithy.build.PluginContext; -import software.amazon.smithy.go.codegen.GoCodegenPlugin; -import software.amazon.smithy.model.Model; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; - -import static software.amazon.smithy.go.codegen.TestUtils.buildMockPluginContext; -import static software.amazon.smithy.go.codegen.TestUtils.loadSmithyModelFromResource; - - -public class GoModGeneratorTest { - private static final Logger LOGGER = Logger.getLogger(GoModGeneratorTest.class.getName()); - - @Test - public void testGoModGenerator() { - - // Arrange - Model model = - loadSmithyModelFromResource("enum-shape-test"); - MockManifest manifest = - new MockManifest(); - PluginContext context = - buildMockPluginContext(model, manifest, "smithy.example#Example"); - - // Act - (new GoCodegenPlugin()).execute(context); - LOGGER.info(manifest.toString()); - - // Assert - assertThat(manifest.hasFile("go.mod"), - is(true)); - } - -} diff --git a/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/TestUtils.java b/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/TestUtils.java index ce9958d2b..dcf5b6ed8 100644 --- a/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/TestUtils.java +++ b/codegen/smithy-go-codegen/src/test/java/software/amazon/smithy/go/codegen/TestUtils.java @@ -72,7 +72,7 @@ public static PluginContext buildMockPluginContext( serviceShapeId, "example", "0.0.1", - true); + false); } public static PluginContext buildPluginContext( From cdd541481a030942a576a78a3c3016f5441d4e91 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Fri, 20 Jan 2023 12:16:54 +0100 Subject: [PATCH 13/16] feat: add go directive to plugin settings --- .../smithy-go-codegen-test/smithy-build.json | 3 ++- .../smithy/go/codegen/GoModGenerator.java | 4 ++++ .../amazon/smithy/go/codegen/GoSettings.java | 23 ++++++++++++++++++- .../smithy/go/codegen/ManifestWriter.java | 16 ++++++------- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/codegen/smithy-go-codegen-test/smithy-build.json b/codegen/smithy-go-codegen-test/smithy-build.json index cc4ddd088..b495c9292 100644 --- a/codegen/smithy-go-codegen-test/smithy-build.json +++ b/codegen/smithy-go-codegen-test/smithy-build.json @@ -5,7 +5,8 @@ "service": "example.weather#Weather", "module": "github.com/aws/smithy-go/internal/tests/service/weather", "moduleVersion": "0.0.1", - "generateGoMod": true + "generateGoMod": true, + "goDirective": "1.18" } } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java index c7d4e77dc..a1c2fb496 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java @@ -69,6 +69,10 @@ static void writeGoMod( String.format("go mod edit -require=%s@%s", dependency.getKey(), dependency.getValue()), manifest.getBaseDir()); } + + CodegenUtils.runCommand( + String.format("go mod edit -go=%s", settings.getGoDirective()), + manifest.getBaseDir()); } private static Map getExternalDependencies( diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java index cd6bb0949..188a61cbd 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java @@ -36,12 +36,14 @@ public final class GoSettings { private static final String MODULE_DESCRIPTION = "moduleDescription"; private static final String MODULE_VERSION = "moduleVersion"; private static final String GENERATE_GO_MOD = "generateGoMod"; + private static final String GO_DIRECTIVE = "goDirective"; private ShapeId service; private String moduleName; private String moduleDescription = ""; private String moduleVersion; private Boolean generateGoMod = false; + private String goDirective = "1.15"; private ShapeId protocol; /** @@ -53,7 +55,7 @@ public final class GoSettings { public static GoSettings from(ObjectNode config) { GoSettings settings = new GoSettings(); config.warnIfAdditionalProperties( - Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION, GENERATE_GO_MOD)); + Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION, GENERATE_GO_MOD, GO_DIRECTIVE)); settings.setService(config.expectStringMember(SERVICE).expectShapeId()); settings.setModuleName(config.expectStringMember(MODULE_NAME).getValue()); @@ -61,6 +63,7 @@ public static GoSettings from(ObjectNode config) { MODULE_DESCRIPTION, settings.getModuleName() + " client")); settings.setModuleVersion(config.getStringMemberOrDefault(MODULE_VERSION, null)); settings.setGenerateGoMod(config.getBooleanMemberOrDefault(GENERATE_GO_MOD, false)); + settings.setGoDirective(config.getStringMemberOrDefault(GO_DIRECTIVE, "1.15")); return settings; } @@ -174,6 +177,24 @@ public void setGenerateGoMod(Boolean generateGoMod) { this.generateGoMod = Objects.requireNonNull(generateGoMod); } + /** + * Gets the optional Go directive for the module that will be generated. + * + * @return Returns the Go directive. + */ + public String getGoDirective() { + return goDirective; + } + + /** + * Sets the Go directive of the module to generate. + * + * @param goDirective The Go directive of the module to generate. + */ + public void setGoDirective(String goDirective) { + this.goDirective = Objects.requireNonNull(goDirective); + } + /** * Gets the configured protocol to generate. * diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java index 6fba6142d..69d3679c8 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ManifestWriter.java @@ -54,14 +54,14 @@ public final class ManifestWriter { private final String moduleName; private final FileManifest fileManifest; private final List dependencies; - private final Optional minimumGoVersion; + private final String goDirective; private final boolean isUnstable; private ManifestWriter(Builder builder) { moduleName = SmithyBuilder.requiredState("moduleName", builder.moduleName); fileManifest = SmithyBuilder.requiredState("fileManifest", builder.fileManifest); dependencies = builder.dependencies.copy(); - minimumGoVersion = builder.minimumGoVersion; + goDirective = builder.goDirective; isUnstable = builder.isUnstable; } @@ -83,6 +83,7 @@ public static void writeManifest( .moduleName(settings.getModuleName()) .fileManifest(fileManifest) .dependencies(dependencies) + .goDirective(settings.getGoDirective()) .isUnstable(settings.getService(model).getTrait(UnstableTrait.class).isPresent()) .build() .writeManifest(); @@ -113,7 +114,7 @@ public void writeManifest() { private Node buildManifestFile() { List nonStdLib = new ArrayList<>(); - Optional minimumGoVersion = this.minimumGoVersion; + Optional minimumGoVersion = Optional.empty(); for (SymbolDependency dependency : dependencies) { if (!dependency.getDependencyType().equals(GoDependency.Type.STANDARD_LIBRARY.toString())) { @@ -148,8 +149,7 @@ private Node buildManifestFile() { generatedFiles = generatedFiles.stream().sorted().collect(Collectors.toList()); manifestNodes.put(StringNode.from("module"), StringNode.from(moduleName)); - minimumGoVersion.ifPresent(version -> manifestNodes.put(StringNode.from("go"), - StringNode.from(version))); + manifestNodes.put(StringNode.from("go"), StringNode.from(minimumGoVersion.orElse(this.goDirective))); manifestNodes.put(StringNode.from("dependencies"), ObjectNode.objectNode(dependencyNodes)); manifestNodes.put(StringNode.from("files"), ArrayNode.fromStrings(generatedFiles)); manifestNodes.put(StringNode.from("unstable"), BooleanNode.from(isUnstable)); @@ -174,7 +174,7 @@ public static class Builder implements SmithyBuilder { private String moduleName; private FileManifest fileManifest; private final BuilderRef> dependencies = BuilderRef.forList(); - private Optional minimumGoVersion = Optional.empty(); + private String goDirective; private boolean isUnstable; public Builder moduleName(String moduleName) { @@ -193,8 +193,8 @@ public Builder dependencies(List dependencies) { return this; } - public Builder minimumGoVersion(String minimumGoVersion) { - this.minimumGoVersion = Optional.of(minimumGoVersion); + public Builder goDirective(String minimumGoVersion) { + this.goDirective = minimumGoVersion; return this; } From acd673ef3e352527a92b437a74596151b8c6c35e Mon Sep 17 00:00:00 2001 From: Eduardo de Moura Rodrigues <16357187+eduardomourar@users.noreply.github.com> Date: Tue, 24 Jan 2023 14:57:44 +0100 Subject: [PATCH 14/16] chore: update 1 Co-authored-by: Steven Yuan --- .../java/software/amazon/smithy/go/codegen/GoModGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java index a1c2fb496..f34393bb7 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. From 42f65ec644bbf4c1626d9203682c99ba34c6650e Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Tue, 24 Jan 2023 15:02:20 +0100 Subject: [PATCH 15/16] chore: use static default go directive --- .../software/amazon/smithy/go/codegen/GoModGenerator.java | 2 +- .../java/software/amazon/smithy/go/codegen/GoSettings.java | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java index f34393bb7..bdc460f23 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModGenerator.java @@ -48,7 +48,7 @@ static void writeGoMod( } Path goModFile = manifest.getBaseDir().resolve("go.mod"); - LOGGER.fine("Creating go.mod at path " + goModFile.toString()); + LOGGER.fine("Generating go.mod file at path " + goModFile.toString()); // `go mod init` will fail if the `go.mod` already exists, so this deletes // it if it's present in the output. While it's technically possible diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java index 188a61cbd..ea6e348b0 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java @@ -31,6 +31,8 @@ */ public final class GoSettings { + private static final String DEFAULT_GO_DIRECTIVE = "1.15"; + private static final String SERVICE = "service"; private static final String MODULE_NAME = "module"; private static final String MODULE_DESCRIPTION = "moduleDescription"; @@ -43,7 +45,7 @@ public final class GoSettings { private String moduleDescription = ""; private String moduleVersion; private Boolean generateGoMod = false; - private String goDirective = "1.15"; + private String goDirective = DEFAULT_GO_DIRECTIVE; private ShapeId protocol; /** @@ -63,7 +65,7 @@ public static GoSettings from(ObjectNode config) { MODULE_DESCRIPTION, settings.getModuleName() + " client")); settings.setModuleVersion(config.getStringMemberOrDefault(MODULE_VERSION, null)); settings.setGenerateGoMod(config.getBooleanMemberOrDefault(GENERATE_GO_MOD, false)); - settings.setGoDirective(config.getStringMemberOrDefault(GO_DIRECTIVE, "1.15")); + settings.setGoDirective(config.getStringMemberOrDefault(GO_DIRECTIVE, DEFAULT_GO_DIRECTIVE)); return settings; } From d44d9935f3c8d2142744083496b0a0be5208431a Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Tue, 24 Jan 2023 15:03:13 +0100 Subject: [PATCH 16/16] chore: remove logging --- .../java/software/amazon/smithy/go/codegen/CodegenVisitor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java index 4ad84ed98..4ef351e47 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java @@ -255,7 +255,6 @@ void execute() { List dependencies = writers.getDependencies(); writers.flushWriters(); - LOGGER.fine("Generating go.mod file"); GoModGenerator.writeGoMod(settings, fileManifest, SymbolDependency.gatherDependencies(dependencies.stream())); LOGGER.fine("Generating build manifest file");