From cc922a47da45ed915bf914bdb790eaa885ad4063 Mon Sep 17 00:00:00 2001 From: George Fu Date: Thu, 7 Dec 2023 12:55:14 -0500 Subject: [PATCH] fix: accommodate services with the world Client in their names (#1102) * fix: accommodate services with the world Client in their names * fix copyright --- .changeset/hip-bobcats-appear.md | 2 ++ config/checkstyle/checkstyle.xml | 2 +- .../codegen/DirectedTypeScriptCodegen.java | 5 +-- .../ExtensionConfigurationGenerator.java | 11 ++++-- .../typescript/codegen/IndexGenerator.java | 3 +- .../codegen/RuntimeExtensionsGenerator.java | 11 ++++-- .../codegen/validation/ReplaceLast.java | 35 +++++++++++++++++++ .../codegen/validation/ReplaceLastTest.java | 16 +++++++++ 8 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 .changeset/hip-bobcats-appear.md create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java create mode 100644 smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLastTest.java diff --git a/.changeset/hip-bobcats-appear.md b/.changeset/hip-bobcats-appear.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/hip-bobcats-appear.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index 9e6f3b27c6c..06aa1cc0d8e 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -22,7 +22,7 @@ + value="/\*\n \* Copyright( 20(19|20|21|22|23)|) Amazon\.com, Inc\. or its affiliates\. All Rights Reserved\.\n"/> diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java index 3a4d1c1b1b5..f70ab9cc58b 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java @@ -56,6 +56,7 @@ import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; import software.amazon.smithy.typescript.codegen.validation.LongValidator; +import software.amazon.smithy.typescript.codegen.validation.ReplaceLast; import software.amazon.smithy.utils.MapUtils; import software.amazon.smithy.utils.SmithyUnstableApi; import software.amazon.smithy.waiters.WaitableTrait; @@ -249,8 +250,8 @@ private void generateClient(GenerateServiceDirective new ServiceAggregatedClientGenerator( settings, model, symbolProvider, aggregatedClientName, writer, applicationProtocol).run()); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java index f8acb3765fb..a35a4fd20b8 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java @@ -23,6 +23,7 @@ import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.typescript.codegen.validation.ReplaceLast; public class ExtensionConfigurationGenerator { @@ -62,9 +63,13 @@ void generate() { }); } - String clientName = symbolProvider.toSymbol(service).getName() - .replace("Client", "") - .replace("client", ""); + String clientName = ReplaceLast.in( + ReplaceLast.in( + symbolProvider.toSymbol(service).getName(), + "Client", "" + ), + "client", "" + ); String clientConfigurationContent = TypeScriptUtils .loadResourceAsString(CLIENT_CONFIGURATION_TEMPLATE) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java index 34db6baf84d..46b4d8017fd 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java @@ -29,6 +29,7 @@ import software.amazon.smithy.model.traits.PaginatedTrait; import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; +import software.amazon.smithy.typescript.codegen.validation.ReplaceLast; import software.amazon.smithy.utils.SmithyInternalApi; import software.amazon.smithy.waiters.WaitableTrait; @@ -99,7 +100,7 @@ private static void writeClientExports( ServiceShape service = settings.getService(model); Symbol symbol = symbolProvider.toSymbol(service); // Normalizes client name, e.g. WeatherClient => Weather - String normalizedClientName = symbol.getName().replace("Client", ""); + String normalizedClientName = ReplaceLast.in(symbol.getName(), "Client", ""); // Write export statement for bare-bones client. writer.write("export * from \"./$L\";", symbol.getName()); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java index 9bd063f2e9e..03b37c5c348 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java @@ -21,6 +21,7 @@ import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.typescript.codegen.validation.ReplaceLast; public class RuntimeExtensionsGenerator { @@ -52,9 +53,13 @@ public RuntimeExtensionsGenerator( } void generate() { - String clientName = symbolProvider.toSymbol(service).getName() - .replace("Client", "") - .replace("client", ""); + String clientName = ReplaceLast.in( + ReplaceLast.in( + symbolProvider.toSymbol(service).getName(), + "Client", "" + ), + "client", "" + ); String template1Contents = TypeScriptUtils.loadResourceAsString(TEMPLATE_1) .replace("${extensionConfigName}", clientName + "ExtensionConfiguration") diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java new file mode 100644 index 00000000000..bfaa256760f --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java @@ -0,0 +1,35 @@ +/* + * 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.typescript.codegen.validation; + +public abstract class ReplaceLast { + + /** + * @param original - source string. + * @param target - substring to be replaced. + * @param replacement - the replacement. + * @return original with the last occurrence of the target string replaced by the replacement string. + */ + public static String in(String original, String target, String replacement) { + int lastPosition = original.lastIndexOf(target); + if (lastPosition >= 0) { + return original.substring(0, lastPosition) + + replacement + + original.substring(lastPosition + target.length()); + } + return original; + } +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLastTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLastTest.java new file mode 100644 index 00000000000..eaa5152f3a0 --- /dev/null +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLastTest.java @@ -0,0 +1,16 @@ +package software.amazon.smithy.typescript.codegen.validation; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ReplaceLastTest { + + @Test + public void replaceLast() { + assertEquals(ReplaceLast.in("WorkspacesThinClientClient", "Client", ""), "WorkspacesThinClient"); + assertEquals(ReplaceLast.in("WorkspacesThinClientClientClient", "Client", ""), "WorkspacesThinClientClient"); + + assertEquals(ReplaceLast.in("welcometothecity", "e", "is"), "welcometothiscity"); + } +} \ No newline at end of file