Skip to content

Commit

Permalink
misc: awsprofile enum parser (#1507)
Browse files Browse the repository at this point in the history
* add getEnumOrNull for AwsProfile

* address pr review

* add quote

* style

* lint
  • Loading branch information
xinsong-cui authored Jan 30, 2025
1 parent 24b36f6 commit 2e3860f
Showing 1 changed file with 23 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RetryMode>("retry_mode")

/**
* Whether service clients should make requests to the FIPS endpoint variant.
Expand Down Expand Up @@ -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<AccountIdEndpointMode>("account_id_endpoint_mode")

/**
* Determines when a request should be compressed or not
Expand Down Expand Up @@ -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<RequestHttpChecksumConfig>("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<ResponseHttpChecksumConfig>("response_checksum_validation")

/**
* Parse a config value as a boolean, ignoring case.
Expand Down Expand Up @@ -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 <reified T : Enum<T>> AwsProfile.getEnumOrNull(key: String, subKey: String? = null): T? =
getOrNull(key, subKey)?.let { value ->
enumValues<T>().firstOrNull {
it.name.equals(value, ignoreCase = true)
} ?: throw ConfigurationException(
buildString {
append(key)
append(" '")
append(value)
append("' is not supported, should be one of: ")
enumValues<T>().joinTo(this) { it.name.lowercase() }
},
)
}

internal fun AwsProfile.getUrlOrNull(key: String, subKey: String? = null): Url? =
getOrNull(key, subKey)?.let {
try {
Expand Down

0 comments on commit 2e3860f

Please sign in to comment.