Skip to content

Commit 19220a8

Browse files
committed
chore(codegen): populate variants in endpoints hashes
1 parent 482f921 commit 19220a8

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
});
@@ -159,10 +170,18 @@ private void writeEndpointProviderFunction() {
159170
}
160171

161172
private void writeEndpointSpecificResolver(String region, ObjectNode resolved) {
162-
if (resolved.containsMember("hostname") || resolved.containsMember("credentialScope")) {
173+
if (resolved.containsMember("variants")
174+
|| resolved.containsMember("hostname")
175+
|| resolved.containsMember("credentialScope")) {
163176
writer.openBlock("$S: {", "},", region, () -> {
164-
String hostname = resolved.expectStringMember("hostname").getValue();
165-
writer.write("hostname: $S,", hostname);
177+
if (resolved.containsMember("hostname")) {
178+
String hostname = resolved.expectStringMember("hostname").getValue();
179+
writer.write("hostname: $S,", hostname);
180+
}
181+
if (resolved.containsMember("variants")) {
182+
ArrayNode variants = resolved.expectArrayMember("variants");
183+
writer.write("variants: $L,", ArrayNode.prettyPrintJson(variants));
184+
}
166185
resolved.getObjectMember("credentialScope").ifPresent(scope -> {
167186
scope.getStringMember("region").ifPresent(signingRegion -> {
168187
writer.write("signingRegion: $S,", signingRegion);
@@ -177,10 +196,10 @@ private void writeEndpointSpecificResolver(String region, ObjectNode resolved) {
177196

178197
private final class Partition {
179198
final ObjectNode defaults;
180-
final String hostnameTemplate;
181199
final String dnsSuffix;
182200
final String identifier;
183201
final String regionRegex;
202+
final String hostnameTemplate;
184203
private final ObjectNode config;
185204

186205
private Partition(ObjectNode config, String partition) {
@@ -189,15 +208,15 @@ private Partition(ObjectNode config, String partition) {
189208
ObjectNode partitionDefaults = config.expectObjectMember("defaults");
190209
defaults = partitionDefaults.merge(getService().getObjectMember("defaults").orElse(Node.objectNode()));
191210

211+
dnsSuffix = config.expectStringMember("dnsSuffix").getValue();
212+
identifier = partition;
213+
regionRegex = config.expectStringMember("regionRegex").getValue();
214+
192215
// Resolve the template to use for this service in this partition.
193216
String template = defaults.expectStringMember("hostname").getValue();
194217
template = template.replace("{service}", endpointPrefix);
195-
template = template.replace("{dnsSuffix}", config.expectStringMember("dnsSuffix").getValue());
218+
template = template.replace("{dnsSuffix}", dnsSuffix);
196219
hostnameTemplate = template;
197-
198-
dnsSuffix = config.expectStringMember("dnsSuffix").getValue();
199-
identifier = partition;
200-
regionRegex = config.expectStringMember("regionRegex").getValue();
201220
}
202221

203222
ObjectNode getDefaults() {
@@ -222,6 +241,27 @@ Set<String> getAllRegions() {
222241
return regions;
223242
}
224243

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

0 commit comments

Comments
 (0)