Skip to content

Commit

Permalink
feat: add MappingDefinitionBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Sep 1, 2022
1 parent 46911ce commit 291fb44
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright KRUD 2022
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package dev.krud.shapeshift.builder

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.resolver.MappingDefinition
import dev.krud.shapeshift.transformer.base.MappingTransformer
import dev.krud.shapeshift.util.getDeclaredFieldRecursive
import java.lang.reflect.Field

/**
* A builder of mapping definitions for non-Kotlin use
*/
class MappingDefinitionBuilder(val fromClazz: Class<out Any>, val toClazz: Class<out Any>) {
private val resolvedMappedFields = mutableListOf<ResolvedMappedField>()

/**
* Define a new mapped field, along with an optional condition, transformer and override mapping strategy
* @see MapFieldBuilder
* @param from The field to map from
* @param to The field to map to
*/
fun mapField(from: String, to: String): MapFieldBuilder {
val mapFieldBuilder = MapFieldBuilder(from, to)
return mapFieldBuilder
}

/**
* Return a mapping definition for the fields defined in this builder
*/
fun build(): MappingDefinition {
return MappingDefinition(
fromClazz,
toClazz,
resolvedMappedFields
)
}

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

/**
* @see MappingDefinitionBuilder.mapField
*/
fun mapField(from: String, to: String): MapFieldBuilder {
buildAndAddSelf()
return this@MappingDefinitionBuilder.mapField(from, to)
}

/**
* Specify a condition to use for this mapped field
* Java Example: `withCondition(ctx -> (Integer) ctx.originalValue > 18)`
*/
fun withCondition(condition: MappingCondition<out Any>): MapFieldBuilder {
this.condition = condition
return this
}

/**
* Specify a transformer to use for this mapped field
* Java Example: `withTransformer(ctx -> (Integer) ctx.originalValue.toString())`
*/
fun withTransformer(transformer: MappingTransformer<out Any, out Any>): MapFieldBuilder {
this.transformer = transformer
return this
}

/**
* Specify an override mapping strategy for this mapped field
*/
fun withMappingStrategy(mappingStrategy: MappingStrategy): MapFieldBuilder {
this.mappingStrategy = mappingStrategy
return this
}

/**
* @see MappingDefinitionBuilder.build
*/
fun build(): MappingDefinition {
buildAndAddSelf()
return this@MappingDefinitionBuilder.build()
}

private fun buildAndAddSelf() {
val resolvedMappedField = ResolvedMappedField(
resolveNodes(from.split("."), fromClazz),
resolveNodes(to.split("."), toClazz),
TransformerCoordinates.NONE,
transformer,
null,
condition,
mappingStrategy
)
this@MappingDefinitionBuilder.resolvedMappedFields.add(resolvedMappedField)
}
}

private fun resolveNodes(nodes: List<String>, clazz: Class<*>): List<Field> {
if (nodes.isEmpty()) {
return emptyList()
}
val realField = clazz.getDeclaredFieldRecursive(nodes.first())
return listOf(realField) + resolveNodes(nodes.drop(1), realField.type)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright KRUD 2022
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package dev.krud.shapeshift.builder

import dev.krud.shapeshift.MappingStrategy
import dev.krud.shapeshift.ShapeShiftBuilder
import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.assertions.isEqualTo
import strikt.assertions.isNull

class MappingDefinitionBuilderTests {
@Test
internal fun `test simple happy flow`() {
val shapeShift = ShapeShiftBuilder()
.excludeDefaultTransformers()
.withMapping(
MappingDefinitionBuilder(From::class.java, To::class.java)
.mapField("name", "name")
.mapField("age", "age")
.mapField("address.city", "city")
.build()
)
.build()
val original = From()
val result = shapeShift.map<From, To>(original)
expectThat(result.name)
.isEqualTo(original.name)
expectThat(result.age)
.isEqualTo(original.age)
expectThat(result.city)
.isEqualTo(original.address.city)
}

@Test
internal fun `test mapping with condition`() {
val shapeShift = ShapeShiftBuilder()
.excludeDefaultTransformers()
.withMapping(
MappingDefinitionBuilder(From::class.java, To::class.java)
.mapField("name", "name")
.mapField("age", "age").withCondition { ctx -> ctx.originalValue as Int > 31 }
.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()
.excludeDefaultTransformers()
.withMapping(
MappingDefinitionBuilder(From::class.java, To::class.java)
.mapField("age", "age").withTransformer { ctx -> ctx.originalValue as Int + 1 }
.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()
.excludeDefaultTransformers()
.withDefaultMappingStrategy(MappingStrategy.MAP_NOT_NULL)
.withMapping(
MappingDefinitionBuilder(From::class.java, To::class.java)
.mapField("profession", "profession").withMappingStrategy(MappingStrategy.MAP_ALL)
.build()
)
.build()
val original = From()
val result = shapeShift.map<From, To>(original)
expectThat(result.profession)
.isNull()
}
}

class From(
val name: String = "John",
val age: Int = 30,
val address: Address = Address(),
val profession: String? = null
) {
class Address {
val city = "London"
}
}

class To(
var name: String? = null,
var age: Int? = null,
var city: String? = null,
val profession: String? = "placeholder"
)

0 comments on commit 291fb44

Please sign in to comment.