From ab79d836ec14c1d975a84892eda13fd0fca5ba55 Mon Sep 17 00:00:00 2001 From: Xinsong Cui Date: Thu, 16 Jan 2025 17:30:01 -0500 Subject: [PATCH 1/5] add getEnumOrNull for AwsProfile --- .../runtime/config/profile/AwsProfile.kt | 53 +++++++------------ 1 file changed, 19 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..e0d6f07d385 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,21 @@ 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( + "Value '$value' is not supported, should be one of: ${ + enumValues().joinToString(", ") { it.name.lowercase() } + }", + ) + } + internal fun AwsProfile.getUrlOrNull(key: String, subKey: String? = null): Url? = getOrNull(key, subKey)?.let { try { From 94c76e4b83393aae4de1f4400d4b4e715e6deb4b Mon Sep 17 00:00:00 2001 From: Xinsong Cui Date: Fri, 17 Jan 2025 11:06:04 -0500 Subject: [PATCH 2/5] address pr review --- .../src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e0d6f07d385..e636dd92444 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 @@ -211,7 +211,7 @@ public inline fun > AwsProfile.getEnumOrNull(key: String, su enumValues().firstOrNull { it.name.equals(value, ignoreCase = true) } ?: throw ConfigurationException( - "Value '$value' is not supported, should be one of: ${ + "$key $value is not supported, should be one of: ${ enumValues().joinToString(", ") { it.name.lowercase() } }", ) From b969f8b9565056602ce312eb0bcdd01d176af7a9 Mon Sep 17 00:00:00 2001 From: Xinsong Cui Date: Fri, 17 Jan 2025 11:11:31 -0500 Subject: [PATCH 3/5] add quote --- .../src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e636dd92444..82b73f025cc 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 @@ -211,7 +211,7 @@ public inline fun > AwsProfile.getEnumOrNull(key: String, su enumValues().firstOrNull { it.name.equals(value, ignoreCase = true) } ?: throw ConfigurationException( - "$key $value is not supported, should be one of: ${ + "$key '$value' is not supported, should be one of: ${ enumValues().joinToString(", ") { it.name.lowercase() } }", ) From 3065a8e4876faeb7d1cbf49c5cbbb758c15fe3bc Mon Sep 17 00:00:00 2001 From: Xinsong Cui Date: Fri, 17 Jan 2025 17:24:14 -0500 Subject: [PATCH 4/5] style --- .../sdk/kotlin/runtime/config/profile/AwsProfile.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 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 82b73f025cc..a46a8bde12d 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 @@ -211,12 +211,17 @@ public inline fun > AwsProfile.getEnumOrNull(key: String, su enumValues().firstOrNull { it.name.equals(value, ignoreCase = true) } ?: throw ConfigurationException( - "$key '$value' is not supported, should be one of: ${ - enumValues().joinToString(", ") { it.name.lowercase() } - }", + 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 { From 5ef76ef7060bba78f6bca56934e9f54f82d8cc19 Mon Sep 17 00:00:00 2001 From: Xinsong Cui Date: Fri, 17 Jan 2025 17:27:50 -0500 Subject: [PATCH 5/5] lint --- .../src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt | 3 +-- 1 file changed, 1 insertion(+), 2 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 a46a8bde12d..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 @@ -217,11 +217,10 @@ public inline fun > AwsProfile.getEnumOrNull(key: String, su 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 {