Skip to content

Commit

Permalink
fix(transformer): prefer primary over secondary constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
DebitCardz committed Jul 6, 2024
1 parent ddac4ff commit 5c5616f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "gg.ingot"
version = "1.3.0"
version = "1.3.1"

repositories {
mavenCentral()
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/gg/ingot/iron/sql/params/SqlParamsBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,16 @@ fun sqlParams(vararg values: Pair<String, Any?>): SqlParamsBuilder {
return SqlParamsBuilder(values.toMap().toMutableMap())
}

/**
* Convert a list of models into a SqlParams instance.
* @param models The models to convert into a SqlParams instance.
* @return The SqlParams instance for chaining.
*/
fun namedSqlParams(vararg models: ExplodingModel): SqlParamsBuilder {
return SqlParamsBuilder(mutableMapOf()).apply {
models.forEach { this + it }
}
}

/** The parameters to use in a SQL query. */
typealias SqlParams = MutableMap<String, Any?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import gg.ingot.iron.IronSettings
import gg.ingot.iron.annotations.Model
import gg.ingot.iron.representation.EntityField
import gg.ingot.iron.strategies.NamingStrategy
import org.slf4j.LoggerFactory
import java.sql.ResultSet
import kotlin.reflect.KClass
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.isAccessible

/**
Expand Down Expand Up @@ -45,7 +47,11 @@ internal class ResultTransformer(
val entity = modelTransformer.transform(clazz)

val emptyConstructor = clazz.constructors.firstOrNull { it.parameters.isEmpty() }
val fullConstructor = clazz.constructors.firstOrNull { it.parameters.size == entity.fields.size }
// prefer primary constructor first
// then try to use secondary constructors if they have the same length of fields.
val fullConstructor = clazz.primaryConstructor
?.takeIf { it.parameters.size == entity.fields.size }
?: clazz.constructors.firstOrNull { it.parameters.size == entity.fields.size }

if (emptyConstructor != null) {
emptyConstructor.isAccessible = true
Expand Down

0 comments on commit 5c5616f

Please sign in to comment.