-
Notifications
You must be signed in to change notification settings - Fork 1k
[Default Configuration Part 3]: add defaults from defaults mode to the configuration resolution chain #2803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zoewangg
merged 4 commits into
feature/master/defaults-mode
from
zoewang/defaults-mode-3
Nov 2, 2021
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
257 changes: 257 additions & 0 deletions
257
.../software/amazon/awssdk/codegen/lite/defaultsmode/DefaultsModeConfigurationGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.codegen.lite.defaultsmode; | ||
|
|
||
| import static javax.lang.model.element.Modifier.FINAL; | ||
| import static javax.lang.model.element.Modifier.PRIVATE; | ||
| import static javax.lang.model.element.Modifier.PUBLIC; | ||
| import static javax.lang.model.element.Modifier.STATIC; | ||
|
|
||
| import com.squareup.javapoet.AnnotationSpec; | ||
| import com.squareup.javapoet.ClassName; | ||
| import com.squareup.javapoet.CodeBlock; | ||
| import com.squareup.javapoet.FieldSpec; | ||
| import com.squareup.javapoet.MethodSpec; | ||
| import com.squareup.javapoet.TypeSpec; | ||
| import java.util.HashMap; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import javax.lang.model.element.Modifier; | ||
| import software.amazon.awssdk.annotations.Generated; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.codegen.lite.PoetClass; | ||
| import software.amazon.awssdk.utils.AttributeMap; | ||
|
|
||
| /** | ||
| * Generates DefaultsModeConfiguration class that contains default options for each mode | ||
| */ | ||
| public class DefaultsModeConfigurationGenerator implements PoetClass { | ||
|
|
||
| private final String basePackage; | ||
| private final String defaultsModeBase; | ||
| private final DefaultConfiguration configuration; | ||
|
|
||
| private static final Map<String, OptionMetadata> CONFIGURATION_MAPPING = new HashMap<>(); | ||
|
|
||
| private static final Map<String, OptionMetadata> HTTP_CONFIGURATION_MAPPING = new HashMap<>(); | ||
|
|
||
| static { | ||
| HTTP_CONFIGURATION_MAPPING.put("connectTimeoutInMillis", | ||
| new OptionMetadata(ClassName.get("java.time", "Duration"), | ||
| ClassName.get("software.amazon.awssdk.http", | ||
| "SdkHttpConfigurationOption", "CONNECTION_TIMEOUT"))); | ||
| CONFIGURATION_MAPPING.put("retryMode", new OptionMetadata(ClassName.get("software.amazon.awssdk.core.retry", "RetryMode" | ||
| ), ClassName.get("software.amazon.awssdk.core.client.config","SdkClientOption", "DEFAULT_RETRY_MODE"))); | ||
| } | ||
|
|
||
| public DefaultsModeConfigurationGenerator(String basePackage, String defaultsModeBase, DefaultConfiguration configuration) { | ||
| this.basePackage = basePackage; | ||
| this.configuration = configuration; | ||
| this.defaultsModeBase = defaultsModeBase; | ||
| } | ||
|
|
||
| @Override | ||
| public TypeSpec poetClass() { | ||
| TypeSpec.Builder builder = TypeSpec.classBuilder(className()) | ||
| .addModifiers(PUBLIC, FINAL) | ||
| .addJavadoc(documentation()) | ||
| .addAnnotation(SdkInternalApi.class) | ||
| .addAnnotation(AnnotationSpec.builder(Generated.class) | ||
| .addMember("value", | ||
| "$S", | ||
| "software.amazon.awssdk:codegen") | ||
| .build()) | ||
| .addMethod(defaultHttpConfigMethod(configuration.modeDefaults().keySet())) | ||
| .addMethod(defaultSdkConfigMethod(configuration.modeDefaults().keySet())) | ||
| .addMethod(createConstructor()); | ||
|
|
||
|
|
||
| configuration.modeDefaults().entrySet().forEach(entry -> { | ||
| builder.addField(addDefaultsFieldForMode(entry)); | ||
| builder.addField(addHttpDefaultsFieldForMode(entry)); | ||
| }); | ||
|
|
||
| addDefaultsFieldForLegacy(builder, "LEGACY_DEFAULTS"); | ||
| addDefaultsFieldForLegacy(builder, "LEGACY_HTTP_DEFAULTS"); | ||
| return builder.build(); | ||
| } | ||
|
|
||
| @Override | ||
| public ClassName className() { | ||
| return ClassName.get(basePackage, "DefaultsModeConfiguration"); | ||
| } | ||
|
|
||
| private FieldSpec addDefaultsFieldForMode(Map.Entry<String, Map<String, String>> modeEntry) { | ||
| String mode = modeEntry.getKey(); | ||
| String fieldName = sanitizeMode(mode) + "_DEFAULTS"; | ||
|
|
||
|
|
||
| CodeBlock.Builder attributeBuilder = CodeBlock.builder() | ||
| .add("$T.builder()", AttributeMap.class); | ||
|
|
||
| modeEntry.getValue() | ||
| .entrySet() | ||
| .stream() | ||
| .filter(e -> CONFIGURATION_MAPPING.containsKey(e.getKey())) | ||
| .forEach(e -> attributeMapBuilder(e.getKey(), e.getValue(), attributeBuilder)); | ||
|
|
||
|
|
||
| FieldSpec.Builder fieldSpec = FieldSpec.builder(AttributeMap.class, fieldName, PRIVATE, STATIC, FINAL) | ||
| .initializer(attributeBuilder | ||
| .add(".build()") | ||
| .build()); | ||
|
|
||
|
|
||
| return fieldSpec.build(); | ||
| } | ||
|
|
||
| private void addDefaultsFieldForLegacy(TypeSpec.Builder builder, String name) { | ||
| FieldSpec field = FieldSpec.builder(AttributeMap.class, name, PRIVATE, STATIC, FINAL) | ||
| .initializer("$T.empty()", AttributeMap.class).build(); | ||
| builder.addField(field); | ||
| } | ||
|
|
||
| private void attributeMapBuilder(String option, String value, CodeBlock.Builder attributeBuilder) { | ||
| OptionMetadata optionMetadata = CONFIGURATION_MAPPING.get(option); | ||
| switch (option) { | ||
| case "retryMode": | ||
| attributeBuilder.add(".put($T, $T.$N)", optionMetadata.attribute, optionMetadata.type, value.toUpperCase(Locale.US)); | ||
| break; | ||
| default: | ||
| throw new IllegalStateException("Unsupported option " + option); | ||
| } | ||
| } | ||
|
|
||
| private void httpAttributeMapBuilder(String option, String value, CodeBlock.Builder attributeBuilder) { | ||
| OptionMetadata optionMetadata = HTTP_CONFIGURATION_MAPPING.get(option); | ||
| switch (option) { | ||
| case "connectTimeoutInMillis": | ||
| attributeBuilder.add(".put($T, $T.ofMillis($N))", optionMetadata.attribute, optionMetadata.type, value); | ||
| break; | ||
| default: | ||
| throw new IllegalStateException("Unsupported option " + option); | ||
| } | ||
| } | ||
|
|
||
| private FieldSpec addHttpDefaultsFieldForMode(Map.Entry<String, Map<String, String>> modeEntry) { | ||
| String mode = modeEntry.getKey(); | ||
| String fieldName = sanitizeMode(mode) + "_HTTP_DEFAULTS"; | ||
|
|
||
| CodeBlock.Builder attributeBuilder = CodeBlock.builder() | ||
| .add("$T.builder()", AttributeMap.class); | ||
|
|
||
| modeEntry.getValue() | ||
| .entrySet() | ||
| .stream() | ||
| .filter(e -> HTTP_CONFIGURATION_MAPPING.containsKey(e.getKey())) | ||
| .forEach(e -> httpAttributeMapBuilder(e.getKey(), e.getValue(), attributeBuilder)); | ||
|
|
||
| FieldSpec.Builder fieldSpec = FieldSpec.builder(AttributeMap.class, fieldName, PRIVATE, STATIC, FINAL) | ||
| .initializer(attributeBuilder | ||
| .add(".build()") | ||
| .build()); | ||
|
|
||
| return fieldSpec.build(); | ||
| } | ||
|
|
||
| private String sanitizeMode(String str) { | ||
| return str.replace('-', '_').toUpperCase(Locale.US); | ||
| } | ||
|
|
||
| private CodeBlock documentation() { | ||
| CodeBlock.Builder builder = CodeBlock.builder() | ||
| .add("Contains a collection of default configuration options for each " | ||
| + "DefaultsMode"); | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
|
|
||
| private MethodSpec defaultHttpConfigMethod(Set<String> modes) { | ||
| String nameSuffix = "_HTTP_DEFAULTS"; | ||
| MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("defaultHttpConfig") | ||
| .returns(AttributeMap.class) | ||
| .addModifiers(PUBLIC, STATIC) | ||
| .addJavadoc("Return the default HTTP config options for a given defaults " | ||
| + "mode") | ||
| .addParameter(defaultsModeClassName(), "mode") | ||
| .beginControlFlow("switch (mode)"); | ||
|
|
||
|
|
||
| addSwitchCaseForEachMode(modes, nameSuffix, methodBuilder); | ||
|
|
||
| addLegacyCase(methodBuilder, "LEGACY" + nameSuffix); | ||
|
|
||
| return methodBuilder | ||
| .addStatement("default: throw new IllegalArgumentException($S + $N)", "Unsupported mode: ", "mode") | ||
| .endControlFlow() | ||
| .build(); | ||
| } | ||
|
|
||
| private void addSwitchCaseForEachMode(Set<String> modes, String nameSuffix, MethodSpec.Builder methodBuilder) { | ||
| modes.forEach(m -> { | ||
| String mode = sanitizeMode(m); | ||
| methodBuilder.addCode("case $N:", mode); | ||
| methodBuilder.addStatement("return $N", mode + nameSuffix); | ||
| }); | ||
| } | ||
|
|
||
| private MethodSpec defaultSdkConfigMethod(Set<String> modes) { | ||
| String nameSuffix = "_DEFAULTS"; | ||
| MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("defaultConfig") | ||
| .returns(AttributeMap.class) | ||
| .addModifiers(PUBLIC, STATIC) | ||
| .addJavadoc("Return the default SDK config options for a given defaults " | ||
| + "mode") | ||
| .addParameter(defaultsModeClassName(), "mode") | ||
| .beginControlFlow("switch (mode)"); | ||
|
|
||
|
|
||
| addSwitchCaseForEachMode(modes, nameSuffix, methodBuilder); | ||
| addLegacyCase(methodBuilder, "LEGACY" + nameSuffix); | ||
|
|
||
| return methodBuilder | ||
| .addStatement("default: throw new IllegalArgumentException($S + $N)", "Unsupported mode: ", "mode") | ||
| .endControlFlow() | ||
| .build(); | ||
| } | ||
|
|
||
| private void addLegacyCase(MethodSpec.Builder methodBuilder, String name) { | ||
| methodBuilder.addCode("case LEGACY:"); | ||
| methodBuilder.addStatement("return $N", name); | ||
| } | ||
|
|
||
| private ClassName defaultsModeClassName() { | ||
| return ClassName.get(defaultsModeBase, "DefaultsMode"); | ||
| } | ||
|
|
||
| private MethodSpec createConstructor() { | ||
| return MethodSpec.constructorBuilder() | ||
| .addModifiers(Modifier.PRIVATE) | ||
| .build(); | ||
| } | ||
|
|
||
| private static final class OptionMetadata { | ||
| private final ClassName type; | ||
| private final ClassName attribute; | ||
|
|
||
| public OptionMetadata(ClassName type, ClassName attribute) { | ||
| this.type = type; | ||
| this.attribute = attribute; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...sources/software/amazon/awssdk/codegen/lite/defaultsmode/defaults-mode-configuration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package software.amazon.awssdk.defaultsmode; | ||
|
|
||
| import java.time.Duration; | ||
| import software.amazon.awssdk.annotations.Generated; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.core.client.config.SdkClientOption; | ||
| import software.amazon.awssdk.core.retry.RetryMode; | ||
| import software.amazon.awssdk.http.SdkHttpConfigurationOption; | ||
| import software.amazon.awssdk.utils.AttributeMap; | ||
|
|
||
| /** | ||
| * Contains a collection of default configuration options for each DefaultsMode | ||
| */ | ||
| @SdkInternalApi | ||
| @Generated("software.amazon.awssdk:codegen") | ||
| public final class DefaultsModeConfiguration { | ||
| private static final AttributeMap STANDARD_DEFAULTS = AttributeMap.builder() | ||
| .put(SdkClientOption.DEFAULT_RETRY_MODE, RetryMode.STANDARD).build(); | ||
|
|
||
| private static final AttributeMap STANDARD_HTTP_DEFAULTS = AttributeMap.builder() | ||
| .put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, Duration.ofMillis(2000)).build(); | ||
|
|
||
| private static final AttributeMap MOBILE_DEFAULTS = AttributeMap.builder() | ||
| .put(SdkClientOption.DEFAULT_RETRY_MODE, RetryMode.ADAPTIVE).build(); | ||
|
|
||
| private static final AttributeMap MOBILE_HTTP_DEFAULTS = AttributeMap.builder() | ||
| .put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, Duration.ofMillis(10000)).build(); | ||
|
|
||
| private static final AttributeMap CROSS_REGION_DEFAULTS = AttributeMap.builder() | ||
| .put(SdkClientOption.DEFAULT_RETRY_MODE, RetryMode.STANDARD).build(); | ||
|
|
||
| private static final AttributeMap CROSS_REGION_HTTP_DEFAULTS = AttributeMap.builder() | ||
| .put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, Duration.ofMillis(2800)).build(); | ||
|
|
||
| private static final AttributeMap IN_REGION_DEFAULTS = AttributeMap.builder() | ||
| .put(SdkClientOption.DEFAULT_RETRY_MODE, RetryMode.STANDARD).build(); | ||
|
|
||
| private static final AttributeMap IN_REGION_HTTP_DEFAULTS = AttributeMap.builder() | ||
| .put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, Duration.ofMillis(1000)).build(); | ||
|
|
||
| private static final AttributeMap LEGACY_DEFAULTS = AttributeMap.empty(); | ||
|
|
||
| private static final AttributeMap LEGACY_HTTP_DEFAULTS = AttributeMap.empty(); | ||
|
|
||
| private DefaultsModeConfiguration() { | ||
| } | ||
|
|
||
| /** | ||
| * Return the default HTTP config options for a given defaults mode | ||
| */ | ||
| public static AttributeMap defaultHttpConfig(DefaultsMode mode) { | ||
| switch (mode) { | ||
| case STANDARD: | ||
| return STANDARD_HTTP_DEFAULTS; | ||
| case MOBILE: | ||
| return MOBILE_HTTP_DEFAULTS; | ||
| case CROSS_REGION: | ||
| return CROSS_REGION_HTTP_DEFAULTS; | ||
| case IN_REGION: | ||
| return IN_REGION_HTTP_DEFAULTS; | ||
| case LEGACY: | ||
| return LEGACY_HTTP_DEFAULTS; | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported mode: " + mode); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return the default SDK config options for a given defaults mode | ||
| */ | ||
| public static AttributeMap defaultConfig(DefaultsMode mode) { | ||
| switch (mode) { | ||
| case STANDARD: | ||
| return STANDARD_DEFAULTS; | ||
| case MOBILE: | ||
| return MOBILE_DEFAULTS; | ||
| case CROSS_REGION: | ||
| return CROSS_REGION_DEFAULTS; | ||
| case IN_REGION: | ||
| return IN_REGION_DEFAULTS; | ||
| case LEGACY: | ||
| return LEGACY_DEFAULTS; | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported mode: " + mode); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.