Search in sources :

Example 16 with DatastoreDataException

use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.

the class DefaultDatastoreEntityConverter method readAsMap.

@Override
public <T, R> Map<T, R> readAsMap(BaseEntity entity, TypeInformation mapTypeInformation) {
    Assert.notNull(mapTypeInformation, "mapTypeInformation can't be null");
    if (entity == null) {
        return null;
    }
    Map<T, R> result;
    if (!mapTypeInformation.getType().isInterface()) {
        try {
            result = (Map<T, R>) ((Constructor<?>) mapTypeInformation.getType().getConstructor()).newInstance();
        } catch (Exception e) {
            throw new DatastoreDataException("Unable to create an instance of a custom map type: " + mapTypeInformation.getType() + " (make sure the class is public and has a public no-args constructor)", e);
        }
    } else {
        result = new HashMap<>();
    }
    EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider(entity, this.conversions);
    Set<String> fieldNames = entity.getNames();
    for (String field : fieldNames) {
        result.put(this.conversions.convertOnRead(field, NOT_EMBEDDED, mapTypeInformation.getComponentType()), propertyValueProvider.getPropertyValue(field, EmbeddedType.of(mapTypeInformation.getMapValueType()), mapTypeInformation.getMapValueType()));
    }
    return result;
}
Also used : Constructor(java.lang.reflect.Constructor) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)

Aggregations

DatastoreDataException (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)16 Key (com.google.cloud.datastore.Key)5 Value (com.google.cloud.datastore.Value)5 DatastorePersistentEntity (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity)5 DatastorePersistentProperty (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)5 IncompleteKey (com.google.cloud.datastore.IncompleteKey)4 KeyValue (com.google.cloud.datastore.KeyValue)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Function (java.util.function.Function)4 Pageable (org.springframework.data.domain.Pageable)4 Sort (org.springframework.data.domain.Sort)4 BaseEntity (com.google.cloud.datastore.BaseEntity)3 Cursor (com.google.cloud.datastore.Cursor)3 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)3 EntityQuery (com.google.cloud.datastore.EntityQuery)3 KeyQuery (com.google.cloud.datastore.KeyQuery)3 ListValue (com.google.cloud.datastore.ListValue)3 NullValue (com.google.cloud.datastore.NullValue)3 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)3