-
Notifications
You must be signed in to change notification settings - Fork 69
/
StructureGenerator.java
239 lines (222 loc) · 8.85 KB
/
StructureGenerator.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright 2019 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.smithy.typescript.codegen;
import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobStreamingMembers;
import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeInlineStreamingMemberType;
import java.util.List;
import java.util.stream.Collectors;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.codegen.core.SymbolReference;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.typescript.codegen.integration.HttpProtocolGeneratorUtils;
import software.amazon.smithy.utils.SmithyInternalApi;
/**
* Generates normal structures and error structures.
*/
@SmithyInternalApi
final class StructureGenerator implements Runnable {
private final Model model;
private final SymbolProvider symbolProvider;
private final TypeScriptWriter writer;
private final StructureShape shape;
private final boolean includeValidation;
/**
* sets 'includeValidation' to 'false' for backwards compatibility.
*/
StructureGenerator(Model model, SymbolProvider symbolProvider, TypeScriptWriter writer, StructureShape shape) {
this(model, symbolProvider, writer, shape, false);
}
StructureGenerator(Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer,
StructureShape shape,
boolean includeValidation) {
this.model = model;
this.symbolProvider = symbolProvider;
this.writer = writer;
this.shape = shape;
this.includeValidation = includeValidation;
}
@Override
public void run() {
if (shape.hasTrait(ErrorTrait.class)) {
renderErrorStructure();
} else {
renderNonErrorStructure();
}
}
/**
* Renders a normal, non-error structure.
*
* <p>For example, given the following Smithy model:
*
* <pre>{@code
* namespace smithy.example
*
* structure Person {
* @required
* name: String,
* @range(min: 1)
* age: Integer,
* }
* }</pre>
*
* <p>The following TypeScript is rendered:
*
* <pre>{@code
* export interface Person {
* name: string | undefined;
* age?: number | null;
* }
*
* export namespace Person {
* export const filterSensitiveLog = (obj: Person): any => ({...obj});
* }
* }</pre>
*
* <p>If validation is enabled, it generates the following:
*
* <pre>{@code
* export interface Person {
* name: string | undefined;
* age?: number | null;
* }
*
* export namespace Person {
* export const filterSensitiveLog = (obj: Person): any => ({...obj});
* export const validate = (obj: Person): ValidationFailure[] => {
* // validation
* }
* }
* }</pre>
*/
private void renderNonErrorStructure() {
Symbol symbol = symbolProvider.toSymbol(shape);
writer.writeShapeDocs(shape);
// Find symbol references with the "extends" property.
String extendsFrom = symbol.getReferences().stream()
.filter(ref -> ref.getProperty(SymbolVisitor.IMPLEMENTS_INTERFACE_PROPERTY).isPresent())
.map(SymbolReference::getAlias)
.collect(Collectors.joining(", "));
if (extendsFrom.isEmpty()) {
writer.openBlock("export interface $L {", symbol.getName());
} else {
writer.openBlock("export interface $L extends $L {", symbol.getName(), extendsFrom);
}
StructuredMemberWriter config = new StructuredMemberWriter(
model, symbolProvider, shape.getAllMembers().values());
config.writeMembers(writer, shape);
writer.closeBlock("}");
writer.write("");
renderStructureNamespace(config, includeValidation);
}
private void renderStructureNamespace(StructuredMemberWriter structuredMemberWriter, boolean includeValidation) {
Symbol symbol = symbolProvider.toSymbol(shape);
writer.openBlock("export namespace $L {", "}", symbol.getName(), () -> {
String objectParam = "obj";
writer.writeDocs("@internal");
writer.openBlock("export const filterSensitiveLog = ($L: $L): any => ({", "})",
objectParam, symbol.getName(),
() -> {
structuredMemberWriter.writeFilterSensitiveLog(writer, objectParam);
}
);
if (!includeValidation) {
return;
}
structuredMemberWriter.writeMemberValidatorCache(writer, "memberValidators");
writer.addImport("ValidationFailure", "__ValidationFailure", "@aws-smithy/server-common");
writer.writeDocs("@internal");
List<MemberShape> blobStreamingMembers = getBlobStreamingMembers(model, shape);
writer.writeInline("export const validate = ($L: ", objectParam);
if (blobStreamingMembers.isEmpty()) {
writer.writeInline("$L", symbol.getName());
} else {
writeInlineStreamingMemberType(writer, symbol, blobStreamingMembers.get(0));
}
writer.openBlock(", path: string = \"\"): __ValidationFailure[] => {", "}", () -> {
structuredMemberWriter.writeMemberValidatorFactory(writer, "memberValidators");
structuredMemberWriter.writeValidateMethodContents(writer, objectParam);
});
});
}
/**
* Error structures generate classes that extend from ServiceException
* (SmithyException is case of server SDK), and add the appropriate fault
* property.
*
* <p>Given the following Smithy structure:
*
* <pre>{@code
* namespace smithy.example
*
* @error("client")
* structure NoSuchResource {
* @required
* resourceType: String
* }
* }</pre>
*
* <p>The following TypeScript is generated:
*
* <pre>{@code
* import { ExceptionOptionType as __ExceptionOptionType } from "@aws-sdk/smithy-client";
* import { ServiceException as __BaseException } from "@aws-sdk/smithy-client";
* // In server SDK:
* // import { SmithyException as __BaseException } from "@aws-smithy/server-common";
*
* export class NoSuchResource extends __BaseException {
* name: "NoSuchResource";
* $fault: "client";
* resourceType: string | undefined;
* // @internal
* constructor(opts: __ExceptionOptionType<NoSuchResource, __BaseException>) {
* super({
* name: "NoSuchResource",
* $fault: "client",
* ...opts
* });
* Object.setPrototypeOf(this, NoSuchResource.prototype);
* this.resourceType = opts.resourceType;
* }
* }
* }</pre>
*/
private void renderErrorStructure() {
ErrorTrait errorTrait = shape.getTrait(ErrorTrait.class).orElseThrow(IllegalStateException::new);
Symbol symbol = symbolProvider.toSymbol(shape);
writer.writeShapeDocs(shape);
boolean isServerSdk = this.includeValidation;
writer.openBlock("export class $L extends $L {", symbol.getName(), "__BaseException");
writer.write("readonly name: $1S = $1S;", shape.getId().getName());
writer.write("readonly $$fault: $1S = $1S;", errorTrait.getValue());
if (!isServerSdk) {
HttpProtocolGeneratorUtils.writeRetryableTrait(writer, shape, ";");
}
StructuredMemberWriter structuredMemberWriter = new StructuredMemberWriter(model, symbolProvider,
shape.getAllMembers().values());
// since any error interface must extend from JavaScript Error interface, message member is already
// required in the JavaScript Error interface
structuredMemberWriter.skipMembers.add("message");
structuredMemberWriter.writeMembers(writer, shape);
structuredMemberWriter.writeErrorConstructor(writer, shape, isServerSdk);
writer.closeBlock("}");
writer.write("");
}
}