Search in sources :

Example 1 with EmbeddedType

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);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) CustomConversions(org.springframework.data.convert.CustomConversions) BiFunction(java.util.function.BiFunction) ValueUtil(com.google.cloud.spring.data.datastore.core.util.ValueUtil) Builder(com.google.cloud.datastore.FullEntity.Builder) TypeInformation(org.springframework.data.util.TypeInformation) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) ValueUtil.boxIfNeeded(com.google.cloud.spring.data.datastore.core.util.ValueUtil.boxIfNeeded) EmbeddedType(com.google.cloud.spring.data.datastore.core.mapping.EmbeddedType) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) Map(java.util.Map) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) StreamSupport(java.util.stream.StreamSupport) Converter(org.springframework.core.convert.converter.Converter) ClassUtils(org.springframework.util.ClassUtils) BaseEntity(com.google.cloud.datastore.BaseEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EntityValue(com.google.cloud.datastore.EntityValue) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) ConversionException(org.springframework.core.convert.ConversionException) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Collectors(java.util.stream.Collectors) PersistentEntity(org.springframework.data.mapping.PersistentEntity) Consumer(java.util.function.Consumer) List(java.util.List) Value(com.google.cloud.datastore.Value) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Optional(java.util.Optional) Blob(com.google.cloud.datastore.Blob) ListValue(com.google.cloud.datastore.ListValue) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Assert(org.springframework.util.Assert) ConversionException(org.springframework.core.convert.ConversionException) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) EntityValue(com.google.cloud.datastore.EntityValue) Value(com.google.cloud.datastore.Value) ListValue(com.google.cloud.datastore.ListValue) ArrayList(java.util.ArrayList) List(java.util.List) TypeInformation(org.springframework.data.util.TypeInformation) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation)

Aggregations

BaseEntity (com.google.cloud.datastore.BaseEntity)1 Blob (com.google.cloud.datastore.Blob)1 EntityValue (com.google.cloud.datastore.EntityValue)1 FullEntity (com.google.cloud.datastore.FullEntity)1 Builder (com.google.cloud.datastore.FullEntity.Builder)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1 ListValue (com.google.cloud.datastore.ListValue)1 Value (com.google.cloud.datastore.Value)1 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)1 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)1 DatastorePersistentProperty (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)1 EmbeddedType (com.google.cloud.spring.data.datastore.core.mapping.EmbeddedType)1 ValueUtil (com.google.cloud.spring.data.datastore.core.util.ValueUtil)1 ValueUtil.boxIfNeeded (com.google.cloud.spring.data.datastore.core.util.ValueUtil.boxIfNeeded)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BiFunction (java.util.function.BiFunction)1