use of org.springframework.data.mapping.PreferredConstructor in project spring-cloud-gcp by spring-cloud.
the class ConverterAwareMappingSpannerEntityReader method read.
/**
* Reads a single POJO from a Cloud Spanner row.
* @param type the type of POJO
* @param source the Cloud Spanner row
* @param includeColumns the columns to read. If null then all columns will be read.
* @param allowMissingColumns if true, then properties with no corresponding column are
* not mapped. If false, then an exception is thrown.
* @param <R> the type of the POJO.
* @return the POJO
*/
@SuppressWarnings("unchecked")
public <R> R read(Class<R> type, Struct source, Set<String> includeColumns, boolean allowMissingColumns) {
boolean readAllColumns = includeColumns == null;
SpannerPersistentEntity<R> persistentEntity = (SpannerPersistentEntity<R>) this.spannerMappingContext.getPersistentEntity(type);
StructAccessor structAccessor = new StructAccessor(source);
StructPropertyValueProvider propertyValueProvider = new StructPropertyValueProvider(structAccessor, this.converter, this, allowMissingColumns);
PreferredConstructor<?, SpannerPersistentProperty> persistenceConstructor = persistentEntity.getPersistenceConstructor();
// @formatter:off
ParameterValueProvider<SpannerPersistentProperty> parameterValueProvider = new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null);
// @formatter:on
EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity);
R instance = instantiator.createInstance(persistentEntity, parameterValueProvider);
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance);
persistentEntity.doWithProperties((PropertyHandler<SpannerPersistentProperty>) (spannerPersistentProperty) -> {
if (spannerPersistentProperty.isEmbedded()) {
accessor.setProperty(spannerPersistentProperty, read(spannerPersistentProperty.getType(), source, includeColumns, allowMissingColumns));
} else {
if (!shouldSkipProperty(structAccessor, spannerPersistentProperty, includeColumns, readAllColumns, allowMissingColumns, persistenceConstructor)) {
Object value = propertyValueProvider.getPropertyValue(spannerPersistentProperty);
accessor.setProperty(spannerPersistentProperty, value);
}
}
});
return instance;
}
Aggregations