-
Notifications
You must be signed in to change notification settings - Fork 914
[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 all commits
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
255 changes: 255 additions & 0 deletions
255
.../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,255 @@ | ||
/* | ||
* 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.ParameterizedTypeName; | ||
import com.squareup.javapoet.TypeSpec; | ||
import java.util.EnumMap; | ||
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 static final String DEFAULT_CONFIG_BY_MODE_ENUM_MAP = "DEFAULT_CONFIG_BY_MODE"; | ||
private static final String DEFAULT_HTTP_CONFIG_BY_MODE_ENUM_MAP = "DEFAULT_HTTP_CONFIG_BY_MODE"; | ||
private static final String DEFAULTS_VAR_SUFFIX = "_DEFAULTS"; | ||
private static final String HTTP_DEFAULTS_VAR_SUFFIX = "_HTTP_DEFAULTS"; | ||
private static final Map<String, OptionMetadata> CONFIGURATION_MAPPING = new HashMap<>(); | ||
private static final Map<String, OptionMetadata> HTTP_CONFIGURATION_MAPPING = new HashMap<>(); | ||
private final String basePackage; | ||
private final String defaultsModeBase; | ||
private final DefaultConfiguration configuration; | ||
|
||
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(defaultConfigMethod(DEFAULT_CONFIG_BY_MODE_ENUM_MAP, "defaultConfig")) | ||
.addMethod(defaultConfigMethod(DEFAULT_HTTP_CONFIG_BY_MODE_ENUM_MAP, | ||
"defaultHttpConfig")) | ||
.addMethod(createConstructor()); | ||
|
||
|
||
configuration.modeDefaults().entrySet().forEach(entry -> { | ||
builder.addField(addDefaultsFieldForMode(entry)); | ||
builder.addField(addHttpDefaultsFieldForMode(entry)); | ||
}); | ||
|
||
addDefaultsFieldForLegacy(builder, "LEGACY_DEFAULTS"); | ||
addDefaultsFieldForLegacy(builder, "LEGACY_HTTP_DEFAULTS"); | ||
|
||
addEnumMapField(builder, DEFAULT_CONFIG_BY_MODE_ENUM_MAP); | ||
addEnumMapField(builder, DEFAULT_HTTP_CONFIG_BY_MODE_ENUM_MAP); | ||
|
||
addStaticEnumMapBlock(builder); | ||
return builder.build(); | ||
} | ||
|
||
private void addStaticEnumMapBlock(TypeSpec.Builder builder) { | ||
CodeBlock.Builder staticCodeBlock = CodeBlock.builder(); | ||
|
||
putItemsToEnumMap(staticCodeBlock, configuration.modeDefaults().keySet(), DEFAULTS_VAR_SUFFIX, | ||
DEFAULT_CONFIG_BY_MODE_ENUM_MAP); | ||
putItemsToEnumMap(staticCodeBlock, configuration.modeDefaults().keySet(), HTTP_DEFAULTS_VAR_SUFFIX, | ||
DEFAULT_HTTP_CONFIG_BY_MODE_ENUM_MAP); | ||
|
||
builder.addStaticBlock(staticCodeBlock.build()); | ||
} | ||
|
||
private void addEnumMapField(TypeSpec.Builder builder, String name) { | ||
ParameterizedTypeName map = ParameterizedTypeName.get(ClassName.get(Map.class), | ||
defaultsModeClassName(), | ||
ClassName.get(AttributeMap.class)); | ||
FieldSpec field = FieldSpec.builder(map, name, PRIVATE, STATIC, FINAL) | ||
.initializer("new $T<>(DefaultsMode.class)", EnumMap.class).build(); | ||
builder.addField(field); | ||
} | ||
|
||
private void putItemsToEnumMap(CodeBlock.Builder codeBlock, Set<String> modes, String suffix, String mapName) { | ||
modes.forEach(m -> { | ||
String mode = sanitizeMode(m); | ||
codeBlock.addStatement("$N.put(DefaultsMode.$N, $N)", mapName, mode, mode + suffix); | ||
}); | ||
|
||
// Add LEGACY since LEGACY is not in the modes set | ||
codeBlock.addStatement("$N.put(DefaultsMode.LEGACY, LEGACY$N)", mapName, suffix); | ||
} | ||
|
||
@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_VAR_SUFFIX; | ||
|
||
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_VAR_SUFFIX; | ||
|
||
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 defaultConfigMethod(String enumMap, String methodName) { | ||
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName) | ||
.returns(AttributeMap.class) | ||
.addModifiers(PUBLIC, STATIC) | ||
.addJavadoc("Return the default config options for a given defaults " | ||
+ "mode") | ||
.addParameter(defaultsModeClassName(), "mode") | ||
.addStatement("return $N.getOrDefault(mode, $T.empty())", | ||
enumMap, AttributeMap.class); | ||
|
||
return methodBuilder.build(); | ||
} | ||
|
||
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; | ||
|
||
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
80 changes: 80 additions & 0 deletions
80
...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,80 @@ | ||
package software.amazon.awssdk.defaultsmode; | ||
|
||
import java.time.Duration; | ||
import java.util.EnumMap; | ||
import java.util.Map; | ||
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 static final Map<DefaultsMode, AttributeMap> DEFAULT_CONFIG_BY_MODE = new EnumMap<>(DefaultsMode.class); | ||
|
||
private static final Map<DefaultsMode, AttributeMap> DEFAULT_HTTP_CONFIG_BY_MODE = new EnumMap<>(DefaultsMode.class); | ||
|
||
static { | ||
DEFAULT_CONFIG_BY_MODE.put(DefaultsMode.STANDARD, STANDARD_DEFAULTS); | ||
DEFAULT_CONFIG_BY_MODE.put(DefaultsMode.MOBILE, MOBILE_DEFAULTS); | ||
DEFAULT_CONFIG_BY_MODE.put(DefaultsMode.CROSS_REGION, CROSS_REGION_DEFAULTS); | ||
DEFAULT_CONFIG_BY_MODE.put(DefaultsMode.IN_REGION, IN_REGION_DEFAULTS); | ||
DEFAULT_CONFIG_BY_MODE.put(DefaultsMode.LEGACY, LEGACY_DEFAULTS); | ||
DEFAULT_HTTP_CONFIG_BY_MODE.put(DefaultsMode.STANDARD, STANDARD_HTTP_DEFAULTS); | ||
DEFAULT_HTTP_CONFIG_BY_MODE.put(DefaultsMode.MOBILE, MOBILE_HTTP_DEFAULTS); | ||
DEFAULT_HTTP_CONFIG_BY_MODE.put(DefaultsMode.CROSS_REGION, CROSS_REGION_HTTP_DEFAULTS); | ||
DEFAULT_HTTP_CONFIG_BY_MODE.put(DefaultsMode.IN_REGION, IN_REGION_HTTP_DEFAULTS); | ||
DEFAULT_HTTP_CONFIG_BY_MODE.put(DefaultsMode.LEGACY, LEGACY_HTTP_DEFAULTS); | ||
} | ||
|
||
private DefaultsModeConfiguration() { | ||
} | ||
|
||
/** | ||
* Return the default config options for a given defaults mode | ||
*/ | ||
public static AttributeMap defaultConfig(DefaultsMode mode) { | ||
return DEFAULT_CONFIG_BY_MODE.getOrDefault(mode, AttributeMap.empty()); | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Return the default config options for a given defaults mode | ||
*/ | ||
public static AttributeMap defaultHttpConfig(DefaultsMode mode) { | ||
return DEFAULT_HTTP_CONFIG_BY_MODE.getOrDefault(mode, AttributeMap.empty()); | ||
} | ||
} |
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.