-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathSchemaGuidedDocumentBuilder.java
More file actions
173 lines (153 loc) · 7.63 KB
/
Copy pathSchemaGuidedDocumentBuilder.java
File metadata and controls
173 lines (153 loc) · 7.63 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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.java.dynamicschemas;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import software.amazon.smithy.java.core.schema.Schema;
import software.amazon.smithy.java.core.schema.SchemaUtils;
import software.amazon.smithy.java.core.schema.ShapeBuilder;
import software.amazon.smithy.java.core.schema.TraitKey;
import software.amazon.smithy.java.core.serde.ShapeDeserializer;
import software.amazon.smithy.java.core.serde.document.Document;
import software.amazon.smithy.java.core.serde.event.EventStream;
import software.amazon.smithy.java.io.datastream.DataStream;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.ShapeType;
/**
* Allows a StructDocument to be used in a ShapeBuilder.
*/
final class SchemaGuidedDocumentBuilder implements ShapeBuilder<StructDocument> {
private final ShapeId service;
private final Schema target;
private final Map<String, Document> map = new LinkedHashMap<>();
SchemaGuidedDocumentBuilder(Schema target, ShapeId service) {
if (target.type() != ShapeType.STRUCTURE && target.type() != ShapeType.UNION) {
throw new IllegalArgumentException("StructDocument can only deserialize a structure or union, "
+ "but got " + target);
}
this.target = target;
this.service = service;
}
@Override
public Schema schema() {
return target;
}
@Override
public StructDocument build() {
if (map.isEmpty() && target.type() == ShapeType.UNION) {
throw new IllegalArgumentException("No value set for union document: " + schema().id());
} else {
// Use "new" here since the document is already properly wrapped throughout.
return new StructDocument(target, map, service);
}
}
@Override
public void setMemberValue(Schema member, Object value) {
SchemaUtils.validateMemberInSchema(target, member, value);
Document convertedValue = switch (value) {
// Convert the given document so it matches the required schema.
case Document d -> StructDocument.convertDocument(member, d, service);
case DataStream ds -> Document.of(member, ds);
case EventStream<?> es -> Document.of(member, es);
// Convert the object to a document and then wrap it with the correct schema.
case null, default -> StructDocument.convertDocument(member, Document.ofObject(value), service);
};
map.put(member.memberName(), convertedValue);
}
@Override
public ShapeBuilder<StructDocument> deserialize(ShapeDeserializer decoder) {
map.putAll(deserialize(decoder, target).asStringMap());
return this;
}
@Override
public ShapeBuilder<StructDocument> deserializeMember(ShapeDeserializer decoder, Schema schema) {
map.putAll(deserialize(decoder, schema.assertMemberTargetIs(target)).asStringMap());
return this;
}
private Document deserialize(ShapeDeserializer decoder, Schema schema) {
return switch (schema.type()) {
case BLOB -> {
if (schema.hasTrait(TraitKey.STREAMING_TRAIT)) {
yield Document.of(schema, decoder.readDataStream(schema));
}
yield new ContentDocument(Document.of(decoder.readBlob(schema)), schema);
}
case BOOLEAN -> new ContentDocument(Document.of(decoder.readBoolean(schema)), schema);
case STRING, ENUM -> new ContentDocument(Document.of(decoder.readString(schema)), schema);
case TIMESTAMP -> new ContentDocument(Document.of(decoder.readTimestamp(schema)), schema);
case BYTE -> new ContentDocument(Document.ofNumber(decoder.readByte(schema)), schema);
case SHORT -> new ContentDocument(Document.ofNumber(decoder.readShort(schema)), schema);
case INTEGER, INT_ENUM -> new ContentDocument(Document.ofNumber(decoder.readInteger(schema)), schema);
case LONG -> new ContentDocument(Document.ofNumber(decoder.readLong(schema)), schema);
case FLOAT -> new ContentDocument(Document.ofNumber(decoder.readFloat(schema)), schema);
case DOCUMENT -> new ContentDocument(decoder.readDocument(), schema);
case DOUBLE -> new ContentDocument(Document.ofNumber(decoder.readDouble(schema)), schema);
case BIG_DECIMAL -> new ContentDocument(Document.ofNumber(decoder.readBigDecimal(schema)), schema);
case BIG_INTEGER -> new ContentDocument(Document.ofNumber(decoder.readBigInteger(schema)), schema);
case LIST -> {
var sparse = schema.hasTrait(TraitKey.SPARSE_TRAIT);
var items = new SchemaList(schema.listMember());
decoder.readList(schema, items, (it, memberDeserializer) -> {
// A null element (only valid in @sparse lists) is retained as null; otherwise read the value.
if (sparse && memberDeserializer.isNull()) {
it.add(memberDeserializer.readNull());
} else {
it.add(deserialize(memberDeserializer, it.schema));
}
});
yield new ContentDocument(Document.of(items), schema);
}
case MAP -> {
var sparse = schema.hasTrait(TraitKey.SPARSE_TRAIT);
var map = new SchemaMap(schema);
decoder.readStringMap(schema, map, (state, mapKey, memberDeserializer) -> {
// A null value (only valid in @sparse maps) is retained as null; otherwise read the value.
if (sparse && memberDeserializer.isNull()) {
state.put(mapKey, memberDeserializer.readNull());
} else {
state.put(mapKey, deserialize(memberDeserializer, state.schema.mapValueMember()));
}
});
yield new ContentDocument(Document.of(map), schema);
}
case STRUCTURE -> createStructDocument(decoder, schema);
case UNION -> {
if (schema.hasTrait(TraitKey.STREAMING_TRAIT)) {
yield Document.of(schema, decoder.readEventStream(schema));
}
yield createStructDocument(decoder, schema);
}
default -> throw new UnsupportedOperationException("Unsupported target type: " + schema.type());
};
}
private StructDocument createStructDocument(ShapeDeserializer decoder, Schema schema) {
var map = new LinkedHashMap<String, Document>();
decoder.readStruct(schema, map, (state, memberSchema, memberDeserializer) -> {
state.put(memberSchema.memberName(), deserialize(memberDeserializer, memberSchema));
});
return new StructDocument(schema, map, service);
}
@Override
public ShapeBuilder<StructDocument> errorCorrection() {
// TODO: fill in defaults.
return this;
}
// Captures the schema of a list to pass to a closure.
private static final class SchemaList extends ArrayList<Document> {
private final Schema schema;
SchemaList(Schema schema) {
this.schema = schema;
}
}
// Captures the schema of a map to pass to a closure.
private static final class SchemaMap extends HashMap<String, Document> {
private final Schema schema;
SchemaMap(Schema schema) {
this.schema = schema;
}
}
}