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..a7add557c6f --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java @@ -0,0 +1,35 @@ +/* + * Copyright 2021 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