/
EndpointRulesSpecUtils.java
185 lines (156 loc) · 7.08 KB
/
EndpointRulesSpecUtils.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.codegen.poet.rules;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.jr.stree.JrsBoolean;
import com.fasterxml.jackson.jr.stree.JrsString;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import software.amazon.awssdk.codegen.internal.Utils;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.Metadata;
import software.amazon.awssdk.codegen.model.rules.endpoints.BuiltInParameter;
import software.amazon.awssdk.codegen.model.rules.endpoints.ParameterModel;
import software.amazon.awssdk.endpoints.Endpoint;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.utils.Validate;
import software.amazon.awssdk.utils.internal.CodegenNamingUtils;
public class EndpointRulesSpecUtils {
private final IntermediateModel intermediateModel;
public EndpointRulesSpecUtils(IntermediateModel intermediateModel) {
this.intermediateModel = intermediateModel;
}
public String basePackage() {
return intermediateModel.getMetadata().getFullEndpointRulesPackageName();
}
public ClassName rulesRuntimeClassName(String name) {
return ClassName.get(intermediateModel.getMetadata().getFullInternalEndpointRulesPackageName(),
name);
}
public ClassName parametersClassName() {
return ClassName.get(basePackage(), intermediateModel.getMetadata().getServiceName() + "EndpointParams");
}
public ClassName providerInterfaceName() {
return ClassName.get(basePackage(), intermediateModel.getMetadata().getServiceName() + "EndpointProvider");
}
public ClassName providerDefaultImplName() {
Metadata md = intermediateModel.getMetadata();
return ClassName.get(md.getFullInternalEndpointRulesPackageName(),
"Default" + providerInterfaceName().simpleName());
}
public ClassName resolverInterceptorName() {
Metadata md = intermediateModel.getMetadata();
return ClassName.get(md.getFullInternalEndpointRulesPackageName(),
md.getServiceName() + "ResolveEndpointInterceptor");
}
public ClassName authSchemesInterceptorName() {
Metadata md = intermediateModel.getMetadata();
return ClassName.get(md.getFullInternalEndpointRulesPackageName(),
md.getServiceName() + "EndpointAuthSchemeInterceptor");
}
public ClassName requestModifierInterceptorName() {
Metadata md = intermediateModel.getMetadata();
return ClassName.get(md.getFullInternalEndpointRulesPackageName(),
md.getServiceName() + "RequestSetEndpointInterceptor");
}
public ClassName clientEndpointTestsName() {
Metadata md = intermediateModel.getMetadata();
return ClassName.get(md.getFullEndpointRulesPackageName(),
md.getServiceName() + "ClientEndpointTests");
}
public ClassName endpointProviderTestsName() {
Metadata md = intermediateModel.getMetadata();
return ClassName.get(md.getFullEndpointRulesPackageName(),
md.getServiceName() + "EndpointProviderTests");
}
public ClassName clientContextParamsName() {
Metadata md = intermediateModel.getMetadata();
return ClassName.get(md.getFullEndpointRulesPackageName(),
md.getServiceName() + "ClientContextParams");
}
public String paramMethodName(String param) {
return Utils.unCapitalize(CodegenNamingUtils.pascalCase(param));
}
public String clientContextParamMethodName(String param) {
return Utils.unCapitalize(CodegenNamingUtils.pascalCase(param));
}
public String clientContextParamName(String paramName) {
return intermediateModel.getNamingStrategy().getEnumValueName(paramName);
}
public TypeName toJavaType(String type) {
switch (type.toLowerCase(Locale.ENGLISH)) {
case "boolean":
return TypeName.get(Boolean.class);
case "string":
return TypeName.get(String.class);
default:
throw new RuntimeException("Unknown type: " + type);
}
}
public CodeBlock valueCreationCode(String type, CodeBlock param) {
String methodName;
switch (type.toLowerCase(Locale.ENGLISH)) {
case "boolean":
methodName = "fromBool";
break;
case "string":
methodName = "fromStr";
break;
default:
throw new RuntimeException("Don't know how to create a Value instance from type " + type);
}
return CodeBlock.builder()
.add("$T.$N($L)", rulesRuntimeClassName("Value"), methodName, param)
.build();
}
public TypeName parameterType(ParameterModel param) {
if (param.getBuiltInEnum() == null || param.getBuiltInEnum() != BuiltInParameter.AWS_REGION) {
return toJavaType(param.getType());
}
if (param.getBuiltInEnum() == BuiltInParameter.AWS_REGION) {
return ClassName.get(Region.class);
}
return toJavaType(param.getType());
}
public CodeBlock treeNodeToLiteral(TreeNode treeNode) {
CodeBlock.Builder b = CodeBlock.builder();
switch (treeNode.asToken()) {
case VALUE_STRING:
b.add("$S", Validate.isInstanceOf(JrsString.class, treeNode, "Expected string").getValue());
break;
case VALUE_TRUE:
case VALUE_FALSE:
b.add("$L", Validate.isInstanceOf(JrsBoolean.class, treeNode, "Expected boolean").booleanValue());
break;
default:
throw new RuntimeException("Don't know how to set default value for parameter of type "
+ treeNode.asToken());
}
return b.build();
}
public boolean isS3() {
return "S3".equals(intermediateModel.getMetadata().getServiceName());
}
public boolean isS3Control() {
return "S3Control".equals(intermediateModel.getMetadata().getServiceName());
}
public TypeName resolverReturnType() {
return ParameterizedTypeName.get(CompletableFuture.class, Endpoint.class);
}
}