forked from aws/aws-sdk-go-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlacierCustomizations.java
116 lines (106 loc) · 5.5 KB
/
GlacierCustomizations.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package software.amazon.smithy.aws.go.codegen.customization;
import java.util.List;
import java.util.stream.Collectors;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoDelegator;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar;
import software.amazon.smithy.go.codegen.integration.ProtocolUtils;
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.utils.ListUtils;
public class GlacierCustomizations implements GoIntegration {
private static final String TREE_HASH_ADDER = "AddTreeHashMiddleware";
private static final String API_VERSION_ADDER = "AddGlacierAPIVersionMiddleware";
private static final String ACCOUNT_ID_ADDER = "AddDefaultAccountIDMiddleware";
private static final String SET_DEFAULT_ACCOUNT_ID = "setDefaultAccountID";
@Override
public byte getOrder() {
return 127;
}
@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return ListUtils.of(
RuntimeClientPlugin.builder()
.servicePredicate(GlacierCustomizations::isGlacier)
.registerMiddleware(MiddlewareRegistrar.builder()
.resolvedFunction(SymbolUtils.createValueSymbolBuilder(TREE_HASH_ADDER,
AwsCustomGoDependency.GLACIER_CUSTOMIZATION).build())
.build())
.build(),
RuntimeClientPlugin.builder()
.servicePredicate(GlacierCustomizations::isGlacier)
.registerMiddleware(MiddlewareRegistrar.builder()
.resolvedFunction(SymbolUtils.createValueSymbolBuilder(API_VERSION_ADDER,
AwsCustomGoDependency.GLACIER_CUSTOMIZATION).build())
.functionArguments(ListUtils.of(
SymbolUtils.createValueSymbolBuilder("ServiceAPIVersion").build()))
.build())
.build(),
RuntimeClientPlugin.builder()
.servicePredicate(GlacierCustomizations::isGlacier)
.registerMiddleware(MiddlewareRegistrar.builder()
.resolvedFunction(SymbolUtils.createValueSymbolBuilder(ACCOUNT_ID_ADDER,
AwsCustomGoDependency.GLACIER_CUSTOMIZATION).build())
.functionArguments(ListUtils.of(
SymbolUtils.createValueSymbolBuilder(SET_DEFAULT_ACCOUNT_ID).build()))
.build())
.build()
);
}
@Override
public void writeAdditionalFiles(
GoSettings settings,
Model model,
SymbolProvider symbolProvider,
GoDelegator goDelegator
) {
ServiceShape service = settings.getService(model);
if (!isGlacier(model, service)) {
return;
}
goDelegator.useShapeWriter(service, writer -> {
writeAccountIdSetter(writer, model, symbolProvider, service);
});
}
private void writeAccountIdSetter(
GoWriter writer,
Model model,
SymbolProvider symbolProvider,
ServiceShape service
) {
writer.writeDocs("setDefaultAccountID sets the AccountID to the given value if the current value is nil");
writer.openBlock("func setDefaultAccountID(input interface{}, accountID string) interface{} {", "}", () -> {
writer.openBlock("switch i := input.(type) {", "}", () -> {
for (OperationShape operation : TopDownIndex.of(model).getContainedOperations(service)) {
StructureShape input = ProtocolUtils.expectInput(model, operation);
List<MemberShape> accountId = input.getAllMembers().values().stream()
.filter(m -> m.getMemberName().toLowerCase().equals("accountid"))
.toList();
if (accountId.isEmpty()) {
continue;
}
writer.openBlock("case $P:", "", symbolProvider.toSymbol(input), () -> {
String memberName = symbolProvider.toMemberName(accountId.get(0));
writer.write("if i.$L == nil { i.$L = &accountID }", memberName, memberName);
writer.write("return i");
});
}
writer.write("default: return input");
});
});
}
private static boolean isGlacier(Model model, ServiceShape service) {
return service.expectTrait(ServiceTrait.class).getSdkId().equalsIgnoreCase("Glacier");
}
}