Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sealed subclass picking #338

Merged
merged 5 commits into from Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -11,6 +11,7 @@ import com.sksamuel.hoplite.fp.plus
import com.sksamuel.hoplite.fp.sequence
import com.sksamuel.hoplite.fp.valid
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KType
import kotlin.reflect.full.createType

Expand Down Expand Up @@ -61,7 +62,16 @@ class SealedClassDecoder : NullHandlingDecoder<Any> {
} else null

val results = kclass.sealedSubclasses
.sortedByDescending { subclass -> subclass.constructors.maxOfOrNull { it.parameters.size } ?: 0 }
.filter { subclass ->
subclass hasConstructorsWithArgumentsNumberLessOrEqualTo node.expectedNumberOfConstructorArguments
}
.sortedWith { subclass1, subclass2 ->
(
subclass1.numberOfMandatoryConstructorArguments.compareTo(subclass2.numberOfMandatoryConstructorArguments)
.takeUnless { it == 0 }
?: subclass1.numberOfTotalConstructorArguments.compareTo(subclass2.numberOfTotalConstructorArguments)
) * -1
}
.map { DataClassDecoder().decode(node, it.createType(), context) }

val success = results.firstOrNull { it.isValid() }
Expand All @@ -72,4 +82,18 @@ class SealedClassDecoder : NullHandlingDecoder<Any> {
}
}
}

private val KClass<*>.numberOfTotalConstructorArguments
get() = constructors.maxOfOrNull { it.numberOfTotalArguments } ?: 0

private val KClass<*>.numberOfMandatoryConstructorArguments
get() = constructors.maxOfOrNull { it.numberOfMandatoryArguments } ?: 0

private infix fun KClass<*>.hasConstructorsWithArgumentsNumberLessOrEqualTo(number: Int) =
constructors.map { it.numberOfMandatoryArguments }.any { it <= number }

private val KFunction<*>.numberOfMandatoryArguments get() = parameters.filterNot { it.isOptional }.size
private val KFunction<*>.numberOfTotalArguments get() = parameters.size

private val Node.expectedNumberOfConstructorArguments get() = size.takeIf { it > 0 } ?: 1
}
Expand Up @@ -7,10 +7,20 @@ import io.kotest.matchers.shouldBe

sealed class ClientAuthConfig {
data class UrlUserPass(val url: String, val user: String, val password: Masked) : ClientAuthConfig()

data class ConfigWithOneDefaultValue(val otherStuff: String = "") : ClientAuthConfig()

data class ConfigWithTwoDefaultValues(val otherStuff: String = "", val anotherStuff: String = "") : ClientAuthConfig()
data class Url(val url: String) : ClientAuthConfig()
}

data class DbConfig(val clientAuth: ClientAuthConfig, val clientNoAuth: ClientAuthConfig)
data class DbConfig(
val clientAuth: ClientAuthConfig,
val clientNoAuth: ClientAuthConfig = ClientAuthConfig.ConfigWithOneDefaultValue(),
val clientWithOneSpecifiedValue: ClientAuthConfig,
val clientWithTwoSpecifiedValue: ClientAuthConfig,
val clientWithDefaultValues: ClientAuthConfig = ClientAuthConfig.ConfigWithOneDefaultValue(),
)

class SealedClassTest : FunSpec() {
init {
Expand All @@ -22,6 +32,14 @@ class SealedClassTest : FunSpec() {
password = Masked("3pass"),
)
config.clientNoAuth shouldBe ClientAuthConfig.Url(url = "1url")
config.clientWithOneSpecifiedValue shouldBe ClientAuthConfig.ConfigWithTwoDefaultValues(
otherStuff = "1url",
)
config.clientWithTwoSpecifiedValue shouldBe ClientAuthConfig.ConfigWithTwoDefaultValues(
otherStuff = "1url",
anotherStuff = "test"
)
config.clientWithDefaultValues shouldBe ClientAuthConfig.ConfigWithOneDefaultValue()
}
}
}
9 changes: 9 additions & 0 deletions hoplite-hocon/src/test/resources/sealed.conf
Expand Up @@ -7,3 +7,12 @@ clientAuth {
clientNoAuth {
url = "1url"
}

clientWithOneSpecifiedValue {
otherStuff = "1url"
}

clientWithTwoSpecifiedValue {
otherStuff = "1url"
anotherStuff = "test"
}