Skip to content

Commit cc922a4

Browse files
authored
fix: accommodate services with the world Client in their names (#1102)
* fix: accommodate services with the world Client in their names * fix copyright
1 parent c794e2f commit cc922a4

File tree

8 files changed

+75
-10
lines changed

8 files changed

+75
-10
lines changed

.changeset/hip-bobcats-appear.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

config/checkstyle/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<!-- Files must contain a copyright header, with or without a year. -->
2323
<module name="RegexpHeader">
2424
<property name="header"
25-
value="/\*\n \* Copyright( 20(19|20|21|22)|) Amazon\.com, Inc\. or its affiliates\. All Rights Reserved\.\n"/>
25+
value="/\*\n \* Copyright( 20(19|20|21|22|23)|) Amazon\.com, Inc\. or its affiliates\. All Rights Reserved\.\n"/>
2626
<property name="fileExtensions" value="java"/>
2727
</module>
2828

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
5757
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
5858
import software.amazon.smithy.typescript.codegen.validation.LongValidator;
59+
import software.amazon.smithy.typescript.codegen.validation.ReplaceLast;
5960
import software.amazon.smithy.utils.MapUtils;
6061
import software.amazon.smithy.utils.SmithyUnstableApi;
6162
import software.amazon.smithy.waiters.WaitableTrait;
@@ -249,8 +250,8 @@ private void generateClient(GenerateServiceDirective<TypeScriptCodegenContext, T
249250

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

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import software.amazon.smithy.model.Model;
2424
import software.amazon.smithy.model.shapes.ServiceShape;
2525
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
26+
import software.amazon.smithy.typescript.codegen.validation.ReplaceLast;
2627

2728
public class ExtensionConfigurationGenerator {
2829

@@ -62,9 +63,13 @@ void generate() {
6263
});
6364
}
6465

65-
String clientName = symbolProvider.toSymbol(service).getName()
66-
.replace("Client", "")
67-
.replace("client", "");
66+
String clientName = ReplaceLast.in(
67+
ReplaceLast.in(
68+
symbolProvider.toSymbol(service).getName(),
69+
"Client", ""
70+
),
71+
"client", ""
72+
);
6873

6974
String clientConfigurationContent = TypeScriptUtils
7075
.loadResourceAsString(CLIENT_CONFIGURATION_TEMPLATE)

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import software.amazon.smithy.model.traits.PaginatedTrait;
3030
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
3131
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
32+
import software.amazon.smithy.typescript.codegen.validation.ReplaceLast;
3233
import software.amazon.smithy.utils.SmithyInternalApi;
3334
import software.amazon.smithy.waiters.WaitableTrait;
3435

@@ -99,7 +100,7 @@ private static void writeClientExports(
99100
ServiceShape service = settings.getService(model);
100101
Symbol symbol = symbolProvider.toSymbol(service);
101102
// Normalizes client name, e.g. WeatherClient => Weather
102-
String normalizedClientName = symbol.getName().replace("Client", "");
103+
String normalizedClientName = ReplaceLast.in(symbol.getName(), "Client", "");
103104

104105
// Write export statement for bare-bones client.
105106
writer.write("export * from \"./$L\";", symbol.getName());

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import software.amazon.smithy.model.Model;
2222
import software.amazon.smithy.model.shapes.ServiceShape;
2323
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
24+
import software.amazon.smithy.typescript.codegen.validation.ReplaceLast;
2425

2526
public class RuntimeExtensionsGenerator {
2627

@@ -52,9 +53,13 @@ public RuntimeExtensionsGenerator(
5253
}
5354

5455
void generate() {
55-
String clientName = symbolProvider.toSymbol(service).getName()
56-
.replace("Client", "")
57-
.replace("client", "");
56+
String clientName = ReplaceLast.in(
57+
ReplaceLast.in(
58+
symbolProvider.toSymbol(service).getName(),
59+
"Client", ""
60+
),
61+
"client", ""
62+
);
5863

5964
String template1Contents = TypeScriptUtils.loadResourceAsString(TEMPLATE_1)
6065
.replace("${extensionConfigName}", clientName + "ExtensionConfiguration")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.smithy.typescript.codegen.validation;
17+
18+
public abstract class ReplaceLast {
19+
20+
/**
21+
* @param original - source string.
22+
* @param target - substring to be replaced.
23+
* @param replacement - the replacement.
24+
* @return original with the last occurrence of the target string replaced by the replacement string.
25+
*/
26+
public static String in(String original, String target, String replacement) {
27+
int lastPosition = original.lastIndexOf(target);
28+
if (lastPosition >= 0) {
29+
return original.substring(0, lastPosition)
30+
+ replacement
31+
+ original.substring(lastPosition + target.length());
32+
}
33+
return original;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package software.amazon.smithy.typescript.codegen.validation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ReplaceLastTest {
8+
9+
@Test
10+
public void replaceLast() {
11+
assertEquals(ReplaceLast.in("WorkspacesThinClientClient", "Client", ""), "WorkspacesThinClient");
12+
assertEquals(ReplaceLast.in("WorkspacesThinClientClientClient", "Client", ""), "WorkspacesThinClientClient");
13+
14+
assertEquals(ReplaceLast.in("welcometothecity", "e", "is"), "welcometothiscity");
15+
}
16+
}

0 commit comments

Comments
 (0)