-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e0c71b
commit 14a8d79
Showing
3 changed files
with
86 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...er-plugin/src/main/kotlin/io/github/mappie/resolving/classes/ObjectMappingsConstructor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.mappie.resolving.classes | ||
|
||
import io.github.mappie.resolving.ConstructorCallMapping | ||
import io.github.mappie.util.getterName | ||
import org.jetbrains.kotlin.ir.declarations.IrConstructor | ||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction | ||
import org.jetbrains.kotlin.ir.declarations.IrValueParameter | ||
import org.jetbrains.kotlin.ir.types.IrType | ||
import org.jetbrains.kotlin.ir.util.hasDefaultValue | ||
import org.jetbrains.kotlin.name.Name | ||
|
||
class ObjectMappingsConstructor(val targetType: IrType, val source: IrValueParameter) { | ||
|
||
var getters = mutableListOf<IrSimpleFunction>() | ||
|
||
// TODO: Map of lists for detecting duplicates | ||
var explicit = mutableListOf<Pair<Name, ObjectMappingSource>>() | ||
|
||
var constructor: IrConstructor? = null | ||
|
||
private val targets | ||
get() = constructor?.valueParameters ?: emptyList() | ||
|
||
fun construct(): ConstructorCallMapping { | ||
val mappings = targets.associateWith { target -> | ||
val concreteSource = explicit.firstOrNull { it.first == target.name } | ||
|
||
if (concreteSource != null) { | ||
listOf(concreteSource.second) | ||
} else { | ||
val getter = getters.firstOrNull { getter -> | ||
getter.name == getterName(target.name) | ||
} | ||
if (getter != null) { | ||
listOf(PropertySource(getter.symbol, target.type, source.symbol, true)) | ||
} else if (target.hasDefaultValue()) { | ||
listOf(DefaultParameterValueSource(target.defaultValue!!.expression)) | ||
} else { | ||
emptyList() | ||
} | ||
} | ||
} | ||
|
||
return ConstructorCallMapping( | ||
targetType = targetType, | ||
sourceType = source.type, | ||
symbol = constructor!!.symbol, | ||
mappings = mappings | ||
) | ||
} | ||
|
||
fun explicit(entry: Pair<Name, ObjectMappingSource>): ObjectMappingsConstructor = | ||
apply { explicit.add(entry) } | ||
|
||
companion object { | ||
fun of(constructor: ObjectMappingsConstructor) = | ||
ObjectMappingsConstructor(constructor.targetType, constructor.source).apply { | ||
getters = constructor.getters | ||
explicit = constructor.explicit | ||
} | ||
|
||
fun of(targetType: IrType, source: IrValueParameter) = | ||
ObjectMappingsConstructor(targetType, source) | ||
} | ||
} |