Skip to content

Commit

Permalink
feat: add ShapeShiftBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed May 9, 2022
1 parent 2423f43 commit 73bb37f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 39 deletions.
46 changes: 26 additions & 20 deletions src/main/java/dev/krud/shapeshift/ShapeShift.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,21 @@ import dev.krud.shapeshift.util.setValue
import org.slf4j.LoggerFactory
import java.lang.reflect.Field

class ShapeShift(
transformers: List<TransformerRegistration<out Any, out Any>> = emptyList()
class ShapeShift constructor(
transformersRegistrations: List<TransformerRegistration<out Any, out Any>> = emptyList()
) {
internal val transformers: MutableList<TransformerRegistration<out Any, out Any>> = transformers.toMutableList()
internal val transformers: MutableList<TransformerRegistration<out Any, out Any>> = mutableListOf()
internal val transformersByNameCache: MutableMap<String, TransformerRegistration<out Any, out Any>> = mutableMapOf()
internal val transformersByTypeCache: MutableMap<Class<out FieldTransformer<*, *>>, TransformerRegistration<*, *>> =
mutableMapOf()
internal val defaultTransformers: MutableMap<ClassPair, TransformerRegistration<out Any, out Any>> = mutableMapOf()
private val mappingStructures: MutableMap<ClassPair, MappingStructure> = mutableMapOf()
private val entityFieldsCache: MutableMap<Class<*>, Map<String, Field>> = mutableMapOf()

fun <From : Any, To : Any> registerTransformer(registration: TransformerRegistration<From, To>) {
val name = registration.name ?: registration.transformer::class.simpleName!!
val newRegistration = registration.copy(name = name)
val existingTransformer = getTransformerByName(name)
if (existingTransformer != TransformerRegistration.EMPTY) {
error("Transformer with name $name already exists with type ${existingTransformer.transformer::class}")
}
if (newRegistration.default) {
val existingDefaultTransformer = defaultTransformers[newRegistration.transformer.id]
if (existingDefaultTransformer != null) {
error("Default transformer with pair ${newRegistration.transformer.id} already exists")
}
defaultTransformers[newRegistration.transformer.id] = newRegistration
init {
for (registration in transformersRegistrations) {
registerTransformer(registration)
}

transformers.add(newRegistration)
transformersByNameCache.remove(name)
transformersByTypeCache.remove(newRegistration.transformer::class.java)
}

fun <From : Any, To : Any> map(fromObject: From, toClazz: Class<To>): To {
Expand Down Expand Up @@ -290,6 +276,26 @@ class ShapeShift(
return transformerRegistration
}

private fun <From : Any, To : Any> registerTransformer(registration: TransformerRegistration<From, To>) {
val name = registration.name ?: registration.transformer::class.simpleName!!
val newRegistration = registration.copy(name = name)
val existingTransformer = getTransformerByName(name)
if (existingTransformer != TransformerRegistration.EMPTY) {
error("Transformer with name $name already exists with type ${existingTransformer.transformer::class}")
}
if (newRegistration.default) {
val existingDefaultTransformer = defaultTransformers[newRegistration.transformer.id]
if (existingDefaultTransformer != null) {
error("Default transformer with pair ${newRegistration.transformer.id} already exists")
}
defaultTransformers[newRegistration.transformer.id] = newRegistration
}

transformers.add(newRegistration)
transformersByNameCache.remove(name)
transformersByTypeCache.remove(newRegistration.transformer::class.java)
}

companion object {
private val log = LoggerFactory.getLogger(ShapeShift::class.java)
const val NODE_DELIMITER = "."
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/dev/krud/shapeshift/ShapeShiftBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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

class ShapeShiftBuilder {
private val transformers: MutableList<TransformerRegistration<*, *>> = mutableListOf()

fun withTransformer(transformerRegistration: TransformerRegistration<*, *>): ShapeShiftBuilder {
transformers += transformerRegistration
return this
}

fun build(): ShapeShift {
return ShapeShift(transformers)
}
}
58 changes: 39 additions & 19 deletions src/test/java/dev/krud/shapeshift/ShapeShiftTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ internal class ShapeShiftTests {

@Test
internal fun `simple mapping with transformer by type`() {
shapeShift.registerTransformer(
TransformerRegistration(
LongToStringTransformer()
shapeShift = ShapeShiftBuilder()
.withTransformer(
TransformerRegistration(
LongToStringTransformer()
)
)
)
.build()

val result = shapeShift.map(TypeTransformerFrom(), StringTo::class.java)

expectThat(result.long)
Expand All @@ -64,12 +67,14 @@ internal class ShapeShiftTests {

@Test
internal fun `simple mapping with transformer by name`() {
shapeShift.registerTransformer(
TransformerRegistration(
LongToStringTransformer(),
name = "myTransformer"
shapeShift = ShapeShiftBuilder()
.withTransformer(
TransformerRegistration(
LongToStringTransformer(),
name = "myTransformer"
)
)
)
.build()
val result = shapeShift.map(NameTransformerFrom(), StringTo::class.java)

expectThat(result.long)
Expand All @@ -78,12 +83,14 @@ internal class ShapeShiftTests {

@Test
internal fun `simple mapping with default transformer`() {
shapeShift.registerTransformer(
TransformerRegistration(
LongToStringTransformer(),
default = true
shapeShift = ShapeShiftBuilder()
.withTransformer(
TransformerRegistration(
LongToStringTransformer(),
default = true
)
)
)
.build()
val result = shapeShift.map(DefaultTransformerFrom(), StringTo::class.java)

expectThat(result.long)
Expand Down Expand Up @@ -225,9 +232,13 @@ internal class ShapeShiftTests {
true,
"second"
)
shapeShift.registerTransformer(firstRegistration)

val builder = ShapeShiftBuilder()
.withTransformer(firstRegistration)
.withTransformer(secondRegistration)

expectThrows<IllegalStateException> {
shapeShift.registerTransformer(secondRegistration)
builder.build()
}
}

Expand All @@ -238,7 +249,9 @@ internal class ShapeShiftTests {
false,
null
)
shapeShift.registerTransformer(registration)
shapeShift = ShapeShiftBuilder()
.withTransformer(registration)
.build()
expectThat(shapeShift.transformers.first().name)
.isEqualTo(
"ExampleFieldTransformer"
Expand All @@ -253,9 +266,12 @@ internal class ShapeShiftTests {
"first"
)

shapeShift.registerTransformer(registration)
val builder = ShapeShiftBuilder()
.withTransformer(registration)
.withTransformer(registration)

expectThrows<IllegalStateException> {
shapeShift.registerTransformer(registration)
builder.build()
}
}

Expand Down Expand Up @@ -330,6 +346,7 @@ internal class ShapeShiftTests {

class Grandchild {
val greatGrandchild: GreatGrandchild = GreatGrandchild()

class GreatGrandchild {
val long: Long = 1L
}
Expand Down Expand Up @@ -383,6 +400,7 @@ internal class ShapeShiftTests {
internal class FromWithMapFromSelfQualifier {
@MappedField(target = GenericTo::class, mapFrom = "child.long")
val child: Child = Child()

class Child {
val long: Long = 1L
}
Expand All @@ -391,6 +409,7 @@ internal class ShapeShiftTests {
@MappedField(target = GenericTo::class, mapFrom = "fromWithMapFromSelfQualifierOnType.child.long")
internal class FromWithMapFromSelfQualifierOnType {
val child: Child = Child()

class Child {
val long: Long = 1L
}
Expand Down Expand Up @@ -465,6 +484,7 @@ internal class ShapeShiftTests {

class Grandchild {
val greatGrandchild: GreatGrandchild? = null

class GreatGrandchild {
val long: Long? = null
}
Expand Down

0 comments on commit 73bb37f

Please sign in to comment.