use of com.google.cloud.spring.data.datastore.core.mapping.EmbeddedType in project spring-cloud-gcp by GoogleCloudPlatform.
the class TwoStepsConversions method convertOnRead.
private <T> T convertOnRead(Object val, EmbeddedType embeddedType, Class targetCollectionType, TypeInformation targetComponentType) {
if (val == null) {
return null;
}
BiFunction<Object, TypeInformation<?>, ?> readConverter;
switch(embeddedType) {
case EMBEDDED_MAP:
readConverter = (x, typeInformation) -> convertOnReadSingleEmbeddedMap(x, typeInformation.getComponentType().getType(), typeInformation.getMapValueType(), targetComponentType);
break;
case EMBEDDED_ENTITY:
readConverter = this::convertOnReadSingleEmbedded;
break;
case NOT_EMBEDDED:
readConverter = this::convertOnReadSingle;
break;
default:
throw new DatastoreDataException("Unexpected property embedded type: " + embeddedType);
}
if (ValueUtil.isCollectionLike(val.getClass()) && targetCollectionType != null && targetComponentType != null) {
// Convert collection.
try {
Assert.isInstanceOf(Iterable.class, val, "Value passed to convertOnRead expected to be Iterable");
List elements = StreamSupport.stream(((Iterable<?>) val).spliterator(), false).map(v -> {
Object o = (v instanceof Value) ? ((Value) v).get() : v;
return readConverter.apply(o, targetComponentType);
}).collect(Collectors.toList());
return (T) convertCollection(elements, targetCollectionType);
} catch (ConversionException | DatastoreDataException ex) {
throw new DatastoreDataException("Unable process elements of a collection", ex);
}
}
// Convert single value.
return (T) readConverter.apply(val, targetComponentType);
}
Aggregations