Skip to content

Commit fc1725f

Browse files
authored
converter utility to cast multi-typed properties (#329)
1 parent 75133ad commit fc1725f

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package software.amazon.cloudformation.resource;
16+
17+
import com.fasterxml.jackson.core.JsonProcessingException;
18+
import com.fasterxml.jackson.core.type.TypeReference;
19+
import java.io.IOException;
20+
21+
public class TypeConverter {
22+
protected TypeConverter() {
23+
throw new UnsupportedOperationException();
24+
}
25+
26+
public static Object convertProperty(final Object input, final TypeReference<?>... typeReferences) // preserves order
27+
{
28+
try {
29+
final Serializer serializer = new Serializer();
30+
final String stringified_input = serializer.serialize(input);
31+
32+
int iter = 0;
33+
while (true) {
34+
TypeReference<?> typeReference = typeReferences[iter];
35+
try {
36+
return serializer.deserialize(stringified_input, typeReference);
37+
} catch (JsonProcessingException exception) {
38+
if (iter < typeReferences.length - 1) {
39+
iter++;
40+
} else {
41+
throw new RuntimeException("No Suitable Type Reference");
42+
}
43+
}
44+
}
45+
} catch (IOException exception) {
46+
throw new RuntimeException("Invalid Object to Stringify");
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package software.amazon.cloudformation.resource;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
import com.fasterxml.jackson.annotation.JsonProperty;
19+
import com.fasterxml.jackson.core.type.TypeReference;
20+
import java.io.IOException;
21+
import java.util.List;
22+
import lombok.Data;
23+
import org.junit.jupiter.api.Test;
24+
25+
public class TypeConverterTest {
26+
@Data
27+
public static class ComplexObject {
28+
@JsonProperty("Key")
29+
private String key;
30+
31+
@JsonProperty("Value")
32+
private String value;
33+
}
34+
35+
public static final String LIST_OF_OBJECTS = "[{\"Key\" : \"Key\",\"Value\" :\"Value\"},{\"Key\" : \"Key\",\"Value\":\"Value\"}]";
36+
public static final String OBJECT = "{\"Key\" : \"Key\",\"Value\" : \"Value\"}";
37+
38+
final TypeReference<ComplexObject> typeReferenceComplexObject = new TypeReference<ComplexObject>() {
39+
};
40+
final TypeReference<List<ComplexObject>> typeReferenceComplexObjectList = new TypeReference<List<ComplexObject>>() {
41+
};
42+
43+
@Test
44+
@SuppressWarnings("unchecked")
45+
public void castMultiTypePropertyToList() throws IOException {
46+
final Serializer ser = new Serializer();
47+
48+
// mimics if multitype was casted to an object
49+
final Object multiTypeProperty = ser.deserialize(LIST_OF_OBJECTS, new TypeReference<Object>() {
50+
});
51+
52+
Object converted = TypeConverter.convertProperty(multiTypeProperty, typeReferenceComplexObject,
53+
typeReferenceComplexObjectList);
54+
55+
assertThat(converted).isInstanceOf(List.class);
56+
57+
List<ComplexObject> complexObject = (List<ComplexObject>) converted;
58+
assertThat(complexObject.size()).isEqualTo(2);
59+
assertThat(complexObject.get(0)).isInstanceOf(ComplexObject.class);
60+
assertThat(complexObject.get(0).key).isEqualTo("Key");
61+
assertThat(complexObject.get(0).value).isEqualTo("Value");
62+
assertThat(complexObject.get(1).key).isEqualTo("Key");
63+
assertThat(complexObject.get(1).value).isEqualTo("Value");
64+
}
65+
66+
@Test
67+
public void castMultiTypePropertyToObject() throws IOException {
68+
final Serializer ser = new Serializer();
69+
70+
// mimics if multitype was casted to an object
71+
final Object multiTypeProperty = ser.deserialize(OBJECT, new TypeReference<Object>() {
72+
});
73+
74+
Object converted = TypeConverter.convertProperty(multiTypeProperty, typeReferenceComplexObject,
75+
typeReferenceComplexObjectList);
76+
assertThat(converted).isInstanceOf(ComplexObject.class);
77+
ComplexObject complexObject = (ComplexObject) converted;
78+
assertThat(complexObject.key).isEqualTo("Key");
79+
assertThat(complexObject.value).isEqualTo("Value");
80+
}
81+
82+
@Test
83+
public void castMultiTypePropertyWithInvalidReferences() throws IOException {
84+
final Serializer ser = new Serializer();
85+
86+
// mimics if multitype was casted to an object
87+
final Object multiTypeProperty = ser.deserialize(OBJECT, new TypeReference<Object>() {
88+
});
89+
90+
try {
91+
Object converted = TypeConverter.convertProperty(multiTypeProperty, new TypeReference<Integer>() {
92+
}, new TypeReference<Boolean>() {
93+
});
94+
95+
} catch (RuntimeException e) {
96+
assertThat(e.getMessage()).isEqualTo("No Suitable Type Reference");
97+
}
98+
}
99+
100+
}

0 commit comments

Comments
 (0)