forked from aws/aws-sdk-go-v2
/
Ec2QueryShapeSerVisitor.java
62 lines (56 loc) · 2.72 KB
/
Ec2QueryShapeSerVisitor.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
package software.amazon.smithy.aws.go.codegen;
import java.util.Optional;
import java.util.function.Predicate;
import software.amazon.smithy.aws.traits.protocols.Ec2QueryNameTrait;
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator.GenerationContext;
import software.amazon.smithy.go.codegen.trait.NoSerializeTrait;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
import software.amazon.smithy.model.traits.XmlNameTrait;
import software.amazon.smithy.utils.FunctionalUtils;
import software.amazon.smithy.utils.StringUtils;
/**
* Visitor to generate serialization functions for shapes in EC2 Query protocol
* document bodies.
*
* This class uses the implementations provided by {@code QueryShapeSerVisitor} but with
* the following protocol specific customizations for ec2 query:
*
* <ul>
* <li>ec2 query flattens all lists, sets, and maps regardless of the {@code @xmlFlattened} trait.</li>
* <li>ec2 query respects the {@code @ec2QueryName} trait, then the {@code xmlName}
* trait value with the first letter capitalized.</li>
* </ul>
*
* Timestamps are serialized to {@link Format}.DATE_TIME by default.
*
* @see QueryShapeSerVisitor
*/
final class Ec2QueryShapeSerVisitor extends QueryShapeSerVisitor {
public Ec2QueryShapeSerVisitor(GenerationContext context) {
this(context, NoSerializeTrait.excludeNoSerializeMembers().and(FunctionalUtils.alwaysTrue()));
}
public Ec2QueryShapeSerVisitor(GenerationContext context, Predicate<MemberShape> memberFilter) {
super(context, memberFilter, true);
}
@Override
protected String getSerializedLocationName(MemberShape memberShape, String defaultValue) {
// The serialization for aws.ec2 prioritizes the @ec2QueryName trait for serialization.
Optional<Ec2QueryNameTrait> trait = memberShape.getTrait(Ec2QueryNameTrait.class);
if (trait.isPresent()) {
return trait.get().getValue();
}
// Fall back to the capitalized @xmlName trait if present on the member,
// otherwise use the capitalized default value.
return StringUtils.capitalize(memberShape.getTrait(XmlNameTrait.class)
.map(XmlNameTrait::getValue)
.orElse(defaultValue));
}
@Override
protected boolean isFlattened(GenerationContext context, MemberShape memberShape) {
// All lists, sets, and maps are flattened in aws.ec2.
ShapeType targetType = context.getModel().expectShape(memberShape.getTarget()).getType();
return targetType == ShapeType.LIST || targetType == ShapeType.SET || targetType == ShapeType.MAP;
}
}