Skip to content

Commit

Permalink
feat: add transformer/condition class reference overloads to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Sep 4, 2022
1 parent 6706fcc commit 70ac909
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class MappingDefinitionBuilder(val fromClazz: Class<out Any>, val toClazz: Class

inner class MapFieldBuilder(val from: String, val to: String) {
private var condition: MappingCondition<out Any>? = null
private var conditionClazz: Class<out MappingCondition<out Any>>? = null
private var transformer: MappingTransformer<out Any, out Any>? = null
private var transformerCoordinates: TransformerCoordinates = TransformerCoordinates.NONE
private var mappingStrategy: MappingStrategy? = null

/**
Expand All @@ -69,6 +71,15 @@ class MappingDefinitionBuilder(val fromClazz: Class<out Any>, val toClazz: Class
return this
}

/**
* Specify a condition reference to use for this mapped field
* Java Example: `withCondition(Above18Condition.class)`
*/
fun withCondition(conditionClazz: Class<out MappingCondition<out Any>>): MapFieldBuilder {
this.conditionClazz = conditionClazz
return this
}

/**
* Specify a transformer to use for this mapped field
* Java Example: `withTransformer(ctx -> (Integer) ctx.originalValue.toString())`
Expand All @@ -78,6 +89,15 @@ class MappingDefinitionBuilder(val fromClazz: Class<out Any>, val toClazz: Class
return this
}

/**
* Specify a transformer reference to use for this mapped field
* Java Example: `withTransformer(IntegerToStringTransoformer.class)`
*/
fun withTransformer(transformerClazz: Class<out MappingTransformer<out Any, out Any>>): MapFieldBuilder {
this.transformerCoordinates = TransformerCoordinates.ofType(transformerClazz)
return this
}

/**
* Specify an override mapping strategy for this mapped field
*/
Expand All @@ -98,9 +118,9 @@ class MappingDefinitionBuilder(val fromClazz: Class<out Any>, val toClazz: Class
val resolvedMappedField = ResolvedMappedField(
resolveNodes(from.split("."), fromClazz),
resolveNodes(to.split("."), toClazz),
TransformerCoordinates.NONE,
transformerCoordinates,
transformer,
null,
conditionClazz,
condition,
mappingStrategy
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ package dev.krud.shapeshift.builder

import dev.krud.shapeshift.MappingStrategy
import dev.krud.shapeshift.ShapeShiftBuilder
import dev.krud.shapeshift.condition.MappingCondition
import dev.krud.shapeshift.condition.MappingConditionContext
import dev.krud.shapeshift.transformer.base.MappingTransformer
import dev.krud.shapeshift.transformer.base.MappingTransformerContext
import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.assertions.isEqualTo
Expand Down Expand Up @@ -59,6 +63,25 @@ class MappingDefinitionBuilderTests {
.isNull()
}

@Test
internal fun `test mapping with condition reference`() {
val shapeShift = ShapeShiftBuilder()
.excludeDefaultTransformers()
.withMapping(
MappingDefinitionBuilder(From::class.java, To::class.java)
.mapField("name", "name")
.mapField("age", "age").withCondition(Above31Condition::class.java)
.build()
)
.build()
val original = From()
val result = shapeShift.map<From, To>(original)
expectThat(result.name)
.isEqualTo(original.name)
expectThat(result.age)
.isNull()
}

@Test
internal fun `test mapping with transformer`() {
val shapeShift = ShapeShiftBuilder()
Expand All @@ -75,6 +98,23 @@ class MappingDefinitionBuilderTests {
.isEqualTo(original.age + 1)
}

@Test
internal fun `test mapping with transformer reference`() {
val shapeShift = ShapeShiftBuilder()
.excludeDefaultTransformers()
.withTransformer(AddOneTransformer())
.withMapping(
MappingDefinitionBuilder(From::class.java, To::class.java)
.mapField("age", "age").withTransformer(AddOneTransformer::class.java)
.build()
)
.build()
val original = From()
val result = shapeShift.map<From, To>(original)
expectThat(result.age)
.isEqualTo(original.age + 1)
}

@Test
internal fun `test mapping with override mapping strategy`() {
val shapeShift = ShapeShiftBuilder()
Expand Down Expand Up @@ -109,4 +149,16 @@ class To(
var age: Int? = null,
var city: String? = null,
val profession: String? = "placeholder"
)
)

class AddOneTransformer : MappingTransformer<Int, Int> {
override fun transform(context: MappingTransformerContext<out Int>): Int {
return (context.originalValue as Int) + 1
}
}

class Above31Condition : MappingCondition<Int> {
override fun isValid(context: MappingConditionContext<Int>): Boolean {
return context.originalValue != null && context.originalValue as Int > 31
}
}

0 comments on commit 70ac909

Please sign in to comment.