Skip to content

Commit 5fffa7b

Browse files
committed
chore(codegen): populate default hostname and variant
1 parent 8a73c94 commit 5fffa7b

File tree

1 file changed

+26
-9
lines changed
  • codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen

1 file changed

+26
-9
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/EndpointGenerator.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.smithy.aws.typescript.codegen;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Optional;
@@ -93,15 +94,23 @@ private void loadServiceEndpoints() {
9394
for (Map.Entry<String, Node> entry : endpointMap.getStringMap().entrySet()) {
9495
ObjectNode config = entry.getValue().expectObjectNode();
9596
// TODO: Do not populate config if "deprecated" is present.
96-
if (config.containsMember("hostname")) {
97-
// Resolve the hostname.
98-
String hostName = config.expectStringMember("hostname").getValue();
99-
hostName = hostName.replace("{dnsSuffix}", dnsSuffix);
100-
hostName = hostName.replace("{service}", endpointPrefix);
101-
hostName = hostName.replace("{region}", entry.getKey());
102-
config = config.withMember("hostname", hostName);
97+
if (config.containsMember("hostname") || config.containsMember("variants")) {
98+
String hostname = config.getStringMemberOrDefault("hostname", partition.hostnameTemplate);
99+
100+
hostname = hostname.replace("{dnsSuffix}", dnsSuffix);
101+
hostname = hostname.replace("{service}", endpointPrefix);
102+
hostname = hostname.replace("{region}", entry.getKey());
103+
// TODO: Remove hostname after fully switching to variants.
104+
config = config.withMember("hostname", hostname);
105+
106+
ArrayNode variants = config.getArrayMember("variants").orElse(
107+
ArrayNode.fromNodes());
108+
ArrayNode defaultVariant = ArrayNode.fromNodes(getDefaultVariant(hostname));
109+
110+
// Populate hostname as the default variant.
111+
config = config.withMember("variants", variants.merge(defaultVariant));
112+
endpoints.put(entry.getKey(), config);
103113
}
104-
endpoints.put(entry.getKey(), config);
105114
}
106115
}
107116
}
@@ -136,7 +145,7 @@ private void writePartitionHash() {
136145
writer.write("regionRegex: $S,", partition.regionRegex);
137146
OptionalUtils.ifPresentOrElse(partition.getPartitionEndpoint(),
138147
endpoint -> writer.write("endpoint: $S,", endpoint),
139-
// TODO: Remove population of hostname after switching to variants.
148+
// TODO: Remove hostname after fully switching to variants.
140149
() -> writer.write("hostname: $S,", partition.hostnameTemplate));
141150
List<Node> variants = partition.getVariants();
142151
if (!variants.isEmpty()) {
@@ -196,6 +205,13 @@ private void writeEndpointSpecificResolver(String region, ObjectNode resolved) {
196205
}
197206
}
198207

208+
private ObjectNode getDefaultVariant(String hostname) {
209+
Map<String, String> defaultVariant = Collections.singletonMap("hostname", hostname);
210+
ArrayNode defaultVariantTags = ArrayNode.fromStrings(Collections.emptyList());
211+
212+
return ObjectNode.fromStringMap(defaultVariant).withMember("tags", defaultVariantTags);
213+
}
214+
199215
private final class Partition {
200216
final ObjectNode defaults;
201217
final String dnsSuffix;
@@ -259,6 +275,7 @@ List<Node> getVariants() {
259275
hostname = hostname.replace("{dnsSuffix}", dnsSuffix);
260276
allVariants.add(variantNode.withMember("hostname", hostname).withoutMember("dnsSuffix"));
261277
});
278+
allVariants.add(getDefaultVariant(hostnameTemplate));
262279
}
263280

264281
return allVariants;

0 commit comments

Comments
 (0)