Skip to content

Commit

Permalink
feat(spring): add support for bean decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Jul 10, 2022
1 parent 43572f9 commit 2cbcdd4
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 22 deletions.
Expand Up @@ -34,7 +34,7 @@ class ShapeShift internal constructor(
val defaultMappingStrategy: MappingStrategy,
val decorators: Set<MappingDecorator<out Any, out Any>>
) {
internal val transformers: MutableList<TransformerRegistration<out Any, out Any>> = mutableListOf()
val transformers: MutableList<TransformerRegistration<out Any, out Any>> = mutableListOf()
internal val transformersByNameCache: MutableMap<String, TransformerRegistration<out Any, out Any>> = concurrentMapOf()
internal val transformersByTypeCache: MutableMap<Class<out FieldTransformer<*, *>>, TransformerRegistration<*, *>> =
concurrentMapOf()
Expand Down
Expand Up @@ -12,8 +12,8 @@ package dev.krud.shapeshift.resolver.annotation

import dev.krud.shapeshift.dto.ResolvedMappedField
import dev.krud.shapeshift.dto.TransformerCoordinates
import dev.krud.shapeshift.resolver.MappingDefinitionResolver
import dev.krud.shapeshift.resolver.MappingDefinition
import dev.krud.shapeshift.resolver.MappingDefinitionResolver
import dev.krud.shapeshift.util.getDeclaredFieldRecursive
import dev.krud.shapeshift.util.splitIgnoreEmpty
import java.lang.reflect.Field
Expand Down Expand Up @@ -56,7 +56,7 @@ class AnnotationMappingDefinitionResolver : MappingDefinitionResolver {
mappedField.overrideMappingStrategy
)
}
return MappingDefinition(sourceClazz, targetClazz, resolvedMappedFields, emptyList())
return MappingDefinition(sourceClazz, targetClazz, resolvedMappedFields)
}

/**
Expand Down
22 changes: 22 additions & 0 deletions spring-boot-starter-shapeshift/pom.xml
Expand Up @@ -46,4 +46,26 @@
<artifactId>shapeshift</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
</plugin>
</plugins>
</build>
</project>
Expand Up @@ -12,8 +12,8 @@ package dev.krud.shapeshift.spring

import dev.krud.shapeshift.ShapeShift
import dev.krud.shapeshift.ShapeShiftBuilder
import dev.krud.shapeshift.TransformerRegistration
import dev.krud.shapeshift.transformer.base.FieldTransformer
import dev.krud.shapeshift.spring.customizer.ShapeShiftDecoratorCustomizer
import dev.krud.shapeshift.spring.customizer.ShapeShiftTransformerCustomizer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
Expand All @@ -22,23 +22,20 @@ import org.springframework.context.annotation.Configuration

@Configuration
@ConditionalOnClass(ShapeShift::class)
open class ShapeShiftAutoConfiguration {
class ShapeShiftAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ShapeShift::class)
open fun shapeShift(@Autowired(required = false) fieldTransformers: Map<String, FieldTransformer<*, *>>?, @Autowired(required = false) customizers: List<ShapeShiftBuilderCustomizer>?): ShapeShift {
fun shapeShift(@Autowired(required = false) customizers: List<ShapeShiftBuilderCustomizer>?): ShapeShift {
val builder = ShapeShiftBuilder()
fieldTransformers?.forEach { (name, fieldTransformer) ->
builder.withTransformer(
TransformerRegistration(
fieldTransformer,
false,
name
)
)
}
customizers?.forEach { customizer ->
customizer.customize(builder)
}
return builder.build()
}

@Bean
fun shapeShiftTransformerCustomizer() = ShapeShiftTransformerCustomizer()

@Bean
fun shapeShiftDecoratorCustomizer() = ShapeShiftDecoratorCustomizer()
}
@@ -0,0 +1,29 @@
/*
* 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.spring.customizer

import dev.krud.shapeshift.ShapeShiftBuilder
import dev.krud.shapeshift.decorator.MappingDecorator
import dev.krud.shapeshift.spring.ShapeShiftBuilderCustomizer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration

@Configuration
class ShapeShiftDecoratorCustomizer : ShapeShiftBuilderCustomizer {
@Autowired(required = false)
private val mappingDecorators: List<MappingDecorator<out Any, out Any>>? = null

override fun customize(builder: ShapeShiftBuilder) {
mappingDecorators?.forEach { mappingDecorator ->
builder.withDecorator(mappingDecorator)
}
}
}
@@ -0,0 +1,36 @@
/*
* 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.spring.customizer

import dev.krud.shapeshift.ShapeShiftBuilder
import dev.krud.shapeshift.TransformerRegistration
import dev.krud.shapeshift.spring.ShapeShiftBuilderCustomizer
import dev.krud.shapeshift.transformer.base.FieldTransformer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration

@Configuration
class ShapeShiftTransformerCustomizer : ShapeShiftBuilderCustomizer {
@Autowired(required = false)
private val fieldTransformers: Map<String, FieldTransformer<out Any, out Any>>? = null

override fun customize(builder: ShapeShiftBuilder) {
fieldTransformers?.forEach { (name, fieldTransformer) ->
builder.withTransformer(
TransformerRegistration(
fieldTransformer,
false,
name
)
)
}
}
}
Expand Up @@ -16,14 +16,35 @@ import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit.jupiter.SpringExtension
import strikt.api.expectThat
import strikt.assertions.contains

@ExtendWith(SpringExtension::class)
@ContextConfiguration(classes = [ShapeShiftAutoConfiguration::class, TestConfiguration::class])
internal class ShapeShiftSpringTest {
@Autowired
private lateinit var shapeShift: ShapeShift

@Autowired
private lateinit var exampleDecorator: ExampleDecorator

@Autowired
private lateinit var exampleTransformer: ExampleTransformer

@Test
internal fun `context loads`() {
println()
}

@Test
internal fun `bean transformer is loaded`() {
expectThat(shapeShift.transformers.map { it.transformer })
.contains(exampleTransformer)
}

@Test
internal fun `bean decorator is loaded`() {
expectThat(shapeShift.decorators)
.contains(exampleDecorator)
}
}
Expand Up @@ -10,16 +10,16 @@

package dev.krud.shapeshift.spring

import dev.krud.shapeshift.transformer.LongToDateTransformer
import dev.krud.shapeshift.transformer.base.FieldTransformer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.util.*

@Configuration
open class TestConfiguration {
class TestConfiguration {
@Bean
open fun longToDateTransformer(): FieldTransformer<Long, Date> {
return LongToDateTransformer()
fun exampleTransformer(): ExampleTransformer {
return ExampleTransformer()
}

@Bean
fun exampleDecorator() = ExampleDecorator()
}
@@ -0,0 +1,33 @@
/*
* 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.spring

import dev.krud.shapeshift.decorator.MappingDecorator
import dev.krud.shapeshift.transformer.base.FieldTransformer
import java.lang.reflect.Field

class ExampleClass

class SecondExampleClass

class ExampleDecorator : MappingDecorator<ExampleClass, SecondExampleClass> {
override fun decorate(from: ExampleClass, to: SecondExampleClass) {
}
}

class ExampleTransformer : FieldTransformer<String, String> {
override val fromType: Class<String> = String::class.java
override val toType: Class<String> = String::class.java

override fun transform(fromField: Field, toField: Field, originalValue: String?, fromObject: Any, toObject: Any): String? {
return originalValue
}
}

0 comments on commit 2cbcdd4

Please sign in to comment.