Skip to content

Commit

Permalink
fix: rename FieldMapper to ShapeShift
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed May 4, 2022
1 parent dae7c41 commit 58d725c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import dev.krud.shapeshift.util.setValue
import org.slf4j.LoggerFactory
import java.lang.reflect.Field

class FieldMapper(
class ShapeShift(
transformers: List<TransformerRegistration<out Any, out Any>> = emptyList()
) {
internal val transformers: MutableList<TransformerRegistration<out Any, out Any>> = transformers.toMutableList()
Expand All @@ -41,7 +41,6 @@ class FieldMapper(
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) {
Expand Down Expand Up @@ -285,7 +284,7 @@ class FieldMapper(
}

companion object {
private val log = LoggerFactory.getLogger(FieldMapper::class.java)
private val log = LoggerFactory.getLogger(ShapeShift::class.java)
const val NODE_DELIMITER = "."
val NODE_DELIMITER_REGEX = Regex("\\.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ import strikt.assertions.isEqualTo
import strikt.assertions.message
import java.lang.reflect.Field

internal class FieldMapperKtTests {
internal lateinit var mapper: FieldMapper
internal class ShapeShiftTests {
internal lateinit var shapeShift: ShapeShift

@BeforeEach
internal fun setUp() {
mapper = FieldMapper()
shapeShift = ShapeShift()
}

@Nested
inner class Scenarios {
@Test
internal fun `multiple mapped fields on field`() {
val result = mapper.map(FromWithMultipleMappedFields(), MultipleFieldTo::class.java)
val result = shapeShift.map(FromWithMultipleMappedFields(), MultipleFieldTo::class.java)
expectThat(result.long)
.isEqualTo(1L)
expectThat(result.secondLong)
Expand All @@ -43,120 +43,120 @@ internal class FieldMapperKtTests {

@Test
internal fun `simple mapping without transformer`() {
val result = mapper.map(TransformerlessFrom(), GenericTo::class.java)
val result = shapeShift.map(TransformerlessFrom(), GenericTo::class.java)

expectThat(result.long)
.isEqualTo(1L)
}

@Test
internal fun `simple mapping with transformer by type`() {
mapper.registerTransformer(
shapeShift.registerTransformer(
TransformerRegistration(
LongToStringTransformer()
)
)
val result = mapper.map(TypeTransformerFrom(), StringTo::class.java)
val result = shapeShift.map(TypeTransformerFrom(), StringTo::class.java)

expectThat(result.long)
.isEqualTo("1")
}

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

expectThat(result.long)
.isEqualTo("1")
}

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

expectThat(result.long)
.isEqualTo("1")
}

@Test
internal fun `simple mapping on type level`() {
val result = mapper.map(TransformerlessTypeLevelFrom(), GenericTo::class.java)
val result = shapeShift.map(TransformerlessTypeLevelFrom(), GenericTo::class.java)

expectThat(result.long)
.isEqualTo(1L)
}

@Test
internal fun `simple mapping with default target mapping`() {
val result = mapper.map(FromWithDefaultMappingTarget(), GenericTo::class.java)
val result = shapeShift.map(FromWithDefaultMappingTarget(), GenericTo::class.java)

expectThat(result.long)
.isEqualTo(1L)
}

@Test
internal fun `complex path mapping on mapFrom`() {
val result = mapper.map(FromWithComplexPath(), GenericTo::class.java)
val result = shapeShift.map(FromWithComplexPath(), GenericTo::class.java)

expectThat(result.long)
.isEqualTo(1L)
}

@Test
internal fun `complex path mapping on mapTo`() {
val result = mapper.map(FromToComplexPath(), ToWithComplexPath::class.java)
val result = shapeShift.map(FromToComplexPath(), ToWithComplexPath::class.java)

expectThat(result.child.grandchild?.greatGrandchild?.long)
.isEqualTo(1L)
}

@Test
internal fun `mapFrom with self field qualifier`() {
val result = mapper.map(FromWithMapFromSelfQualifier(), GenericTo::class.java)
val result = shapeShift.map(FromWithMapFromSelfQualifier(), GenericTo::class.java)

expectThat(result.long)
.isEqualTo(1L)
}

@Test
internal fun `mapFrom with self field qualifier on type level`() {
val result = mapper.map(FromWithMapFromSelfQualifierOnType(), GenericTo::class.java)
val result = shapeShift.map(FromWithMapFromSelfQualifierOnType(), GenericTo::class.java)

expectThat(result.long)
.isEqualTo(1L)
}

@Test
internal fun `mapTo with self field qualifier`() {
val result = mapper.map(FromWithMapToSelfQualifier(), ToWithShallowPath::class.java)
val result = shapeShift.map(FromWithMapToSelfQualifier(), ToWithShallowPath::class.java)

expectThat(result.child?.long)
.isEqualTo(1L)
}

@Test
internal fun `mapTo with self field qualifier on type level`() {
val result = mapper.map(FromWithMapToSelfQualifierOnType(), ToWithShallowPath::class.java)
val result = shapeShift.map(FromWithMapToSelfQualifierOnType(), ToWithShallowPath::class.java)

expectThat(result.child?.long)
.isEqualTo(1L)
}

@Test
internal fun `nested class mapping`() {
val result = mapper.map(
val result = shapeShift.map(
FromWithBase(),
ToWithBase::class.java
)
Expand All @@ -170,14 +170,14 @@ internal class FieldMapperKtTests {

@Test
internal fun `mapping null object`() {
val result = mapper.map(FromWithNullField(), ToWithPopulatedField::class.java)
val result = shapeShift.map(FromWithNullField(), ToWithPopulatedField::class.java)
expectThat(result.long)
.isEqualTo(1L)
}

@Test
internal fun `mapping nested null object`() {
val result = mapper.map(FromWithNullShallowPath(), ToWithPopulatedField::class.java)
val result = shapeShift.map(FromWithNullShallowPath(), ToWithPopulatedField::class.java)

expectThat(result.long)
.isEqualTo(1L)
Expand All @@ -187,7 +187,7 @@ internal class FieldMapperKtTests {
@Test
internal fun `mismatch between from and to types should throw exception`() {
expectThrows<IllegalStateException> {
mapper.map(
shapeShift.map(
DefaultTransformerFrom(),
StringTo::class.java
)
Expand All @@ -197,7 +197,7 @@ internal class FieldMapperKtTests {
@Test
internal fun `supplying invalid from path should throw exception`() {
expectThrows<IllegalStateException> {
mapper.map(
shapeShift.map(
FromWithInvalidFromPath(), GenericTo::class.java
)
}
Expand All @@ -206,7 +206,7 @@ internal class FieldMapperKtTests {
@Test
internal fun `supplying invalid to path should throw exception`() {
expectThrows<IllegalStateException> {
mapper.map(
shapeShift.map(
FromWithInvalidToPath(), GenericTo::class.java
)
}
Expand All @@ -225,9 +225,9 @@ internal class FieldMapperKtTests {
true,
"second"
)
mapper.registerTransformer(firstRegistration)
shapeShift.registerTransformer(firstRegistration)
expectThrows<IllegalStateException> {
mapper.registerTransformer(secondRegistration)
shapeShift.registerTransformer(secondRegistration)
}
}

Expand All @@ -238,8 +238,8 @@ internal class FieldMapperKtTests {
false,
null
)
mapper.registerTransformer(registration)
expectThat(mapper.transformers.first().name)
shapeShift.registerTransformer(registration)
expectThat(shapeShift.transformers.first().name)
.isEqualTo(
"ExampleFieldTransformer"
)
Expand All @@ -253,16 +253,30 @@ internal class FieldMapperKtTests {
"first"
)

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

@Test
internal fun `using unregistered transformer by name should throw exception`() {
expectThrows<IllegalStateException> {
shapeShift.map(NameTransformerFrom(), StringTo::class.java)
}
}

@Test
internal fun `using unregistered transformer by type should throw exception`() {
expectThrows<IllegalStateException> {
shapeShift.map(TypeTransformerFrom(), StringTo::class.java)
}
}

@Test
internal fun `type level mapped field throws exception if mapFrom is empty`() {
expectThrows<IllegalStateException> {
mapper.map(FromWithInvalidTypeAnnotation(), GenericTo::class.java)
shapeShift.map(FromWithInvalidTypeAnnotation(), GenericTo::class.java)
}.and {
this.message.isEqualTo("mapFrom can not be empty when used at a type level")
}
Expand All @@ -271,14 +285,14 @@ internal class FieldMapperKtTests {
@Test
internal fun `field level mapped field without default mapping target and no target should throw exception`() {
expectThrows<IllegalStateException> {
mapper.map(FromWithoutDefinedTarget(), GenericTo::class.java)
shapeShift.map(FromWithoutDefinedTarget(), GenericTo::class.java)
}
}

@Test
internal fun `type level mapped field without default mapping target and no target should throw exception`() {
expectThrows<IllegalStateException> {
mapper.map(TypeFromWithoutDefinedTarget(), GenericTo::class.java)
shapeShift.map(TypeFromWithoutDefinedTarget(), GenericTo::class.java)
}
}

Expand Down

0 comments on commit 58d725c

Please sign in to comment.