From bca9548d173280f8adfd2b40896c361a5e15b5cd Mon Sep 17 00:00:00 2001 From: Idan Elhalwani Date: Sun, 4 Sep 2022 15:29:12 +0300 Subject: [PATCH] feat: add automap option to builder --- .../builder/MappingDefinitionBuilder.kt | 42 ++++++++++++++++++- .../builder/MappingDefinitionBuilderTests.kt | 20 +++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/shapeshift/src/main/kotlin/dev/krud/shapeshift/builder/MappingDefinitionBuilder.kt b/shapeshift/src/main/kotlin/dev/krud/shapeshift/builder/MappingDefinitionBuilder.kt index 55127ba..f2ba8a4 100644 --- a/shapeshift/src/main/kotlin/dev/krud/shapeshift/builder/MappingDefinitionBuilder.kt +++ b/shapeshift/src/main/kotlin/dev/krud/shapeshift/builder/MappingDefinitionBuilder.kt @@ -14,8 +14,10 @@ import dev.krud.shapeshift.MappingStrategy import dev.krud.shapeshift.condition.MappingCondition import dev.krud.shapeshift.dto.ResolvedMappedField import dev.krud.shapeshift.dto.TransformerCoordinates +import dev.krud.shapeshift.enums.AutoMappingStrategy import dev.krud.shapeshift.resolver.MappingDefinition import dev.krud.shapeshift.transformer.base.MappingTransformer +import dev.krud.shapeshift.util.getAutoMappings import dev.krud.shapeshift.util.getDeclaredFieldRecursive import java.lang.reflect.Field @@ -24,6 +26,22 @@ import java.lang.reflect.Field */ class MappingDefinitionBuilder(val fromClazz: Class, val toClazz: Class) { private val resolvedMappedFields = mutableListOf() + private var autoMappingStrategy: AutoMappingStrategy = AutoMappingStrategy.NONE + + /** + * Automatically map all fields with the given strategy + */ + fun autoMap(strategy: AutoMappingStrategy): MappingDefinitionBuilder { + this.autoMappingStrategy = strategy + return this + } + + /** + * Automatically map all fields with the given strategy with strategy [AutoMappingStrategy.BY_NAME_AND_TYPE] + */ + fun autoMap(): MappingDefinitionBuilder { + return autoMap(AutoMappingStrategy.BY_NAME_AND_TYPE) + } /** * Define a new mapped field, along with an optional condition, transformer and override mapping strategy @@ -40,10 +58,16 @@ class MappingDefinitionBuilder(val fromClazz: Class, val toClazz: Class * Return a mapping definition for the fields defined in this builder */ fun build(): MappingDefinition { + val autoMappedFields = getAutoMappings(fromClazz, toClazz, autoMappingStrategy) + .filter { autoResolvedMappedField -> + resolvedMappedFields.none { + it.mapFromCoordinates.first() == autoResolvedMappedField.mapFromCoordinates.first() || it.mapToCoordinates.first() == autoResolvedMappedField.mapToCoordinates.first() + } + } return MappingDefinition( fromClazz, toClazz, - resolvedMappedFields + resolvedMappedFields + autoMappedFields ) } @@ -54,6 +78,22 @@ class MappingDefinitionBuilder(val fromClazz: Class, val toClazz: Class private var transformerCoordinates: TransformerCoordinates = TransformerCoordinates.NONE private var mappingStrategy: MappingStrategy? = null + /** + * @see MappingDefinitionBuilder.autoMap + */ + fun autoMap(strategy: AutoMappingStrategy): MappingDefinitionBuilder { + buildAndAddSelf() + return this@MappingDefinitionBuilder.autoMap(strategy) + } + + /** + * @see MappingDefinitionBuilder.autoMap + */ + fun autoMap(): MappingDefinitionBuilder { + buildAndAddSelf() + return this@MappingDefinitionBuilder.autoMap() + } + /** * @see MappingDefinitionBuilder.mapField */ diff --git a/shapeshift/src/test/kotlin/dev/krud/shapeshift/builder/MappingDefinitionBuilderTests.kt b/shapeshift/src/test/kotlin/dev/krud/shapeshift/builder/MappingDefinitionBuilderTests.kt index 3143e4c..1fcbccb 100644 --- a/shapeshift/src/test/kotlin/dev/krud/shapeshift/builder/MappingDefinitionBuilderTests.kt +++ b/shapeshift/src/test/kotlin/dev/krud/shapeshift/builder/MappingDefinitionBuilderTests.kt @@ -131,6 +131,26 @@ class MappingDefinitionBuilderTests { expectThat(result.profession) .isNull() } + + @Test + internal fun `test auto mapping`() { + val shapeShift = ShapeShiftBuilder() + .excludeDefaultTransformers() + .withMapping( + MappingDefinitionBuilder(From::class.java, To::class.java) + .autoMap() + .build() + ) + .build() + val original = From(profession = "Test") + val result = shapeShift.map(original) + expectThat(result.name) + .isEqualTo(original.name) + expectThat(result.age) + .isEqualTo(original.age) + expectThat(result.profession) + .isEqualTo(original.profession) + } } class From(