Skip to content

Commit

Permalink
fix: fix bug with transformers of primitive types not unboxing
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed May 19, 2022
1 parent 4891087 commit 409da96
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShift.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class ShapeShift constructor(
}
if (value != null) {
try {
if (!toPair.field.type.isAssignableFrom(value::class.java)) {
error("Type mismatch: Expected ${toPair.field.type} but got ${value::class.java}")
if (!toPair.type.isAssignableFrom(value::class.java)) {
error("Type mismatch: Expected ${toPair.type} but got ${value::class.java}")
}
toPair.field.setValue(toPair.target, value)
} catch (e: Exception) {
Expand Down Expand Up @@ -139,15 +139,17 @@ class ShapeShift constructor(
"Field ${nodes.firstOrNull()} not found on class ${target::class.java}"
)

val fieldType = field.type.kotlin.javaObjectType

if (nodes.size == 1) {
return ObjectFieldTrio(target, field, field.type) // todo: boxPrimitveType
return ObjectFieldTrio(target, field, fieldType)
}
nodes.removeFirst()
field.isAccessible = true
var subTarget = field.get(target)

if (subTarget == null && type == SourceType.TO) {
subTarget = field.type.newInstance()
subTarget = fieldType.newInstance()
field.set(target, subTarget)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ interface FieldTransformer<From : Any, To : Any> {
fun transform(fromField: Field, toField: Field, originalValue: From?, fromObject: Any, toObject: Any): To?

companion object {
val FieldTransformer<*, *>.id: ClassPair get() = fromType to toType
val FieldTransformer<*, *>.id: ClassPair get() = fromType.kotlin.javaObjectType to toType.kotlin.javaObjectType
}
}

0 comments on commit 409da96

Please sign in to comment.