Skip to content

Commit accc26b

Browse files
committed
Updating to use ImmutableMap. Adding javadoc where missing
1 parent e7ea508 commit accc26b

File tree

16 files changed

+320
-415
lines changed

16 files changed

+320
-415
lines changed

codegen-lite-maven-plugin/src/main/java/software/amazon/awssdk/codegen/lite/maven/plugin/RegionGenerationMojo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class RegionGenerationMojo extends AbstractMojo {
5656

5757
public void execute() throws MojoExecutionException {
5858
Path baseSourcesDirectory = Paths.get(outputDirectory).resolve("generated-sources").resolve("sdk");
59+
Path testsDirectory = Paths.get(outputDirectory).resolve("generated-test-sources").resolve("sdk-tests");
5960

6061
Partitions partitions = RegionMetadataLoader.build(endpoints);
6162

@@ -66,6 +67,7 @@ public void execute() throws MojoExecutionException {
6667
generateServiceProvider(baseSourcesDirectory, partitions);
6768

6869
project.addCompileSourceRoot(baseSourcesDirectory.toFile().getAbsolutePath());
70+
project.addTestCompileSourceRoot(testsDirectory.toFile().getAbsolutePath());
6971
}
7072

7173
public void generateRegionClass(Path baseSourcesDirectory, Partitions partitions) {

codegen-lite/src/main/java/software/amazon/awssdk/codegen/lite/regions/RegionGenerator.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public RegionGenerator(Partitions partitions,
5656
public TypeSpec poetClass() {
5757
TypeSpec.Builder builder = TypeSpec.classBuilder(className())
5858
.addModifiers(FINAL, PUBLIC)
59+
.addJavadoc(documentation())
5960
.addAnnotation(SdkPublicApi.class)
6061
.addAnnotation(AnnotationSpec.builder(Generated.class)
6162
.addMember("value",
@@ -215,6 +216,30 @@ private TypeSpec cache() {
215216
.build();
216217
}
217218

219+
private CodeBlock documentation() {
220+
return CodeBlock.builder()
221+
.add("An Amazon Web Services region that hosts a set of Amazon services.")
222+
.add(System.lineSeparator())
223+
.add("<p>An instance of this class can be retrieved by referencing one of the static constants defined in"
224+
+ " this class (eg. {@link Region#US_EAST_1}) or by using the {@link Region#of(String)} method if "
225+
+ "the region you want is not included in this release of the SDK.</p>")
226+
.add(System.lineSeparator())
227+
.add("<p>Each AWS region corresponds to a separate geographical location where a set of Amazon services "
228+
+ "is deployed. These regions (except for the special {@link #AWS_GLOBAL} and {@link #AWS_CN_GLOBAL}"
229+
+ " regions) are separate from each other, with their own set of resources. This means a resource "
230+
+ "created in one region (eg. an SQS queue) is not available in another region.</p>")
231+
.add(System.lineSeparator())
232+
.add("<p>To programmatically determine whether a particular service is deployed to a region, you can use "
233+
+ "the {@code serviceMetadata} method on the service's client interface. Additional metadata about "
234+
+ "a region can be discovered using {@link RegionMetadata#of(Region)}.</p>")
235+
.add(System.lineSeparator())
236+
.add("<p>The {@link Region#id()} will be used as the signing region for all requests to AWS services "
237+
+ "unless an explicit region override is available in {@link RegionMetadata}. This id will also be "
238+
+ "used to construct the endpoint for accessing a service unless an explicit endpoint is available "
239+
+ "for that region in {@link RegionMetadata}.</p>")
240+
.build();
241+
}
242+
218243
@Override
219244
public ClassName className() {
220245
return ClassName.get(basePackage, "Region");

codegen-lite/src/main/java/software/amazon/awssdk/codegen/lite/regions/RegionMetadataProviderGenerator.java

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

1616
package software.amazon.awssdk.codegen.lite.regions;
1717

18-
import static java.util.AbstractMap.SimpleEntry;
1918
import static javax.lang.model.element.Modifier.FINAL;
2019
import static javax.lang.model.element.Modifier.PRIVATE;
2120
import static javax.lang.model.element.Modifier.PUBLIC;
@@ -29,9 +28,6 @@
2928
import com.squareup.javapoet.ParameterizedTypeName;
3029
import com.squareup.javapoet.TypeName;
3130
import com.squareup.javapoet.TypeSpec;
32-
import java.util.ArrayList;
33-
import java.util.Collections;
34-
import java.util.List;
3531
import java.util.Locale;
3632
import java.util.Map;
3733
import java.util.stream.Collectors;
@@ -41,6 +37,7 @@
4137
import software.amazon.awssdk.codegen.lite.PoetClass;
4238
import software.amazon.awssdk.codegen.lite.Utils;
4339
import software.amazon.awssdk.codegen.lite.regions.model.Partitions;
40+
import software.amazon.awssdk.utils.ImmutableMap;
4441

4542
public class RegionMetadataProviderGenerator implements PoetClass {
4643

@@ -63,6 +60,7 @@ public TypeSpec poetClass() {
6360
ClassName.get(regionBasePackage, "RegionMetadata"));
6461
return TypeSpec.classBuilder(className())
6562
.addModifiers(PUBLIC)
63+
.addSuperinterface(ClassName.get(regionBasePackage, "RegionMetadataProvider"))
6664
.addAnnotation(AnnotationSpec.builder(Generated.class)
6765
.addMember("value", "$S", "software.amazon.awssdk:codegen")
6866
.build())
@@ -78,29 +76,19 @@ public TypeSpec poetClass() {
7876

7977
@Override
8078
public ClassName className() {
81-
return ClassName.get(regionBasePackage, "RegionMetadataProvider");
79+
return ClassName.get(regionBasePackage, "GeneratedRegionMetadataProvider");
8280
}
8381

8482
private CodeBlock regions(Partitions partitions) {
85-
CodeBlock.Builder builder = CodeBlock.builder().add("$T.unmodifiableMap($T.of(", Collections.class, Stream.class);
83+
CodeBlock.Builder builder = CodeBlock.builder().add("$T.<Region, RegionMetadata>builder()", ImmutableMap.class);
8684

87-
List<String> regions = new ArrayList<>();
85+
partitions.getPartitions()
86+
.stream()
87+
.forEach(p -> p.getRegions()
88+
.keySet()
89+
.forEach(r -> builder.add(".put(Region.$L, new $T())", regionClass(r), regionMetadataClass(r))));
8890

89-
partitions.getPartitions().stream().forEach(p -> regions.addAll(p.getRegions().keySet()));
90-
91-
for (int i = 0; i < regions.size() - 1; i++) {
92-
builder.add("new $T<>(Region.$L, new $T()),",
93-
SimpleEntry.class,
94-
regionClass(regions.get(i)),
95-
regionMetadataClass(regions.get(i)));
96-
}
97-
98-
builder.add("new $T<>(Region.$L, new $T())",
99-
SimpleEntry.class,
100-
regionClass(regions.get(regions.size() - 1)),
101-
regionMetadataClass(regions.get(regions.size() - 1)));
102-
103-
return builder.add(").collect($T.toMap((e) -> e.getKey(), (e) -> e.getValue())))", Collectors.class).build();
91+
return builder.add(".build()").build();
10492
}
10593

10694
private String regionClass(String region) {
@@ -113,7 +101,7 @@ private ClassName regionMetadataClass(String region) {
113101

114102
private MethodSpec getter() {
115103
return MethodSpec.methodBuilder("regionMetadata")
116-
.addModifiers(PUBLIC, STATIC)
104+
.addModifiers(PUBLIC)
117105
.addParameter(ClassName.get(regionBasePackage, "Region"), "region")
118106
.returns(ClassName.get(regionBasePackage, "RegionMetadata"))
119107
.addStatement("return $L.get($L)", "REGION_METADATA", "region")

codegen-lite/src/main/java/software/amazon/awssdk/codegen/lite/regions/ServiceMetadataGenerator.java

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
package software.amazon.awssdk.codegen.lite.regions;
1717

18-
import static java.util.AbstractMap.SimpleEntry;
19-
2018
import static javax.lang.model.element.Modifier.FINAL;
2119
import static javax.lang.model.element.Modifier.PRIVATE;
2220
import static javax.lang.model.element.Modifier.STATIC;
@@ -33,7 +31,6 @@
3331
import java.util.ArrayList;
3432
import java.util.Arrays;
3533
import java.util.Collections;
36-
import java.util.HashMap;
3734
import java.util.List;
3835
import java.util.Locale;
3936
import java.util.Map;
@@ -46,6 +43,7 @@
4643
import software.amazon.awssdk.codegen.lite.Utils;
4744
import software.amazon.awssdk.codegen.lite.regions.model.Partitions;
4845
import software.amazon.awssdk.codegen.lite.regions.model.Service;
46+
import software.amazon.awssdk.utils.ImmutableMap;
4947

5048
public class ServiceMetadataGenerator implements PoetClass {
5149

@@ -110,25 +108,17 @@ public ClassName className() {
110108
private CodeBlock serviceEndpoints(Partitions partitions) {
111109
List<Service> services = getServiceData(partitions);
112110

113-
String endpointsCodeBlock = services.stream()
114-
.flatMap(s -> s.getEndpoints()
115-
.entrySet()
116-
.stream()
117-
.filter(e -> e.getValue().getHostname() != null)
118-
.map(e -> "new $1T<>(\"" + e.getKey() + "\", " +
119-
"\"" + e.getValue().getHostname() + "\")"))
120-
.collect(Collectors.joining(", "));
121-
122-
CodeBlock.Builder builder = CodeBlock.builder();
123-
124-
if (endpointsCodeBlock.length() > 0) {
125-
builder.add("$T.unmodifiableMap($T.of(", Collections.class, Stream.class);
126-
builder.add(endpointsCodeBlock, SimpleEntry.class);
127-
builder.add(").collect($T.toMap((e) -> e.getKey(), (e) -> e.getValue())))", Collectors.class).build();
128-
return builder.build();
129-
}
111+
CodeBlock.Builder builder = CodeBlock.builder().add("$T.<String, String>builder()", ImmutableMap.class);
112+
113+
services.stream()
114+
.forEach(s -> s.getEndpoints()
115+
.entrySet()
116+
.stream()
117+
.filter(e -> e.getValue().getHostname() != null)
118+
.forEach(e -> builder.add(".put(\"" + e.getKey() + "\", " +
119+
"\"" + e.getValue().getHostname() + "\")")));
130120

131-
return builder.add("new $T()", HashMap.class).build();
121+
return builder.add(".build()").build();
132122
}
133123

134124
private CodeBlock regionsField(Partitions partitions) {
@@ -155,27 +145,19 @@ private CodeBlock regionsField(Partitions partitions) {
155145
private CodeBlock signingRegionOverrides(Partitions partitions) {
156146
List<Service> serviceData = getServiceData(partitions);
157147

158-
String data = serviceData.stream()
159-
.flatMap(s -> s.getEndpoints()
160-
.entrySet()
161-
.stream()
162-
.filter(e -> e.getValue().getCredentialScope() != null)
163-
.filter(e -> e.getValue().getCredentialScope().getRegion() != null)
164-
.map(fm ->
165-
"new $1T<>(\"" + fm.getKey() + "\", \"" +
166-
fm.getValue().getCredentialScope().getRegion() + "\")"
167-
)).collect(Collectors.joining(", "));
168-
169-
CodeBlock.Builder builder = CodeBlock.builder();
170-
171-
if (data.length() > 0) {
172-
builder.add("$T.unmodifiableMap($T.of(", Collections.class, Stream.class);
173-
builder.add(data, SimpleEntry.class);
174-
builder.add(").collect($T.toMap((e) -> e.getKey(), (e) -> e.getValue())))", Collectors.class).build();
175-
return builder.build();
176-
}
148+
CodeBlock.Builder builder = CodeBlock.builder().add("$T.<String, String>builder()", ImmutableMap.class);
149+
150+
serviceData.stream()
151+
.forEach(s -> s.getEndpoints()
152+
.entrySet()
153+
.stream()
154+
.filter(e -> e.getValue().getCredentialScope() != null)
155+
.filter(e -> e.getValue().getCredentialScope().getRegion() != null)
156+
.forEach(fm ->
157+
builder.add(".put(\"" + fm.getKey() + "\", \"" +
158+
fm.getValue().getCredentialScope().getRegion() + "\")")));
177159

178-
return CodeBlock.builder().add("new $T()", HashMap.class).build();
160+
return builder.add(".build()").build();
179161
}
180162

181163
private MethodSpec regions() {
@@ -195,8 +177,9 @@ private MethodSpec endpointFor() {
195177
.addParameter(ClassName.get(regionBasePackage, "Region"), "region")
196178
.addAnnotation(Override.class)
197179
.returns(URI.class)
198-
.addStatement("return $T.create(REGION_OVERRIDDEN_ENDPOINTS.getOrDefault(region.id(), " +
199-
"computeEndpoint(ENDPOINT_PREFIX, region)))",
180+
.addStatement("return $T.create(REGION_OVERRIDDEN_ENDPOINTS.containsKey(region.id()) ? "
181+
+ "REGION_OVERRIDDEN_ENDPOINTS.get(region.id()) : "
182+
+ "computeEndpoint(ENDPOINT_PREFIX, region))",
200183
URI.class)
201184
.build();
202185
}

codegen-lite/src/main/java/software/amazon/awssdk/codegen/lite/regions/ServiceMetadataProviderGenerator.java

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

1616
package software.amazon.awssdk.codegen.lite.regions;
1717

18-
import static java.util.AbstractMap.SimpleEntry;
1918
import static javax.lang.model.element.Modifier.FINAL;
2019
import static javax.lang.model.element.Modifier.PRIVATE;
2120
import static javax.lang.model.element.Modifier.PUBLIC;
@@ -29,17 +28,17 @@
2928
import com.squareup.javapoet.ParameterizedTypeName;
3029
import com.squareup.javapoet.TypeName;
3130
import com.squareup.javapoet.TypeSpec;
32-
import java.util.ArrayList;
33-
import java.util.Collections;
34-
import java.util.List;
31+
import java.util.HashSet;
3532
import java.util.Map;
33+
import java.util.Set;
3634
import java.util.stream.Collectors;
3735
import java.util.stream.Stream;
3836
import software.amazon.awssdk.annotations.Generated;
3937
import software.amazon.awssdk.annotations.SdkPublicApi;
4038
import software.amazon.awssdk.codegen.lite.PoetClass;
4139
import software.amazon.awssdk.codegen.lite.Utils;
4240
import software.amazon.awssdk.codegen.lite.regions.model.Partitions;
41+
import software.amazon.awssdk.utils.ImmutableMap;
4342

4443
public class ServiceMetadataProviderGenerator implements PoetClass {
4544

@@ -62,6 +61,7 @@ public TypeSpec poetClass() {
6261
ClassName.get(regionBasePackage, "ServiceMetadata"));
6362
return TypeSpec.classBuilder(className())
6463
.addModifiers(PUBLIC)
64+
.addSuperinterface(ClassName.get(regionBasePackage, "ServiceMetadataProvider"))
6565
.addAnnotation(AnnotationSpec.builder(Generated.class)
6666
.addMember("value", "$S", "software.amazon.awssdk:codegen")
6767
.build())
@@ -77,29 +77,27 @@ public TypeSpec poetClass() {
7777

7878
@Override
7979
public ClassName className() {
80-
return ClassName.get(regionBasePackage, "ServiceMetadataProvider");
80+
return ClassName.get(regionBasePackage, "GeneratedServiceMetadataProvider");
8181
}
8282

8383
private CodeBlock regions(Partitions partitions) {
84-
CodeBlock.Builder builder = CodeBlock.builder().add("$T.unmodifiableMap($T.of(", Collections.class, Stream.class);
85-
86-
List<String> services = new ArrayList<>();
87-
88-
partitions.getPartitions().stream().forEach(p -> services.addAll(p.getServices().keySet()));
89-
90-
for (int i = 0; i < services.size() - 1; i++) {
91-
builder.add("new $T<>($S, new $T()),",
92-
SimpleEntry.class,
93-
services.get(i),
94-
serviceMetadataClass(services.get(i)));
95-
}
96-
97-
builder.add("new $T<>($S, new $T())",
98-
SimpleEntry.class,
99-
services.get(services.size() - 1),
100-
serviceMetadataClass(services.get(services.size() - 1)));
101-
102-
return builder.add(").collect($T.toMap((e) -> e.getKey(), (e) -> e.getValue())))", Collectors.class).build();
84+
CodeBlock.Builder builder = CodeBlock.builder().add("$T.<String, ServiceMetadata>builder()", ImmutableMap.class);
85+
86+
Set<String> seenServices = new HashSet<>();
87+
88+
partitions.getPartitions()
89+
.stream()
90+
.forEach(p -> p.getServices()
91+
.keySet()
92+
.stream()
93+
.forEach(s -> {
94+
if (!seenServices.contains(s)) {
95+
builder.add(".put($S, new $T())", s, serviceMetadataClass(s));
96+
seenServices.add(s);
97+
}
98+
}));
99+
100+
return builder.add(".build()").build();
103101
}
104102

105103
private ClassName serviceMetadataClass(String service) {
@@ -111,7 +109,7 @@ private ClassName serviceMetadataClass(String service) {
111109

112110
private MethodSpec getter() {
113111
return MethodSpec.methodBuilder("serviceMetadata")
114-
.addModifiers(PUBLIC, STATIC)
112+
.addModifiers(PUBLIC)
115113
.addParameter(String.class, "endpointPrefix")
116114
.returns(ClassName.get(regionBasePackage, "ServiceMetadata"))
117115
.addStatement("return $L.get($L)", "SERVICE_METADATA", "endpointPrefix")

0 commit comments

Comments
 (0)