mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#295 Replacing MapNullToDefault with NullValueMapping
This commit is contained in:
parent
ac0a532fb4
commit
8ea20d4315
@ -109,5 +109,5 @@ public @interface Mapper {
|
||||
*
|
||||
* @return The strategy applied when determining whether to return null or an empty object, list or map.
|
||||
*/
|
||||
MapNullToDefaultStrategy mapNullToDefaultStrategy() default MapNullToDefaultStrategy.DEFAULT;
|
||||
NullValueMappingStrategy nullValueMappingStrategy() default NullValueMappingStrategy.DEFAULT;
|
||||
}
|
||||
|
@ -87,5 +87,5 @@ public @interface MapperConfig {
|
||||
*
|
||||
* @return The strategy applied when determining whether to return null or an empty object, list or map.
|
||||
*/
|
||||
MapNullToDefaultStrategy mapNullToDefaultStrategy() default MapNullToDefaultStrategy.DEFAULT;
|
||||
NullValueMappingStrategy nullValueMappingStrategy() default NullValueMappingStrategy.DEFAULT;
|
||||
}
|
||||
|
@ -38,9 +38,9 @@ import java.lang.annotation.Target;
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Target( { ElementType.METHOD, ElementType.TYPE } )
|
||||
@Target( { ElementType.METHOD } )
|
||||
@Retention( RetentionPolicy.SOURCE )
|
||||
public @interface MapNullToDefault {
|
||||
public @interface NullValueMapping {
|
||||
|
||||
MapNullToDefaultStrategy value() default MapNullToDefaultStrategy.MAP_NULL_TO_DEFAULT;
|
||||
NullValueMappingStrategy value() default NullValueMappingStrategy.RETURN_DEFAULT;
|
||||
}
|
@ -23,12 +23,12 @@ package org.mapstruct;
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public enum MapNullToDefaultStrategy {
|
||||
public enum NullValueMappingStrategy {
|
||||
|
||||
/**
|
||||
* A null source argument of a mapping method will be mapped to a null target result
|
||||
*/
|
||||
MAP_NULL_TO_NULL,
|
||||
RETURN_NULL,
|
||||
|
||||
/**
|
||||
* A null source argument of a mapping method will be mapped to a default target result
|
||||
@ -40,17 +40,16 @@ public enum MapNullToDefaultStrategy {
|
||||
* <li>For an map mapping this means a {@link java.util.Collections#emptyMap() } will be returned<\li>
|
||||
* </ol>
|
||||
*/
|
||||
MAP_NULL_TO_DEFAULT,
|
||||
RETURN_DEFAULT,
|
||||
|
||||
/**
|
||||
* When given via {@link Mapper#mapNullToDefaultStrategy()}, causes the setting specified via
|
||||
* {@link MapperConfig#mapNullToDefaultStrategy()} to be applied, if present.
|
||||
* When given via {@link Mapper#nullValueMappingStrategy() ()}, causes the setting specified via
|
||||
* {@link MapperConfig#nullValueMappingStrategy() ()} to be applied, if present.
|
||||
* <p>
|
||||
* When given via {@link MapNullToDefault#mapNullToDefaultStrategy()}, causes the setting specified via
|
||||
* {@link Mapper#mapNullToDefaultStrategy()} to be applied, if present.
|
||||
* When given via {@link NullValueMapping#value() ()}, causes the setting specified via
|
||||
* {@link Mapper#nullValueMappingStrategy() ()} to be applied, if present.
|
||||
* <p>
|
||||
* Otherwise causes
|
||||
* {@link #MAP_NULL_TO_NULL} to be applied.
|
||||
* Otherwise causes {@link #RETURN_NULL} to be applied.
|
||||
*/
|
||||
DEFAULT;
|
||||
}
|
@ -40,7 +40,7 @@ import org.mapstruct.ap.model.source.Mapping;
|
||||
import org.mapstruct.ap.model.source.SourceMethod;
|
||||
import org.mapstruct.ap.model.source.SourceReference;
|
||||
import org.mapstruct.ap.option.ReportingPolicy;
|
||||
import org.mapstruct.ap.prism.MapNullToDefaultPrism;
|
||||
import org.mapstruct.ap.prism.NullValueMappingPrism;
|
||||
import org.mapstruct.ap.prism.CollectionMappingStrategyPrism;
|
||||
import org.mapstruct.ap.util.Executables;
|
||||
import org.mapstruct.ap.util.MapperConfig;
|
||||
@ -100,7 +100,7 @@ public class BeanMappingMethod extends MappingMethod {
|
||||
reportErrorForUnmappedTargetPropertiesIfRequired();
|
||||
|
||||
// mapNullToDefault
|
||||
MapNullToDefaultPrism prism = MapNullToDefaultPrism.getInstanceOn( method.getExecutable() );
|
||||
NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() );
|
||||
boolean mapNullToDefault =
|
||||
MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism );
|
||||
|
||||
|
@ -31,7 +31,7 @@ import org.mapstruct.ap.model.common.Parameter;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.model.source.Method;
|
||||
import org.mapstruct.ap.prism.MapNullToDefaultPrism;
|
||||
import org.mapstruct.ap.prism.NullValueMappingPrism;
|
||||
import org.mapstruct.ap.util.MapperConfig;
|
||||
import org.mapstruct.ap.util.Strings;
|
||||
|
||||
@ -109,7 +109,7 @@ public class IterableMappingMethod extends MappingMethod {
|
||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||
|
||||
// mapNullToDefault
|
||||
MapNullToDefaultPrism prism = MapNullToDefaultPrism.getInstanceOn( method.getExecutable() );
|
||||
NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() );
|
||||
boolean mapNullToDefault
|
||||
= MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism );
|
||||
|
||||
|
@ -31,7 +31,7 @@ import org.mapstruct.ap.model.common.Parameter;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.model.source.Method;
|
||||
import org.mapstruct.ap.prism.MapNullToDefaultPrism;
|
||||
import org.mapstruct.ap.prism.NullValueMappingPrism;
|
||||
import org.mapstruct.ap.util.MapperConfig;
|
||||
import org.mapstruct.ap.util.Strings;
|
||||
|
||||
@ -141,7 +141,7 @@ public class MapMappingMethod extends MappingMethod {
|
||||
}
|
||||
|
||||
// mapNullToDefault
|
||||
MapNullToDefaultPrism prism = MapNullToDefaultPrism.getInstanceOn( method.getExecutable() );
|
||||
NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() );
|
||||
boolean mapNullToDefault =
|
||||
MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism );
|
||||
|
||||
|
@ -23,9 +23,9 @@ package org.mapstruct.ap.prism;
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public enum MapNullToDefaultStrategyPrism {
|
||||
public enum NullValueMappingStrategyPrism {
|
||||
|
||||
MAP_NULL_TO_NULL,
|
||||
MAP_NULL_TO_DEFAULT,
|
||||
RETURN_NULL,
|
||||
RETURN_DEFAULT,
|
||||
DEFAULT;
|
||||
}
|
@ -27,7 +27,7 @@ import org.mapstruct.DecoratedWith;
|
||||
import org.mapstruct.InheritInverseConfiguration;
|
||||
import org.mapstruct.IterableMapping;
|
||||
import org.mapstruct.MapMapping;
|
||||
import org.mapstruct.MapNullToDefault;
|
||||
import org.mapstruct.NullValueMapping;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MapperConfig;
|
||||
import org.mapstruct.Mapping;
|
||||
@ -53,7 +53,7 @@ import org.mapstruct.TargetType;
|
||||
@GeneratePrism(value = MapperConfig.class, publicAccess = true),
|
||||
@GeneratePrism(value = InheritInverseConfiguration.class, publicAccess = true),
|
||||
@GeneratePrism( value = Qualifier.class, publicAccess = true ),
|
||||
@GeneratePrism( value = MapNullToDefault.class, publicAccess = true ),
|
||||
@GeneratePrism( value = NullValueMapping.class, publicAccess = true ),
|
||||
|
||||
// external types
|
||||
@GeneratePrism(value = XmlElementDecl.class, publicAccess = true)
|
||||
|
@ -29,8 +29,8 @@ import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
|
||||
import org.mapstruct.ap.prism.MapNullToDefaultStrategyPrism;
|
||||
import org.mapstruct.ap.prism.MapNullToDefaultPrism;
|
||||
import org.mapstruct.ap.prism.NullValueMappingStrategyPrism;
|
||||
import org.mapstruct.ap.prism.NullValueMappingPrism;
|
||||
|
||||
import org.mapstruct.ap.option.ReportingPolicy;
|
||||
import org.mapstruct.ap.prism.CollectionMappingStrategyPrism;
|
||||
@ -119,37 +119,37 @@ public class MapperConfig {
|
||||
return CollectionMappingStrategyPrism.ACCESSOR_ONLY;
|
||||
}
|
||||
|
||||
public boolean isMapToDefault(MapNullToDefaultPrism mapNullToDefault) {
|
||||
public boolean isMapToDefault(NullValueMappingPrism mapNullToDefault) {
|
||||
|
||||
// check on method level
|
||||
if ( mapNullToDefault != null ) {
|
||||
MapNullToDefaultStrategyPrism methodPolicy
|
||||
= MapNullToDefaultStrategyPrism.valueOf( mapNullToDefault.value() );
|
||||
if ( methodPolicy != MapNullToDefaultStrategyPrism.DEFAULT ) {
|
||||
return methodPolicy == MapNullToDefaultStrategyPrism.MAP_NULL_TO_DEFAULT;
|
||||
NullValueMappingStrategyPrism methodPolicy
|
||||
= NullValueMappingStrategyPrism.valueOf( mapNullToDefault.value() );
|
||||
if ( methodPolicy != NullValueMappingStrategyPrism.DEFAULT ) {
|
||||
return methodPolicy == NullValueMappingStrategyPrism.RETURN_DEFAULT;
|
||||
}
|
||||
}
|
||||
|
||||
// check on mapper level
|
||||
MapNullToDefaultStrategyPrism mapperPolicy =
|
||||
MapNullToDefaultStrategyPrism.valueOf( mapperPrism.mapNullToDefaultStrategy() );
|
||||
NullValueMappingStrategyPrism mapperPolicy =
|
||||
NullValueMappingStrategyPrism.valueOf( mapperPrism.nullValueMappingStrategy() );
|
||||
|
||||
if ( mapperPolicy != MapNullToDefaultStrategyPrism.DEFAULT ) {
|
||||
if ( mapperPolicy != NullValueMappingStrategyPrism.DEFAULT ) {
|
||||
// it is not the default mapper configuration, so return the mapper configured value
|
||||
return mapperPolicy == MapNullToDefaultStrategyPrism.MAP_NULL_TO_DEFAULT;
|
||||
return mapperPolicy == NullValueMappingStrategyPrism.RETURN_DEFAULT;
|
||||
}
|
||||
|
||||
// check on mapping config level
|
||||
else if ( mapperConfigPrism != null ) {
|
||||
// try the config mapper configuration
|
||||
MapNullToDefaultStrategyPrism configPolicy =
|
||||
MapNullToDefaultStrategyPrism.valueOf( mapperConfigPrism.mapNullToDefaultStrategy() );
|
||||
if ( configPolicy != MapNullToDefaultStrategyPrism.DEFAULT ) {
|
||||
NullValueMappingStrategyPrism configPolicy =
|
||||
NullValueMappingStrategyPrism.valueOf( mapperConfigPrism.nullValueMappingStrategy() );
|
||||
if ( configPolicy != NullValueMappingStrategyPrism.DEFAULT ) {
|
||||
// its not the default configuration, so return the mapper config configured value
|
||||
return configPolicy == MapNullToDefaultStrategyPrism.MAP_NULL_TO_DEFAULT;
|
||||
return configPolicy == NullValueMappingStrategyPrism.RETURN_DEFAULT;
|
||||
}
|
||||
}
|
||||
// when nothing specified, return MAP_NULL_TO_NULL (default option)
|
||||
// when nothing specified, return RETURN_NULL (default option)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MapNullToDefault;
|
||||
import org.mapstruct.NullValueMapping;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.ap.test.mapnulltodefault.source.Car;
|
||||
@ -35,7 +35,7 @@ public interface CarMapper {
|
||||
|
||||
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
|
||||
|
||||
@MapNullToDefault
|
||||
@NullValueMapping
|
||||
@Mappings({
|
||||
@Mapping(target = "seatCount", source = "numberOfSeats"),
|
||||
@Mapping(target = "model", constant = "ModelT"),
|
||||
@ -43,7 +43,7 @@ public interface CarMapper {
|
||||
})
|
||||
CarDto carToCarDto(Car car);
|
||||
|
||||
@MapNullToDefault
|
||||
@NullValueMapping
|
||||
@Mappings({
|
||||
@Mapping(target = "seatCount", source = "car.numberOfSeats"),
|
||||
@Mapping(target = "model", source = "model"), // TODO, should not be needed, must be made based on name only
|
||||
@ -52,10 +52,10 @@ public interface CarMapper {
|
||||
CarDto carToCarDto(Car car, String model);
|
||||
|
||||
|
||||
@MapNullToDefault
|
||||
@NullValueMapping
|
||||
List<CarDto> carsToCarDtos(List<Car> cars);
|
||||
|
||||
|
||||
@MapNullToDefault
|
||||
@NullValueMapping
|
||||
Map<Integer, CarDto> carsToCarDtoMap(Map<Integer, Car> cars);
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MapNullToDefault;
|
||||
import org.mapstruct.MapNullToDefaultStrategy;
|
||||
import org.mapstruct.NullValueMapping;
|
||||
import org.mapstruct.NullValueMappingStrategy;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.ap.test.mapnulltodefault.source.Car;
|
||||
@ -44,10 +44,10 @@ public interface CarMapperSettingOnConfig {
|
||||
CarDto carToCarDto(Car car);
|
||||
|
||||
|
||||
@MapNullToDefault(MapNullToDefaultStrategy.DEFAULT)
|
||||
@NullValueMapping(NullValueMappingStrategy.DEFAULT)
|
||||
List<CarDto> carsToCarDtos(List<Car> cars);
|
||||
|
||||
|
||||
@MapNullToDefault(MapNullToDefaultStrategy.MAP_NULL_TO_NULL)
|
||||
@NullValueMapping(NullValueMappingStrategy.RETURN_NULL)
|
||||
Map<Integer, CarDto> carsToCarDtoMap(Map<Integer, Car> cars);
|
||||
}
|
||||
|
@ -23,15 +23,15 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MapNullToDefault;
|
||||
import org.mapstruct.MapNullToDefaultStrategy;
|
||||
import org.mapstruct.NullValueMapping;
|
||||
import org.mapstruct.NullValueMappingStrategy;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.ap.test.mapnulltodefault.source.Car;
|
||||
import org.mapstruct.ap.test.mapnulltodefault.target.CarDto;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper(imports = UUID.class, mapNullToDefaultStrategy = MapNullToDefaultStrategy.MAP_NULL_TO_DEFAULT )
|
||||
@Mapper(imports = UUID.class, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT )
|
||||
public interface CarMapperSettingOnMapper {
|
||||
|
||||
CarMapperSettingOnMapper INSTANCE = Mappers.getMapper( CarMapperSettingOnMapper.class );
|
||||
@ -44,10 +44,10 @@ public interface CarMapperSettingOnMapper {
|
||||
CarDto carToCarDto(Car car);
|
||||
|
||||
|
||||
@MapNullToDefault(MapNullToDefaultStrategy.DEFAULT)
|
||||
@NullValueMapping(NullValueMappingStrategy.RETURN_DEFAULT)
|
||||
List<CarDto> carsToCarDtos(List<Car> cars);
|
||||
|
||||
|
||||
@MapNullToDefault(MapNullToDefaultStrategy.MAP_NULL_TO_NULL)
|
||||
@NullValueMapping(NullValueMappingStrategy.RETURN_NULL)
|
||||
Map<Integer, CarDto> carsToCarDtoMap(Map<Integer, Car> cars);
|
||||
}
|
||||
|
@ -18,14 +18,14 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.mapnulltodefault;
|
||||
|
||||
import org.mapstruct.MapNullToDefaultStrategy;
|
||||
import org.mapstruct.NullValueMappingStrategy;
|
||||
import org.mapstruct.MapperConfig;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@MapperConfig( mapNullToDefaultStrategy = MapNullToDefaultStrategy.MAP_NULL_TO_DEFAULT )
|
||||
@MapperConfig( nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT )
|
||||
public class CentralConfig {
|
||||
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ import org.mapstruct.CollectionMappingStrategy;
|
||||
import org.mapstruct.ap.prism.CollectionMappingStrategyPrism;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
import org.mapstruct.MapNullToDefaultStrategy;
|
||||
import org.mapstruct.ap.prism.MapNullToDefaultStrategyPrism;
|
||||
import org.mapstruct.NullValueMappingStrategy;
|
||||
import org.mapstruct.ap.prism.NullValueMappingStrategyPrism;
|
||||
|
||||
/**
|
||||
* Test for manually created prisms on enumeration types
|
||||
@ -43,8 +43,8 @@ public class EnumPrismsTest {
|
||||
|
||||
@Test
|
||||
public void mapNullToDefaultStrategyPrismIsCorrect() {
|
||||
assertThat( namesOf( MapNullToDefaultStrategy.values() ) ).isEqualTo(
|
||||
namesOf( MapNullToDefaultStrategyPrism.values() ) );
|
||||
assertThat( namesOf( NullValueMappingStrategy.values() ) ).isEqualTo(
|
||||
namesOf( NullValueMappingStrategyPrism.values() ) );
|
||||
}
|
||||
|
||||
private static List<String> namesOf(Enum<?>[] values) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user