forked from smithy-lang/smithy-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountIdAutofill.kt
45 lines (41 loc) · 1.65 KB
/
AccountIdAutofill.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rustsdk.customize.glacier
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationCustomization
import software.amazon.smithy.rust.codegen.core.smithy.customize.OperationSection
import software.amazon.smithy.rust.codegen.core.util.inputShape
class AccountIdAutofill : OperationCustomization() {
override fun mutSelf(): Boolean = true
override fun consumesSelf(): Boolean = false
override fun section(section: OperationSection): Writable {
return when (section) {
is OperationSection.MutateInput -> writable {
rust(
"""
if ${section.input}.account_id.as_deref().unwrap_or_default().is_empty() {
${section.input}.account_id = Some("-".to_owned());
}
""",
)
}
else -> emptySection
}
}
companion object {
fun forOperation(operation: OperationShape, model: Model): AccountIdAutofill? {
val input = operation.inputShape(model)
return if (input.memberNames.contains("accountId")) {
AccountIdAutofill()
} else {
null
}
}
}
}