Skip to content

Commit

Permalink
feat: add shapeshift instance to context objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Oct 27, 2022
1 parent defed11 commit 1ad5690
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
8 changes: 4 additions & 4 deletions shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShift.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ShapeShift internal constructor(

val decorators = getDecorators<From, To>(classPair)
if (decorators.isNotEmpty()) {
val context = MappingDecoratorContext(fromObject, toObject)
val context = MappingDecoratorContext(fromObject, toObject, this)
for (decorator in decorators) {
decorator.decorate(context)
}
Expand Down Expand Up @@ -139,19 +139,19 @@ class ShapeShift internal constructor(

if (condition != null) {
condition as MappingCondition<Any>
val context = MappingConditionContext(fromValue)
val context = MappingConditionContext(fromValue, this)
if (!condition.isValid(context)) {
return
}
}

val valueToSet = if (resolvedMappedField.transformer != null) {
val transformer = resolvedMappedField.transformer as MappingTransformer<Any, Any>
val context = MappingTransformerContext(fromValue, fromObject, toObject, fromPair.field, toPair.field)
val context = MappingTransformerContext(fromValue, fromObject, toObject, fromPair.field, toPair.field, this)
transformer.transform(context)
} else if (transformerRegistration != MappingTransformerRegistration.EMPTY) {
val transformer = transformerRegistration.transformer as MappingTransformer<Any, Any>
val context = MappingTransformerContext(fromValue, fromObject, toObject, fromPair.field, toPair.field)
val context = MappingTransformerContext(fromValue, fromObject, toObject, fromPair.field, toPair.field, this)
transformer.transform(context)
} else {
fromValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@

package dev.krud.shapeshift.condition

import dev.krud.shapeshift.ShapeShift

/**
* The context payload used for [MappingCondition]
*/
data class MappingConditionContext<Value : Any?>(
/**
* The value to be mapped
*/
val originalValue: Value?
val originalValue: Value?,
/**
* The [ShapeShift] instance used
*/
val shapeShift: ShapeShift
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package dev.krud.shapeshift.decorator

import dev.krud.shapeshift.ShapeShift

/**
* The context payload used for [MappingDecorator]
*/
Expand All @@ -21,5 +23,9 @@ data class MappingDecoratorContext<From : Any, To : Any>(
/**
* The to (target) object of the mapping
*/
val to: To
val to: To,
/**
* The [ShapeShift] instance used
*/
val shapeShift: ShapeShift
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

package dev.krud.shapeshift.transformer.base

import dev.krud.shapeshift.ShapeShift
import java.lang.reflect.Field

data class MappingTransformerContext<From : Any?>(
val originalValue: From?,
val fromObject: Any,
val toObject: Any,
val fromField: Field,
val toField: Field
val toField: Field,
val shapeShift: ShapeShift
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package dev.krud.shapeshift.transformer

import dev.krud.shapeshift.ShapeShiftBuilder
import dev.krud.shapeshift.transformer.base.MappingTransformerContext
import kotlin.reflect.jvm.javaField

Expand All @@ -23,6 +24,7 @@ fun <From : Any?> mockMappingTransformerContext(value: From?): MappingTransforme
ExampleObject(),
ExampleObject(),
ExampleObject::name.javaField!!,
ExampleObject::name.javaField!!
ExampleObject::name.javaField!!,
ShapeShiftBuilder().build()
)
}

0 comments on commit 1ad5690

Please sign in to comment.