Skip to content

Commit 2e3860f

Browse files
authored
misc: awsprofile enum parser (#1507)
* add getEnumOrNull for AwsProfile * address pr review * add quote * style * lint
1 parent 24b36f6 commit 2e3860f

File tree

1 file changed

+23
-34
lines changed
  • aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/profile

1 file changed

+23
-34
lines changed

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/profile/AwsProfile.kt

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,7 @@ public val AwsProfile.credentialProcess: String?
8383
*/
8484
@InternalSdkApi
8585
public val AwsProfile.retryMode: RetryMode?
86-
get() = getOrNull("retry_mode")?.run {
87-
RetryMode.values().firstOrNull { it.name.equals(this, ignoreCase = true) }
88-
?: throw ConfigurationException(
89-
"retry_mode $this is not supported, should be one of: ${
90-
RetryMode.values().joinToString(", ") { it.name.lowercase() }
91-
}",
92-
)
93-
}
86+
get() = getEnumOrNull<RetryMode>("retry_mode")
9487

9588
/**
9689
* Whether service clients should make requests to the FIPS endpoint variant.
@@ -139,14 +132,7 @@ public val AwsProfile.sdkUserAgentAppId: String?
139132
*/
140133
@InternalSdkApi
141134
public val AwsProfile.accountIdEndpointMode: AccountIdEndpointMode?
142-
get() = getOrNull("account_id_endpoint_mode")?.run {
143-
AccountIdEndpointMode.values().firstOrNull { it.name.equals(this, ignoreCase = true) }
144-
?: throw ConfigurationException(
145-
"account_id_endpoint_mode $this is not supported, should be one of: ${
146-
AccountIdEndpointMode.values().joinToString(", ") { it.name.lowercase() }
147-
}",
148-
)
149-
}
135+
get() = getEnumOrNull<AccountIdEndpointMode>("account_id_endpoint_mode")
150136

151137
/**
152138
* Determines when a request should be compressed or not
@@ -174,30 +160,14 @@ public val AwsProfile.sigV4aSigningRegionSet: String?
174160
*/
175161
@InternalSdkApi
176162
public val AwsProfile.requestChecksumCalculation: RequestHttpChecksumConfig?
177-
get() = getOrNull("request_checksum_calculation")?.run {
178-
RequestHttpChecksumConfig
179-
.values()
180-
.firstOrNull { it.name.equals(this, ignoreCase = true) }
181-
?: throw ConfigurationException(
182-
"request_checksum_calculation $this is not supported, should be one of: " +
183-
RequestHttpChecksumConfig.values().joinToString(", ") { it.name.lowercase() },
184-
)
185-
}
163+
get() = getEnumOrNull<RequestHttpChecksumConfig>("request_checksum_calculation")
186164

187165
/**
188166
* Configures response checksum validation
189167
*/
190168
@InternalSdkApi
191169
public val AwsProfile.responseChecksumValidation: ResponseHttpChecksumConfig?
192-
get() = getOrNull("response_checksum_validation")?.run {
193-
ResponseHttpChecksumConfig
194-
.values()
195-
.firstOrNull { it.name.equals(this, ignoreCase = true) }
196-
?: throw ConfigurationException(
197-
"response_checksum_validation $this is not supported, should be one of: " +
198-
ResponseHttpChecksumConfig.values().joinToString(", ") { it.name.lowercase() },
199-
)
200-
}
170+
get() = getEnumOrNull<ResponseHttpChecksumConfig>("response_checksum_validation")
201171

202172
/**
203173
* Parse a config value as a boolean, ignoring case.
@@ -232,6 +202,25 @@ public fun AwsProfile.getLongOrNull(key: String, subKey: String? = null): Long?
232202
)
233203
}
234204

205+
/**
206+
* Parse a config value as an enum.
207+
*/
208+
@InternalSdkApi
209+
public inline fun <reified T : Enum<T>> AwsProfile.getEnumOrNull(key: String, subKey: String? = null): T? =
210+
getOrNull(key, subKey)?.let { value ->
211+
enumValues<T>().firstOrNull {
212+
it.name.equals(value, ignoreCase = true)
213+
} ?: throw ConfigurationException(
214+
buildString {
215+
append(key)
216+
append(" '")
217+
append(value)
218+
append("' is not supported, should be one of: ")
219+
enumValues<T>().joinTo(this) { it.name.lowercase() }
220+
},
221+
)
222+
}
223+
235224
internal fun AwsProfile.getUrlOrNull(key: String, subKey: String? = null): Url? =
236225
getOrNull(key, subKey)?.let {
237226
try {

0 commit comments

Comments
 (0)