Skip to content

Commit

Permalink
feat: add automap option to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Sep 4, 2022
1 parent 70ac909 commit bca9548
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,6 +26,22 @@ import java.lang.reflect.Field
*/
class MappingDefinitionBuilder(val fromClazz: Class<out Any>, val toClazz: Class<out Any>) {
private val resolvedMappedFields = mutableListOf<ResolvedMappedField>()
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
Expand All @@ -40,10 +58,16 @@ class MappingDefinitionBuilder(val fromClazz: Class<out Any>, 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
)
}

Expand All @@ -54,6 +78,22 @@ class MappingDefinitionBuilder(val fromClazz: Class<out Any>, 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<From, To>(original)
expectThat(result.name)
.isEqualTo(original.name)
expectThat(result.age)
.isEqualTo(original.age)
expectThat(result.profession)
.isEqualTo(original.profession)
}
}

class From(
Expand Down

0 comments on commit bca9548

Please sign in to comment.