Skip to content

Commit

Permalink
feat: change transformers list to set
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed May 21, 2022
1 parent 9f1483d commit c5841b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShift.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory
import java.lang.reflect.Field

class ShapeShift constructor(
transformersRegistrations: List<TransformerRegistration<out Any, out Any>> = emptyList()
transformersRegistrations: Set<TransformerRegistration<out Any, out Any>> = emptySet()
) {
internal val transformers: MutableList<TransformerRegistration<out Any, out Any>> = mutableListOf()
internal val transformersByNameCache: MutableMap<String, TransformerRegistration<out Any, out Any>> = mutableMapOf()
Expand All @@ -49,7 +49,7 @@ class ShapeShift constructor(
return map(fromObject, toObject)
}

fun <To : Any> map(fromObject: Any, toObject: To): To {
private fun <To : Any> map(fromObject: Any, toObject: To): To {
val toClazz = toObject::class.java
val mappingStructure = getMappingStructure(fromObject::class.java, toClazz)
for (typeAnnotation in mappingStructure.typeAnnotations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package dev.krud.shapeshift
import dev.krud.shapeshift.transformer.base.FieldTransformer

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

fun withTransformer(fieldTransformer: FieldTransformer<*, *>, default: Boolean = false, name: String? = null): ShapeShiftBuilder {
transformers.add(TransformerRegistration(fieldTransformer, default, name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package dev.krud.shapeshift

import dev.krud.shapeshift.annotation.DefaultMappingTarget
import dev.krud.shapeshift.annotation.MappedField
import dev.krud.shapeshift.transformer.DateToLongTransformer
import dev.krud.shapeshift.transformer.base.FieldTransformer
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
Expand Down Expand Up @@ -259,7 +260,7 @@ internal class ShapeShiftTests {
}

@Test
internal fun `registering same transformer twice should throw exception`() {
internal fun `registering same transformer twice should result in 1 transformer`() {
val registration = TransformerRegistration(
ExampleFieldTransformer(),
false,
Expand All @@ -270,6 +271,29 @@ internal class ShapeShiftTests {
.withTransformer(registration)
.withTransformer(registration)

val shapeShift = builder.build()
expectThat(shapeShift.transformers.size)
.isEqualTo(1)
}

@Test
internal fun `registering transformer with an existing name twice should throw exception`() {
val registration = TransformerRegistration(
ExampleFieldTransformer(),
false,
"first"
)

val secondRegistration = TransformerRegistration(
DateToLongTransformer(),
false,
"first"
)

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

expectThrows<IllegalStateException> {
builder.build()
}
Expand Down

0 comments on commit c5841b6

Please sign in to comment.