Skip to content

Commit

Permalink
fix: accommodate services with the world Client in their names (#1102)
Browse files Browse the repository at this point in the history
* fix: accommodate services with the world Client in their names

* fix copyright
  • Loading branch information
kuhe authored Dec 7, 2023
1 parent c794e2f commit cc922a4
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .changeset/hip-bobcats-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 1 addition & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<!-- Files must contain a copyright header, with or without a year. -->
<module name="RegexpHeader">
<property name="header"
value="/\*\n \* Copyright( 20(19|20|21|22)|) Amazon\.com, Inc\. or its affiliates\. All Rights Reserved\.\n"/>
value="/\*\n \* Copyright( 20(19|20|21|22|23)|) Amazon\.com, Inc\. or its affiliates\. All Rights Reserved\.\n"/>
<property name="fileExtensions" value="java"/>
</module>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -249,8 +250,8 @@ private void generateClient(GenerateServiceDirective<TypeScriptCodegenContext, T

// Generate the aggregated service client.
Symbol serviceSymbol = symbolProvider.toSymbol(service);
String aggregatedClientName = serviceSymbol.getName().replace("Client", "");
String filename = serviceSymbol.getDefinitionFile().replace("Client", "");
String aggregatedClientName = ReplaceLast.in(serviceSymbol.getName(), "Client", "");
String filename = ReplaceLast.in(serviceSymbol.getDefinitionFile(), "Client", "");
delegator.useFileWriter(filename, writer -> new ServiceAggregatedClientGenerator(
settings, model, symbolProvider, aggregatedClientName, writer, applicationProtocol).run());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit cc922a4

Please sign in to comment.