Skip to content
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

misc: awsprofile enum parser #1507

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,21 @@ 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(
"Value '$value' is not supported, should be one of: ${
xinsong-cui marked this conversation as resolved.
Show resolved Hide resolved
enumValues<T>().joinToString(", ") { it.name.lowercase() }
}",
)
}

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