From 2e3860fc1ea4160e35165e25e3a3bddfcdf7cc69 Mon Sep 17 00:00:00 2001 From: Xinsong Cui Date: Thu, 30 Jan 2025 16:33:37 -0500 Subject: [PATCH] misc: awsprofile enum parser (#1507) * add getEnumOrNull for AwsProfile * address pr review * add quote * style * lint --- .../runtime/config/profile/AwsProfile.kt | 57 ++++++++----------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt b/aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt index b5eb254683a..1f69799c31e 100644 --- a/aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt +++ b/aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt @@ -83,14 +83,7 @@ public val AwsProfile.credentialProcess: String? */ @InternalSdkApi public val AwsProfile.retryMode: RetryMode? - get() = getOrNull("retry_mode")?.run { - RetryMode.values().firstOrNull { it.name.equals(this, ignoreCase = true) } - ?: throw ConfigurationException( - "retry_mode $this is not supported, should be one of: ${ - RetryMode.values().joinToString(", ") { it.name.lowercase() } - }", - ) - } + get() = getEnumOrNull("retry_mode") /** * Whether service clients should make requests to the FIPS endpoint variant. @@ -139,14 +132,7 @@ public val AwsProfile.sdkUserAgentAppId: String? */ @InternalSdkApi public val AwsProfile.accountIdEndpointMode: AccountIdEndpointMode? - get() = getOrNull("account_id_endpoint_mode")?.run { - AccountIdEndpointMode.values().firstOrNull { it.name.equals(this, ignoreCase = true) } - ?: throw ConfigurationException( - "account_id_endpoint_mode $this is not supported, should be one of: ${ - AccountIdEndpointMode.values().joinToString(", ") { it.name.lowercase() } - }", - ) - } + get() = getEnumOrNull("account_id_endpoint_mode") /** * Determines when a request should be compressed or not @@ -174,30 +160,14 @@ public val AwsProfile.sigV4aSigningRegionSet: String? */ @InternalSdkApi public val AwsProfile.requestChecksumCalculation: RequestHttpChecksumConfig? - get() = getOrNull("request_checksum_calculation")?.run { - RequestHttpChecksumConfig - .values() - .firstOrNull { it.name.equals(this, ignoreCase = true) } - ?: throw ConfigurationException( - "request_checksum_calculation $this is not supported, should be one of: " + - RequestHttpChecksumConfig.values().joinToString(", ") { it.name.lowercase() }, - ) - } + get() = getEnumOrNull("request_checksum_calculation") /** * Configures response checksum validation */ @InternalSdkApi public val AwsProfile.responseChecksumValidation: ResponseHttpChecksumConfig? - get() = getOrNull("response_checksum_validation")?.run { - ResponseHttpChecksumConfig - .values() - .firstOrNull { it.name.equals(this, ignoreCase = true) } - ?: throw ConfigurationException( - "response_checksum_validation $this is not supported, should be one of: " + - ResponseHttpChecksumConfig.values().joinToString(", ") { it.name.lowercase() }, - ) - } + get() = getEnumOrNull("response_checksum_validation") /** * Parse a config value as a boolean, ignoring case. @@ -232,6 +202,25 @@ public fun AwsProfile.getLongOrNull(key: String, subKey: String? = null): Long? ) } +/** + * Parse a config value as an enum. + */ +@InternalSdkApi +public inline fun > AwsProfile.getEnumOrNull(key: String, subKey: String? = null): T? = + getOrNull(key, subKey)?.let { value -> + enumValues().firstOrNull { + it.name.equals(value, ignoreCase = true) + } ?: throw ConfigurationException( + buildString { + append(key) + append(" '") + append(value) + append("' is not supported, should be one of: ") + enumValues().joinTo(this) { it.name.lowercase() } + }, + ) + } + internal fun AwsProfile.getUrlOrNull(key: String, subKey: String? = null): Url? = getOrNull(key, subKey)?.let { try {