-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathOperationShape.java
More file actions
335 lines (298 loc) · 10.6 KB
/
OperationShape.java
File metadata and controls
335 lines (298 loc) · 10.6 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.model.shapes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import software.amazon.smithy.model.SourceException;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.traits.MixinTrait;
import software.amazon.smithy.model.traits.UnitTypeTrait;
import software.amazon.smithy.utils.BuilderRef;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.ToSmithyBuilder;
/**
* Represents an API operation.
*/
public final class OperationShape extends Shape implements ToSmithyBuilder<OperationShape> {
private final ShapeId input;
private final ShapeId output;
private final Set<ShapeId> errors;
private final Set<ShapeId> introducedErrors;
private OperationShape(Builder builder) {
super(builder, false);
input = Objects.requireNonNull(builder.input);
output = Objects.requireNonNull(builder.output);
if (getMixins().isEmpty()) {
errors = builder.errors.copy();
introducedErrors = errors;
} else {
// Compute mixin properties of the operation. Input / output are
// forbidden in operation mixins, so we don't bother with them
// here.
Set<ShapeId> computedErrors = new LinkedHashSet<>();
for (Shape shape : builder.getMixins().values()) {
shape.asOperationShape().ifPresent(mixin -> computedErrors.addAll(mixin.getErrorsSet()));
}
introducedErrors = builder.errors.copy();
computedErrors.addAll(introducedErrors);
errors = Collections.unmodifiableSet(new LinkedHashSet<>(computedErrors));
}
if (hasTrait(MixinTrait.ID) && (!input.equals(UnitTypeTrait.UNIT) || !output.equals(UnitTypeTrait.UNIT))) {
throw new SourceException(String.format(
"Operation shapes with the mixin trait MUST target `%s` for their input and output. Operation "
+ "mixin shape `%s` defines one or both of these properties.",
UnitTypeTrait.UNIT,
getId()), builder.getSourceLocation());
}
}
public static Builder builder() {
return new Builder();
}
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
public <R> R accept(ShapeVisitor<R> visitor) {
return visitor.operationShape(this);
}
@Override
public Optional<OperationShape> asOperationShape() {
return Optional.of(this);
}
@Override
public ShapeType getType() {
return ShapeType.OPERATION;
}
/**
* <p>Gets the optional shape ID of the input of the operation.</p>
*
* <p>For backward compatibility, if the input targets {@code smithy.api#Unit},
* then an empty optional is returned.
*
* @return Returns the optional shape ID.
*/
public Optional<ShapeId> getInput() {
return input.equals(UnitTypeTrait.UNIT) ? Optional.empty() : Optional.of(input);
}
/**
* <p>Gets the optional shape ID of the output of the operation.</p>
*
* <p>For backward compatibility, if the output targets {@code smithy.api#Unit},
* then an empty optional is returned.
*
* @return Returns the optional shape ID.
*/
public Optional<ShapeId> getOutput() {
return output.equals(UnitTypeTrait.UNIT) ? Optional.empty() : Optional.of(output);
}
/**
* Gets the input of the operation.
*
* <p>All operations have input, and they default to target
* {@code smithy.api#Unit}.
*
* @return Returns the non-nullable input.
*/
public ShapeId getInputShape() {
return input;
}
/**
* Gets the output of the operation.
*
* <p>All operations have output, and they default to target
* {@code smithy.api#Unit}.
*
* @return Returns the non-nullable output.
*/
public ShapeId getOutputShape() {
return output;
}
@Deprecated
public List<ShapeId> getErrors() {
return ListUtils.copyOf(errors);
}
/**
* <p>Gets a set of the error shape IDs bound directly to the operation
* that can be encountered.
*
* <p>This DOES NOT include errors that are common to a service. Operations
* can be bound to multiple services, so common service errors cannot be
* returned by this method. Use {@link #getErrors(ServiceShape)} or
* {@link OperationIndex#getErrors(ToShapeId, ToShapeId)} to get all of the
* errors an operation can encounter when used within a service.</p>
*
* <p>Each returned {@link ShapeId} must resolve to a
* {@link StructureShape} that is targeted by an error trait; however,
* this is only guaranteed after a model is validated.</p>
*
* @return Returns the errors.
* @see #getErrors(ServiceShape)
* @see OperationIndex#getErrors(ToShapeId, ToShapeId)
*/
public Set<ShapeId> getErrorsSet() {
return errors;
}
@Deprecated
public List<ShapeId> getIntroducedErrors() {
return ListUtils.copyOf(introducedErrors);
}
/**
* Gets the errors introduced by the shape and not inherited
* from mixins.
*
* @return Returns the introduced errors.
*/
public Set<ShapeId> getIntroducedErrorsSet() {
return introducedErrors;
}
/**
* <p>Gets a list of the error shape IDs the operation can encounter,
* including any common errors of a service.
*
* <p>No validation is performed here to ensure that the operation is
* actually bound to the given service shape.
*
* @return Returns the errors.
* @see OperationIndex#getErrors(ToShapeId, ToShapeId)
*/
public List<ShapeId> getErrors(ServiceShape service) {
Set<ShapeId> result = new LinkedHashSet<>(service.getErrorsSet());
result.addAll(getErrorsSet());
return new ArrayList<>(result);
}
@Override
public boolean equals(Object other) {
if (!super.equals(other)) {
return false;
} else {
OperationShape otherShape = (OperationShape) other;
return input.equals(otherShape.input)
&& output.equals(otherShape.output)
&& errors.equals(otherShape.errors);
}
}
/**
* Builder used to create a {@link OperationShape}.
*/
public static final class Builder extends AbstractShapeBuilder<Builder, OperationShape> {
private ShapeId input = UnitTypeTrait.UNIT;
private ShapeId output = UnitTypeTrait.UNIT;
private final BuilderRef<Set<ShapeId>> errors = BuilderRef.forOrderedSet();
public Builder() {}
private Builder(OperationShape shape) {
shape.updateBuilder(this);
this.input = shape.input;
this.output = shape.output;
this.errors.setBorrowed(shape.introducedErrors);
}
@Override
public ShapeType getShapeType() {
return ShapeType.OPERATION;
}
/**
* Sets the input shape ID of the operation.
*
* @param inputShape Shape ID that MUST reference a structure.
* @return Returns the builder.
*/
public Builder input(ToShapeId inputShape) {
input = inputShape == null ? UnitTypeTrait.UNIT : inputShape.toShapeId();
return this;
}
/**
* Sets the output shape ID of the operation.
*
* @param outputShape Shape ID that MUST reference a structure.
* @return Returns the builder.
*/
public Builder output(ToShapeId outputShape) {
output = outputShape == null ? UnitTypeTrait.UNIT : outputShape.toShapeId();
return this;
}
/**
* Sets and replaces the errors of the operation.
*
* @param errorShapeIds Error shape IDs to set.
* @return Returns the builder.
*/
public Builder errors(Collection<ShapeId> errorShapeIds) {
errors.clear();
errorShapeIds.forEach(this::addError);
return this;
}
/**
* Adds an error to the operation.
*
* @param errorShapeId Error shape ID to add.
* @return Returns the builder.
*/
public Builder addError(ToShapeId errorShapeId) {
errors.get().add(errorShapeId.toShapeId());
return this;
}
/**
* Adds an error to the operation.
*
* @param errorShapeId Error shape ID to add.
* @return Returns the builder.
* @throws ShapeIdSyntaxException if the shape ID is invalid.
*/
public Builder addError(String errorShapeId) {
return addError(ShapeId.from(errorShapeId));
}
/**
* Adds an each of the errors to the operation.
*
* @param errorShapeIds Error shape IDs to add.
* @return Returns the builder.
*/
public Builder addErrors(Collection<ShapeId> errorShapeIds) {
errors.get().addAll(Objects.requireNonNull(errorShapeIds));
return this;
}
/**
* Removes an error by Shape ID.
*
* @param errorShapeId Error shape ID to remove.
* @return Returns the builder.
*/
public Builder removeError(ToShapeId errorShapeId) {
errors.get().remove(errorShapeId.toShapeId());
return this;
}
/**
* Removes all errors.
* @return Returns the builder.
*/
public Builder clearErrors() {
errors.clear();
return this;
}
@Override
public OperationShape build() {
return new OperationShape(this);
}
@Override
public Builder flattenMixins() {
if (getMixins().isEmpty()) {
return this;
}
Set<ShapeId> computedErrors = new LinkedHashSet<>();
for (Shape shape : getMixins().values()) {
shape.asOperationShape().ifPresent(mixin -> computedErrors.addAll(mixin.getErrorsSet()));
}
computedErrors.addAll(errors.peek());
errors(computedErrors);
return super.flattenMixins();
}
}
}