Skip to content

Commit 53ab291

Browse files
authored
feat: support Document type serialization (#429)
* feat: support Document type serialization * style: use Document.ListBuilder * test: test dependency change scope to test * chore: remove extra comment
1 parent 939d46a commit 53ab291

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@
190190
<artifactId>s3</artifactId>
191191
<scope>test</scope>
192192
</dependency>
193+
<dependency>
194+
<groupId>software.amazon.awssdk</groupId>
195+
<artifactId>kendra</artifactId>
196+
<scope>test</scope>
197+
</dependency>
193198

194199
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
195200
<dependency>

src/main/java/software/amazon/cloudformation/proxy/aws/SdkPojoDeserializer.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import software.amazon.awssdk.core.SdkBytes;
3636
import software.amazon.awssdk.core.SdkField;
3737
import software.amazon.awssdk.core.SdkPojo;
38+
import software.amazon.awssdk.core.document.Document;
39+
import software.amazon.awssdk.core.document.Document.ListBuilder;
40+
import software.amazon.awssdk.core.document.Document.MapBuilder;
3841
import software.amazon.awssdk.core.protocol.MarshallingType;
3942
import software.amazon.awssdk.core.traits.ListTrait;
4043
import software.amazon.awssdk.core.traits.MapTrait;
@@ -151,8 +154,10 @@ private Object readObject(SdkField<?> field, JsonParser p, DeserializationContex
151154
return readMap(field, p, ctxt);
152155
} else if (type.equals(MarshallingType.SDK_POJO)) {
153156
return readPojo(field.constructor().get(), p, ctxt);
157+
} else if (type.equals(MarshallingType.DOCUMENT)) {
158+
return readDocument(field, p, ctxt);
154159
}
155-
throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got Map/SdkPojo");
160+
throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got Map/SdkPojo/Document");
156161
}
157162

158163
case START_ARRAY: {
@@ -204,6 +209,38 @@ private List<Object> readList(SdkField<?> field, JsonParser p, DeserializationCo
204209
return value;
205210
}
206211

212+
private Document readDocument(SdkField<?> field, JsonParser p, DeserializationContext ctxt) throws IOException {
213+
switch (p.currentToken()) {
214+
case VALUE_STRING:
215+
return Document.fromString(p.getText());
216+
case VALUE_TRUE:
217+
case VALUE_FALSE:
218+
return Document.fromBoolean(p.getBooleanValue());
219+
case VALUE_NUMBER_FLOAT:
220+
case VALUE_NUMBER_INT:
221+
return Document.fromNumber(p.getText());
222+
case START_ARRAY: {
223+
ListBuilder builder = Document.listBuilder();
224+
while (p.nextToken() != JsonToken.END_ARRAY) {
225+
builder.addDocument(readDocument(field, p, ctxt));
226+
}
227+
return builder.build();
228+
}
229+
case START_OBJECT:
230+
MapBuilder builder = Document.mapBuilder();
231+
while (p.nextToken() != JsonToken.END_OBJECT) {
232+
String fieldName = p.getCurrentName();
233+
p.nextToken();
234+
builder.putDocument(fieldName, readDocument(field, p, ctxt));
235+
}
236+
return builder.build();
237+
case VALUE_NULL:
238+
return Document.fromNull();
239+
default:
240+
throw new JsonMappingException(p, "Can not map type " + type + " as Document Token = " + p.currentToken());
241+
}
242+
}
243+
207244
@Override
208245
public boolean isCachable() {
209246
return true;

src/main/java/software/amazon/cloudformation/proxy/aws/SdkPojoSerializer.java

+36
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import java.math.BigDecimal;
2424
import java.time.Instant;
2525
import java.util.Collection;
26+
import java.util.List;
2627
import java.util.Map;
2728
import software.amazon.awssdk.core.SdkBytes;
2829
import software.amazon.awssdk.core.SdkField;
2930
import software.amazon.awssdk.core.SdkPojo;
31+
import software.amazon.awssdk.core.document.Document;
3032
import software.amazon.awssdk.core.protocol.MarshallingType;
3133
import software.amazon.awssdk.core.traits.ListTrait;
3234
import software.amazon.awssdk.core.traits.MapTrait;
@@ -75,6 +77,8 @@ private void writeObject(Object value, SdkField<?> sdkField, JsonGenerator gen,
7577
writeSdkList((Collection<Object>) value, sdkField, gen, serializers);
7678
} else if (type.equals(MarshallingType.MAP)) {
7779
writeSdkMap((Map<String, Object>) value, sdkField, gen, serializers);
80+
} else if (type.equals(MarshallingType.DOCUMENT)) {
81+
writeSdkDocument((Document) value, gen);
7882
}
7983
}
8084

@@ -134,4 +138,36 @@ private void writeSdkMap(Map<String, Object> map, SdkField<?> sdkField, JsonGene
134138
}
135139
gen.writeEndObject();
136140
}
141+
142+
private void writeSdkDocument(Document document, JsonGenerator gen) throws IOException {
143+
if (document == null) {
144+
gen.writeNull();
145+
return;
146+
}
147+
if (document.isMap()) {
148+
Map<String, Document> map = document.asMap();
149+
gen.writeStartObject();
150+
for (Map.Entry<String, Document> each : map.entrySet()) {
151+
gen.writeFieldName(each.getKey());
152+
Document value = each.getValue();
153+
writeSdkDocument(value, gen);
154+
}
155+
gen.writeEndObject();
156+
} else if (document.isList()) {
157+
gen.writeStartArray();
158+
List<Document> collection = document.asList();
159+
for (Document each : collection) {
160+
writeSdkDocument(each, gen);
161+
}
162+
gen.writeEndArray();
163+
} else if (document.isBoolean()) {
164+
gen.writeBoolean(document.asBoolean());
165+
} else if (document.isNumber()) {
166+
gen.writeNumber(document.asNumber().stringValue());
167+
} else if (document.isString()) {
168+
gen.writeString(document.asString());
169+
} else if (document.isNull()) {
170+
gen.writeNull();
171+
}
172+
}
137173
}

src/test/java/software/amazon/cloudformation/proxy/aws/AWSServiceSerdeTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import software.amazon.awssdk.core.SdkBytes;
3535
import software.amazon.awssdk.core.SdkField;
3636
import software.amazon.awssdk.core.SdkPojo;
37+
import software.amazon.awssdk.core.document.Document;
3738
import software.amazon.awssdk.core.protocol.MarshallLocation;
3839
import software.amazon.awssdk.core.protocol.MarshallingType;
3940
import software.amazon.awssdk.core.traits.LocationTrait;
@@ -47,6 +48,9 @@
4748
import software.amazon.awssdk.services.cloudwatchevents.model.PutTargetsResultEntry;
4849
import software.amazon.awssdk.services.cloudwatchevents.model.Tag;
4950
import software.amazon.awssdk.services.cloudwatchevents.model.Target;
51+
import software.amazon.awssdk.services.kendra.model.DataSourceConfiguration;
52+
import software.amazon.awssdk.services.kendra.model.TemplateConfiguration;
53+
import software.amazon.awssdk.services.kendra.model.UpdateDataSourceRequest;
5054
import software.amazon.awssdk.utils.builder.SdkBuilder;
5155
import software.amazon.cloudformation.resource.Serializer;
5256

@@ -433,4 +437,30 @@ public void typeMismatchError() {
433437
assertThat(exception.getMessage()).contains("Type mismatch, expecting " + MarshallingType.MAP + " got List type");
434438
}
435439

440+
@Test
441+
public void documentFieldSerde() throws Exception {
442+
Document template = Document.mapBuilder().putString("type", "S3")
443+
.putDocument("connectionConfiguration",
444+
Document.mapBuilder()
445+
.putDocument("repositoryEndpointMetadata", Document.mapBuilder().putString("BucketName", "mybucket").build())
446+
.build())
447+
.putDocument("repositoryConfigurations",
448+
Document.mapBuilder()
449+
.putDocument("document",
450+
Document.mapBuilder().putDocument("fieldMappings", Document.listBuilder()
451+
.addDocument(Document.mapBuilder().putString("indexFieldName", "foo")
452+
.putString("indexFieldType", "STRING").putString("dataSourceFieldName", "foo").build())
453+
.build()).build())
454+
.build())
455+
.putBoolean("booleanValue", false).putNumber("numberValue", 1).putNull("nullValue").build();
456+
UpdateDataSourceRequest request = UpdateDataSourceRequest.builder()
457+
.configuration(DataSourceConfiguration.builder()
458+
.templateConfiguration(TemplateConfiguration.builder().template(template).build()).build())
459+
.id("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").indexId("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").build();
460+
461+
String json = serializer.serialize(request);
462+
UpdateDataSourceRequest deserialized = serializer.deserialize(json, new TypeReference<UpdateDataSourceRequest>() {
463+
});
464+
assertThat(deserialized).isEqualTo(request);
465+
}
436466
}

0 commit comments

Comments
 (0)