Skip to content

Commit 913653a

Browse files
committed
chore(codegen): populate variants in endpoints hashes
1 parent 75e9a97 commit 913653a

File tree

1 file changed

+50
-10
lines changed
  • codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen

1 file changed

+50
-10
lines changed

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

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package software.amazon.smithy.aws.typescript.codegen;
1717

18+
import java.util.ArrayList;
1819
import java.util.List;
1920
import java.util.Map;
2021
import java.util.Optional;
@@ -24,6 +25,7 @@
2425
import software.amazon.smithy.aws.traits.ServiceTrait;
2526
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
2627
import software.amazon.smithy.codegen.core.CodegenException;
28+
import software.amazon.smithy.model.node.ArrayNode;
2729
import software.amazon.smithy.model.node.Node;
2830
import software.amazon.smithy.model.node.ObjectNode;
2931
import software.amazon.smithy.model.node.StringNode;
@@ -90,15 +92,16 @@ private void loadServiceEndpoints() {
9092

9193
for (Map.Entry<String, Node> entry : endpointMap.getStringMap().entrySet()) {
9294
ObjectNode config = entry.getValue().expectObjectNode();
95+
// TODO: Do not populate config if "deprecated" is present.
9396
if (config.containsMember("hostname")) {
9497
// Resolve the hostname.
9598
String hostName = config.expectStringMember("hostname").getValue();
9699
hostName = hostName.replace("{dnsSuffix}", dnsSuffix);
97100
hostName = hostName.replace("{service}", endpointPrefix);
98101
hostName = hostName.replace("{region}", entry.getKey());
99102
config = config.withMember("hostname", hostName);
100-
endpoints.put(entry.getKey(), config);
101103
}
104+
endpoints.put(entry.getKey(), config);
102105
}
103106
}
104107
}
@@ -134,6 +137,14 @@ private void writePartitionHash() {
134137
OptionalUtils.ifPresentOrElse(partition.getPartitionEndpoint(),
135138
endpoint -> writer.write("endpoint: $S,", endpoint),
136139
() -> writer.write("hostname: $S,", partition.hostnameTemplate));
140+
List<Node> variants = partition.getVariants();
141+
if (!variants.isEmpty()) {
142+
writer.openBlock("variants: [", "],", () -> {
143+
variants.forEach(variant -> {
144+
writer.write("$L, ", Node.prettyPrintJson(variant));
145+
});
146+
});
147+
}
137148
});
138149
});
139150
});
@@ -157,10 +168,18 @@ private void writeEndpointProviderFunction() {
157168
}
158169

159170
private void writeEndpointSpecificResolver(String region, ObjectNode resolved) {
160-
if (resolved.containsMember("hostname") || resolved.containsMember("credentialScope")) {
171+
if (resolved.containsMember("variants")
172+
|| resolved.containsMember("hostname")
173+
|| resolved.containsMember("credentialScope")) {
161174
writer.openBlock("$S: {", "},", region, () -> {
162-
String hostname = resolved.expectStringMember("hostname").getValue();
163-
writer.write("hostname: $S,", hostname);
175+
if (resolved.containsMember("hostname")) {
176+
String hostname = resolved.expectStringMember("hostname").getValue();
177+
writer.write("hostname: $S,", hostname);
178+
}
179+
if (resolved.containsMember("variants")) {
180+
ArrayNode variants = resolved.expectArrayMember("variants");
181+
writer.write("variants: $L,", ArrayNode.prettyPrintJson(variants));
182+
}
164183
resolved.getObjectMember("credentialScope").ifPresent(scope -> {
165184
scope.getStringMember("region").ifPresent(signingRegion -> {
166185
writer.write("signingRegion: $S,", signingRegion);
@@ -175,10 +194,10 @@ private void writeEndpointSpecificResolver(String region, ObjectNode resolved) {
175194

176195
private final class Partition {
177196
final ObjectNode defaults;
178-
final String hostnameTemplate;
179197
final String dnsSuffix;
180198
final String identifier;
181199
final String regionRegex;
200+
final String hostnameTemplate;
182201
private final ObjectNode config;
183202

184203
private Partition(ObjectNode config, String partition) {
@@ -187,15 +206,15 @@ private Partition(ObjectNode config, String partition) {
187206
ObjectNode partitionDefaults = config.expectObjectMember("defaults");
188207
defaults = partitionDefaults.merge(getService().getObjectMember("defaults").orElse(Node.objectNode()));
189208

209+
dnsSuffix = config.expectStringMember("dnsSuffix").getValue();
210+
identifier = partition;
211+
regionRegex = config.expectStringMember("regionRegex").getValue();
212+
190213
// Resolve the template to use for this service in this partition.
191214
String template = defaults.expectStringMember("hostname").getValue();
192215
template = template.replace("{service}", endpointPrefix);
193-
template = template.replace("{dnsSuffix}", config.expectStringMember("dnsSuffix").getValue());
216+
template = template.replace("{dnsSuffix}", dnsSuffix);
194217
hostnameTemplate = template;
195-
196-
dnsSuffix = config.expectStringMember("dnsSuffix").getValue();
197-
identifier = partition;
198-
regionRegex = config.expectStringMember("regionRegex").getValue();
199218
}
200219

201220
ObjectNode getDefaults() {
@@ -220,6 +239,27 @@ Set<String> getAllRegions() {
220239
return regions;
221240
}
222241

242+
List<Node> getVariants() {
243+
List<Node> allVariants = new ArrayList<Node>();
244+
245+
if (defaults.containsMember("variants")) {
246+
ArrayNode variants = defaults.expectArrayMember("variants");
247+
variants.forEach(variant -> {
248+
ObjectNode variantNode = variant.expectObjectNode();
249+
String hostname = variantNode.expectStringMember("hostname").getValue();
250+
if (variantNode.containsMember("dnsSuffix")) {
251+
String dnsSuffix = variantNode.expectStringMember("dnsSuffix").getValue();
252+
hostname = hostname.replace("{dnsSuffix}", dnsSuffix);
253+
}
254+
hostname = hostname.replace("{service}", endpointPrefix);
255+
hostname = hostname.replace("{dnsSuffix}", dnsSuffix);
256+
allVariants.add(variantNode.withMember("hostname", hostname).withoutMember("dnsSuffix"));
257+
});
258+
}
259+
260+
return allVariants;
261+
}
262+
223263
Optional<String> getPartitionEndpoint() {
224264
ObjectNode service = getService();
225265
// Note: regionalized services always use regionalized endpoints.

0 commit comments

Comments
 (0)