|
16 | 16 | package software.amazon.smithy.aws.typescript.codegen;
|
17 | 17 |
|
18 | 18 | import java.util.ArrayList;
|
| 19 | +import java.util.Collections; |
19 | 20 | import java.util.List;
|
20 | 21 | import java.util.Map;
|
21 | 22 | import java.util.Optional;
|
@@ -93,15 +94,23 @@ private void loadServiceEndpoints() {
|
93 | 94 | for (Map.Entry<String, Node> entry : endpointMap.getStringMap().entrySet()) {
|
94 | 95 | ObjectNode config = entry.getValue().expectObjectNode();
|
95 | 96 | // 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); |
103 | 113 | }
|
104 |
| - endpoints.put(entry.getKey(), config); |
105 | 114 | }
|
106 | 115 | }
|
107 | 116 | }
|
@@ -136,7 +145,7 @@ private void writePartitionHash() {
|
136 | 145 | writer.write("regionRegex: $S,", partition.regionRegex);
|
137 | 146 | OptionalUtils.ifPresentOrElse(partition.getPartitionEndpoint(),
|
138 | 147 | 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. |
140 | 149 | () -> writer.write("hostname: $S,", partition.hostnameTemplate));
|
141 | 150 | List<Node> variants = partition.getVariants();
|
142 | 151 | if (!variants.isEmpty()) {
|
@@ -196,6 +205,13 @@ private void writeEndpointSpecificResolver(String region, ObjectNode resolved) {
|
196 | 205 | }
|
197 | 206 | }
|
198 | 207 |
|
| 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 | + |
199 | 215 | private final class Partition {
|
200 | 216 | final ObjectNode defaults;
|
201 | 217 | final String dnsSuffix;
|
@@ -259,6 +275,7 @@ List<Node> getVariants() {
|
259 | 275 | hostname = hostname.replace("{dnsSuffix}", dnsSuffix);
|
260 | 276 | allVariants.add(variantNode.withMember("hostname", hostname).withoutMember("dnsSuffix"));
|
261 | 277 | });
|
| 278 | + allVariants.add(getDefaultVariant(hostnameTemplate)); |
262 | 279 | }
|
263 | 280 |
|
264 | 281 | return allVariants;
|
|
0 commit comments