Skip to content

Commit

Permalink
chore: address sonarcloud additional codesmells
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Nov 3, 2022
1 parent c1d2d93 commit 595f2cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
62 changes: 31 additions & 31 deletions shapeshift/src/main/kotlin/dev/krud/shapeshift/ShapeShift.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class ShapeShift internal constructor(
* Map [fromObjects] to a list of [toClazz] objects
*/
fun <From : Any, To : Any> mapCollection(fromObjects: Collection<From>, toClazz: Class<To>): List<To> {
val toObjects = ArrayList<To>(fromObjects.size)
val toObjects = mutableListOf<To>()
for (fromObject in fromObjects) {
toObjects.add(map(fromObject, toClazz))
}
Expand All @@ -115,36 +115,18 @@ class ShapeShift internal constructor(
fromPair.field.isAccessible = true
toPair.field.isAccessible = true

val mappingStrategy: MappingStrategy

if (resolvedMappedField.overrideMappingStrategy != null && resolvedMappedField.overrideMappingStrategy != MappingStrategy.NONE) {
mappingStrategy = resolvedMappedField.overrideMappingStrategy
} else {
mappingStrategy = defaultMappingStrategy
}
val mappingStrategy = resolvedMappedField.effectiveMappingStrategy(defaultMappingStrategy)
val fromValue = fromPair.field.getValue(fromPair.target)

val shouldMap = when (mappingStrategy) {
val shouldMapValue = when (mappingStrategy) {
MappingStrategy.NONE -> error("Mapping strategy is set to NONE")
MappingStrategy.MAP_ALL -> true
MappingStrategy.MAP_NOT_NULL -> fromValue != null
}

if (shouldMap) {
if (shouldMapValue) {
try {
val condition = resolvedMappedField.condition
?: if (resolvedMappedField.conditionClazz != null) {
getConditionInstance(resolvedMappedField.conditionClazz)
} else {
null
}

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

val valueToSet = if (resolvedMappedField.transformer != null) {
Expand Down Expand Up @@ -176,6 +158,31 @@ class ShapeShift internal constructor(
}
}

private fun ResolvedMappedField.conditionMatches(value: Any?): Boolean {
val condition = this.condition
?: this.conditionClazz?.getCachedInstance()
?: return true
condition as MappingCondition<Any>
val context = MappingConditionContext(value, this@ShapeShift)
return condition.isValid(context)
}

private fun ResolvedMappedField.effectiveMappingStrategy(defaultMappingStrategy: MappingStrategy): MappingStrategy {
return if (overrideMappingStrategy != null && overrideMappingStrategy != MappingStrategy.NONE
) {
overrideMappingStrategy
} else {
defaultMappingStrategy
}
}

private fun Class<out MappingCondition<*>>?.getCachedInstance(): MappingCondition<*>? {
this ?: return null
return conditionCache.computeIfAbsent(this) {
this.getDeclaredConstructor().newInstance()
}
}

private fun getFieldInstanceByNodes(nodes: List<Field>, target: Any?, type: SourceType): ObjectFieldTrio? {
// This if only applies to recursive runs of this function
// When target is null and type is from, don't attempt to instantiate the object
Expand Down Expand Up @@ -258,12 +265,6 @@ class ShapeShift internal constructor(
return transformerRegistration
}

private fun getConditionInstance(conditionClazz: Class<out MappingCondition<*>>): MappingCondition<*> {
return conditionCache.computeIfAbsent(conditionClazz) {
conditionClazz.getDeclaredConstructor().newInstance()
}
}

private fun <From : Any, To : Any> registerTransformer(registration: MappingTransformerRegistration<From, To>) {
if (registration.default) {
val existingDefaultTransformer = defaultTransformers[registration.id]
Expand All @@ -290,7 +291,6 @@ class ShapeShift internal constructor(
}

companion object {

enum class SourceType {
FROM,
TO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import dev.krud.shapeshift.transformer.StringToLongMappingTransformer
import dev.krud.shapeshift.transformer.StringToShortMappingTransformer
import dev.krud.shapeshift.transformer.base.MappingTransformer
import java.util.function.Supplier
import kotlin.reflect.KClass

/**
* A builder used to create a new ShapeShift instance.
Expand Down

0 comments on commit 595f2cb

Please sign in to comment.