diff --git a/shapeshift/src/main/kotlin/dev/krud/shapeshift/MappingTransformerRegistration.kt b/shapeshift/src/main/kotlin/dev/krud/shapeshift/MappingTransformerRegistration.kt index 7123686..41f4391 100644 --- a/shapeshift/src/main/kotlin/dev/krud/shapeshift/MappingTransformerRegistration.kt +++ b/shapeshift/src/main/kotlin/dev/krud/shapeshift/MappingTransformerRegistration.kt @@ -18,27 +18,24 @@ data class MappingTransformerRegistration( val fromClazz: Class, val toClazz: Class, val transformer: MappingTransformer, - val default: Boolean = false, - val name: String? = null + val default: Boolean = false ) { companion object { val EMPTY = MappingTransformerRegistration( Any::class.java, Any::class.java, EmptyTransformer, - false, - "empty" + false ) val MappingTransformerRegistration.id: ClassPair get() = ClassPair(fromClazz, toClazz) - inline fun MappingTransformer.toRegistration(default: Boolean = false, name: String? = null): MappingTransformerRegistration { + inline fun MappingTransformer.toRegistration(default: Boolean = false): MappingTransformerRegistration { return MappingTransformerRegistration( From::class.java, To::class.java, this, - default, - name + default ) } } diff --git a/shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShift.kt b/shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShift.kt index 9fe8676..9ab7a06 100644 --- a/shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShift.kt +++ b/shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShift.kt @@ -37,7 +37,6 @@ class ShapeShift internal constructor( val decoratorRegistrations: Set> ) { val transformerRegistrations: MutableList> = mutableListOf() - internal val transformersByNameCache: MutableMap> = concurrentMapOf() internal val transformersByTypeCache: MutableMap>, MappingTransformerRegistration> = concurrentMapOf() internal val defaultTransformers: MutableMap, MappingTransformerRegistration> = mutableMapOf() @@ -187,12 +186,6 @@ class ShapeShift internal constructor( return getFieldInstanceByNodes(nodes.drop(1), subTarget, type) } - private fun getTransformerByName(name: String): MappingTransformerRegistration { - return transformersByNameCache.computeIfAbsent(name) { _ -> - transformerRegistrations.find { it.name == name } ?: MappingTransformerRegistration.EMPTY - } - } - private fun getTransformerByType(type: Class>): MappingTransformerRegistration { return transformersByTypeCache.computeIfAbsent(type) { _ -> transformerRegistrations.find { it.transformer::class.java == type } ?: MappingTransformerRegistration.EMPTY @@ -230,16 +223,6 @@ class ShapeShift internal constructor( val transformationPair = fromPair to toPair log.trace("Attempting to find transformer for transformation pair [ $transformationPair ]") var transformerRegistration: MappingTransformerRegistration<*, *> = MappingTransformerRegistration.EMPTY - log.trace("Checking transformerRef field") - if (!coordinates.name.isNullOrBlank()) { - log.trace("transformerRef is not empty with value [ " + coordinates.name + " ]") - transformerRegistration = getTransformerByName(coordinates.name) - if (transformerRegistration != MappingTransformerRegistration.EMPTY) { - log.trace("Found transformer by ref [ ${coordinates.name} ] of type [ " + transformerRegistration.transformer.javaClass.name + " ]") - } else { - error("Could not find transformer by ref [ ${coordinates.name} ] on $fromPair") - } - } if (transformerRegistration == MappingTransformerRegistration.EMPTY) { log.trace("Checking transformer field") if (coordinates.type == null) { @@ -270,23 +253,16 @@ class ShapeShift internal constructor( } private fun registerTransformer(registration: MappingTransformerRegistration) { - val name = registration.name ?: registration.transformer::class.simpleName!! - val newRegistration = registration.copy(name = name) - val existingTransformer = getTransformerByName(name) - if (existingTransformer != MappingTransformerRegistration.EMPTY) { - error("Transformer with name $name already exists with type ${existingTransformer.transformer::class}") - } - if (newRegistration.default) { - val existingDefaultTransformer = defaultTransformers[newRegistration.id] + if (registration.default) { + val existingDefaultTransformer = defaultTransformers[registration.id] if (existingDefaultTransformer != null) { - error("Default transformer with pair ${newRegistration.id} already exists") + error("Default transformer with pair ${registration.id} already exists") } - defaultTransformers[newRegistration.id] = newRegistration + defaultTransformers[registration.id] = registration } - transformerRegistrations.add(newRegistration) - transformersByNameCache.remove(name) - transformersByTypeCache.remove(newRegistration.transformer::class.java) + transformerRegistrations.add(registration) + transformersByTypeCache.remove(registration.transformer::class.java) } companion object { diff --git a/shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShiftBuilder.kt b/shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShiftBuilder.kt index e74ba62..b34b249 100644 --- a/shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShiftBuilder.kt +++ b/shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShiftBuilder.kt @@ -84,8 +84,8 @@ class ShapeShiftBuilder { /** * Add a transformer to the ShapeShift instance */ - inline fun withTransformer(mappingTransformer: MappingTransformer, default: Boolean = false, name: String? = null): ShapeShiftBuilder { - return withTransformer(mappingTransformer.toRegistration(default, name)) + inline fun withTransformer(mappingTransformer: MappingTransformer, default: Boolean = false): ShapeShiftBuilder { + return withTransformer(mappingTransformer.toRegistration(default)) } /** diff --git a/shapeshift/src/main/kotlin/dev/krud/shapeshift/dto/TransformerCoordinates.kt b/shapeshift/src/main/kotlin/dev/krud/shapeshift/dto/TransformerCoordinates.kt index d8ecddf..8373400 100644 --- a/shapeshift/src/main/kotlin/dev/krud/shapeshift/dto/TransformerCoordinates.kt +++ b/shapeshift/src/main/kotlin/dev/krud/shapeshift/dto/TransformerCoordinates.kt @@ -14,12 +14,10 @@ import dev.krud.shapeshift.transformer.EmptyTransformer import dev.krud.shapeshift.transformer.base.MappingTransformer data class TransformerCoordinates( - val name: String? = null, val type: Class>? = null ) { companion object { val NONE = TransformerCoordinates() - fun ofName(name: String) = TransformerCoordinates(name) fun ofType( type: Class> ): TransformerCoordinates { diff --git a/shapeshift/src/main/kotlin/dev/krud/shapeshift/resolver/annotation/AnnotationMappingDefinitionResolver.kt b/shapeshift/src/main/kotlin/dev/krud/shapeshift/resolver/annotation/AnnotationMappingDefinitionResolver.kt index 2b4836f..663983c 100644 --- a/shapeshift/src/main/kotlin/dev/krud/shapeshift/resolver/annotation/AnnotationMappingDefinitionResolver.kt +++ b/shapeshift/src/main/kotlin/dev/krud/shapeshift/resolver/annotation/AnnotationMappingDefinitionResolver.kt @@ -26,11 +26,7 @@ class AnnotationMappingDefinitionResolver : MappingDefinitionResolver { val resolvedMappedFields = mutableListOf() for ((mappedField, field) in mappedFieldReferences) { - val transformerCoordinates = if (mappedField.transformerRef.isBlank()) { - TransformerCoordinates.ofType(mappedField.transformer.java) - } else { - TransformerCoordinates.ofName(mappedField.transformerRef) - } + val transformerCoordinates = TransformerCoordinates.ofType(mappedField.transformer.java) val mapFromCoordinates = resolveNodesToFields(mappedField.mapFrom.splitIgnoreEmpty(NODE_DELIMITER), field, fromClazz) diff --git a/shapeshift/src/main/kotlin/dev/krud/shapeshift/resolver/annotation/MappedField.kt b/shapeshift/src/main/kotlin/dev/krud/shapeshift/resolver/annotation/MappedField.kt index 9e0a710..92eb86e 100644 --- a/shapeshift/src/main/kotlin/dev/krud/shapeshift/resolver/annotation/MappedField.kt +++ b/shapeshift/src/main/kotlin/dev/krud/shapeshift/resolver/annotation/MappedField.kt @@ -40,11 +40,6 @@ annotation class MappedField( * The [MappingTransformer] to use on the value */ val transformer: KClass> = EmptyTransformer::class, - /** - * Bean name for a defined [MappingTransformer] bean to use on the value - * Supersedes [MappedField.transformer] if specified - */ - val transformerRef: String = "", /** * The condition to use to determine if the field should be mapped diff --git a/shapeshift/src/test/kotlin/dev/krud/shapeshift/ShapeShiftTests.kt b/shapeshift/src/test/kotlin/dev/krud/shapeshift/ShapeShiftTests.kt index 1f39f07..2a85741 100644 --- a/shapeshift/src/test/kotlin/dev/krud/shapeshift/ShapeShiftTests.kt +++ b/shapeshift/src/test/kotlin/dev/krud/shapeshift/ShapeShiftTests.kt @@ -60,19 +60,6 @@ internal class ShapeShiftTests { .isEqualTo("1") } - @Test - internal fun `simple mapping with transformer by name`() { - shapeShift = ShapeShiftBuilder() - .withTransformer( - LongToStringTransformer().toRegistration(name = "myTransformer") - ) - .build() - val result = shapeShift.map(NameTransformerFrom(), StringTo::class.java) - - expectThat(result.long) - .isEqualTo("1") - } - @Test internal fun `simple mapping with default transformer`() { shapeShift = ShapeShiftBuilder() @@ -241,55 +228,6 @@ internal class ShapeShiftTests { } } - @Test - internal fun `registering transformer with null name should use simple class name when registering`() { - val registration = ExampleFieldTransformer().toRegistration() - shapeShift = ShapeShiftBuilder() - .excludeDefaultTransformers() - .withTransformer(registration) - .build() - expectThat(shapeShift.transformerRegistrations.first().name) - .isEqualTo( - "ExampleFieldTransformer" - ) - } - - @Test - internal fun `registering same transformer twice should result in 1 transformer`() { - val registration = ExampleFieldTransformer().toRegistration(name = "first") - - val builder = ShapeShiftBuilder() - .excludeDefaultTransformers() - .withTransformer(registration) - .withTransformer(registration) - - val shapeShift = builder.build() - expectThat(shapeShift.transformerRegistrations.size) - .isEqualTo(1) - } - - @Test - internal fun `registering transformer with an existing name twice should throw exception`() { - val registration = ExampleFieldTransformer().toRegistration(name = "first") - - val secondRegistration = ExampleFieldTransformer().toRegistration(name = "first") - - val builder = ShapeShiftBuilder() - .withTransformer(registration) - .withTransformer(secondRegistration) - - expectThrows { - builder.build() - } - } - - @Test - internal fun `using unregistered transformer by name should throw exception`() { - expectThrows { - shapeShift.map(NameTransformerFrom(), StringTo::class.java) - } - } - @Test internal fun `using unregistered transformer by type should throw exception`() { expectThrows { diff --git a/shapeshift/src/test/kotlin/dev/krud/shapeshift/_fixtures.kt b/shapeshift/src/test/kotlin/dev/krud/shapeshift/_fixtures.kt index afd0c0e..d355808 100644 --- a/shapeshift/src/test/kotlin/dev/krud/shapeshift/_fixtures.kt +++ b/shapeshift/src/test/kotlin/dev/krud/shapeshift/_fixtures.kt @@ -177,11 +177,6 @@ internal class DefaultTransformerFrom { val long: Long = 1L } -internal class NameTransformerFrom { - @MappedField(target = StringTo::class, transformerRef = "myTransformer") - val long: Long = 1L -} - internal class TypeTransformerFrom { @MappedField(target = StringTo::class, transformer = LongToStringTransformer::class) val long: Long = 1L diff --git a/spring-boot-starter-shapeshift/src/main/kotlin/dev/krud/shapeshift/spring/customizer/ShapeShiftTransformerCustomizer.kt b/spring-boot-starter-shapeshift/src/main/kotlin/dev/krud/shapeshift/spring/customizer/ShapeShiftTransformerCustomizer.kt index 3e6495f..4d76e98 100644 --- a/spring-boot-starter-shapeshift/src/main/kotlin/dev/krud/shapeshift/spring/customizer/ShapeShiftTransformerCustomizer.kt +++ b/spring-boot-starter-shapeshift/src/main/kotlin/dev/krud/shapeshift/spring/customizer/ShapeShiftTransformerCustomizer.kt @@ -21,17 +21,16 @@ import org.springframework.core.GenericTypeResolver @Configuration class ShapeShiftTransformerCustomizer : ShapeShiftBuilderCustomizer { @Autowired(required = false) - private val mappingTransformers: Map>? = null + private val mappingTransformers: List>? = null override fun customize(builder: ShapeShiftBuilder) { - mappingTransformers?.forEach { (name, mappingTransformer) -> + mappingTransformers?.forEach { mappingTransformer -> val types = GenericTypeResolver.resolveTypeArguments(mappingTransformer::class.java, MappingTransformer::class.java) val registration = MappingTransformerRegistration( - types[0] as Class, + types!![0] as Class, types[1] as Class, mappingTransformer as MappingTransformer, - false, - name + false ) builder.withTransformer( registration