Polishing.

Reformat code. Tweak documentation wording.

See #3596
Original pull request: #3982.
This commit is contained in:
Mark Paluch
2022-03-21 09:19:59 +01:00
parent 29fb085d8b
commit 8672808222
6 changed files with 63 additions and 57 deletions

View File

@@ -176,8 +176,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Assert.notNull(path, "ObjectPath must not be null");
return new ConversionContext(this, conversions, path, this::readDocument, this::readCollectionOrArray, this::readMap,
this::readDBRef, this::getPotentiallyConvertedSimpleRead);
return new ConversionContext(this, conversions, path, this::readDocument, this::readCollectionOrArray,
this::readMap, this::readDBRef, this::getPotentiallyConvertedSimpleRead);
}
/**
@@ -393,18 +393,18 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
EntityProjection<?, ?> property = returnedTypeDescriptor.findProperty(name);
if (property == null) {
return new ConversionContext(conversions, path, MappingMongoConverter.this::readDocument, collectionConverter,
return new ConversionContext(sourceConverter, conversions, path, MappingMongoConverter.this::readDocument, collectionConverter,
mapConverter, dbRefConverter, elementConverter);
}
return new ProjectingConversionContext(sourceConverter, conversions, path, collectionConverter, mapConverter, dbRefConverter,
elementConverter, property);
return new ProjectingConversionContext(sourceConverter, conversions, path, collectionConverter, mapConverter,
dbRefConverter, elementConverter, property);
}
@Override
public ConversionContext withPath(ObjectPath currentPath) {
return new ProjectingConversionContext(sourceConverter, conversions, currentPath, collectionConverter, mapConverter,
dbRefConverter, elementConverter, returnedTypeDescriptor);
return new ProjectingConversionContext(sourceConverter, conversions, currentPath, collectionConverter,
mapConverter, dbRefConverter, elementConverter, returnedTypeDescriptor);
}
}
@@ -935,8 +935,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
TypeInformation<?> type = prop.getTypeInformation();
if(conversions.hasPropertyValueConverter(prop)) {
accessor.put(prop, conversions.getPropertyValueConverter(prop).write(obj, new MongoConversionContext(prop, this)));
if (conversions.hasPropertyValueConverter(prop)) {
accessor.put(prop,
conversions.getPropertyValueConverter(prop).write(obj, new MongoConversionContext(prop, this)));
return;
}
@@ -1270,8 +1271,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
private void writeSimpleInternal(@Nullable Object value, Bson bson, MongoPersistentProperty property) {
DocumentAccessor accessor = new DocumentAccessor(bson);
if(conversions.hasPropertyValueConverter(property)) {
accessor.put(property, conversions.getPropertyValueConverter(property).write(value, new MongoConversionContext(property, this)));
if (conversions.hasPropertyValueConverter(property)) {
accessor.put(property,
conversions.getPropertyValueConverter(property).write(value, new MongoConversionContext(property, this)));
return;
}
@@ -1916,9 +1918,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return null;
}
if(context.conversions.hasPropertyValueConverter(property)) {
return (T) context.conversions.getPropertyValueConverter(property).read(value, new MongoConversionContext(property, context.sourceConverter));
if (context.conversions.hasPropertyValueConverter(property)) {
return (T) context.conversions.getPropertyValueConverter(property).read(value,
new MongoConversionContext(property, context.sourceConverter));
}
return (T) context.convert(value, property.getTypeInformation());
@@ -2148,7 +2150,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
final ContainerValueConverter<DBRef> dbRefConverter;
final ValueConverter<Object> elementConverter;
ConversionContext(MongoConverter sourceConverter, org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
ConversionContext(MongoConverter sourceConverter,
org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
ContainerValueConverter<Bson> documentConverter, ContainerValueConverter<Collection<?>> collectionConverter,
ContainerValueConverter<Bson> mapConverter, ContainerValueConverter<DBRef> dbRefConverter,
ValueConverter<Object> elementConverter) {
@@ -2235,8 +2238,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Assert.notNull(currentPath, "ObjectPath must not be null");
return new ConversionContext(sourceConverter, conversions, currentPath, documentConverter, collectionConverter, mapConverter,
dbRefConverter, elementConverter);
return new ConversionContext(sourceConverter, conversions, currentPath, documentConverter, collectionConverter,
mapConverter, dbRefConverter, elementConverter);
}
public ObjectPath getPath() {

View File

@@ -50,11 +50,7 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@Override
public <T> T read(@Nullable Object value, TypeInformation<T> target) {
if (!(value instanceof Bson)) {
return ValueConversionContext.super.read(value, target);
}
return mongoConverter.read(target.getType(), (Bson) value);
return value instanceof Bson ? mongoConverter.read(target.getType(), (Bson) value)
: ValueConversionContext.super.read(value, target);
}
}

View File

@@ -36,6 +36,7 @@ import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.data.convert.ConverterBuilder;
import org.springframework.data.convert.PropertyValueConversions;
import org.springframework.data.convert.PropertyValueConverter;
import org.springframework.data.convert.PropertyValueConverterFactory;
@@ -263,7 +264,8 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
}
/**
* Add {@link Converter converters}, {@link ConverterFactory factories}, ...
* Add {@link Converter converters}, {@link ConverterFactory factories}, {@link ConverterBuilder.ConverterAware
* converter-aware objects}, and {@link GenericConverter generic converters}.
*
* @param converters must not be {@literal null} nor contain {@literal null} values.
* @return this.
@@ -307,6 +309,7 @@ public class MongoCustomConversions extends org.springframework.data.convert.Cus
*/
public MongoConverterConfigurationAdapter setPropertyValueConversions(PropertyValueConversions valueConversions) {
Assert.notNull(valueConversions, "PropertyValueConversions must not be null");
this.propertyValueConversions = valueConversions;
return this;
}

View File

@@ -18,11 +18,9 @@ package org.springframework.data.mongodb.core.convert;
import org.springframework.data.convert.PropertyValueConverter;
/**
* Pre typed {@link PropertyValueConverter} specific for the Data MongoDB module.
*
* MongoDB-specific {@link PropertyValueConverter} extension.
*
* @author Christoph Strobl
* @since 3.4
*/
public interface MongoValueConverter<S, T> extends PropertyValueConverter<S, T, MongoConversionContext> {
}
public interface MongoValueConverter<S, T> extends PropertyValueConverter<S, T, MongoConversionContext> {}