Made strict behaviour optional default to true for conf parsing. HSM and Doorman can opt in for this functionality independently.

This commit is contained in:
sollecitom 2018-03-01 18:08:22 +00:00
parent b39a1a71d9
commit 38208d9e44
3 changed files with 19 additions and 17 deletions

View File

@ -102,7 +102,7 @@ fun parseParameters(vararg args: String): NetworkManagementServerParameters {
val config = argConfig.withFallback(ConfigFactory.parseFile(configFile.toFile(), ConfigParseOptions.defaults().setAllowMissing(true))) val config = argConfig.withFallback(ConfigFactory.parseFile(configFile.toFile(), ConfigParseOptions.defaults().setAllowMissing(true)))
.resolve() .resolve()
.parseAs<NetworkManagementServerParameters>() .parseAs<NetworkManagementServerParameters>(false)
// Make sure trust store password is only specified in root keygen mode. // Make sure trust store password is only specified in root keygen mode.
if (config.mode != Mode.ROOT_KEYGEN) { if (config.mode != Mode.ROOT_KEYGEN) {

View File

@ -73,5 +73,5 @@ fun parseParameters(vararg args: String): Parameters {
require(configFile.isRegularFile()) { "Config file $configFile does not exist" } require(configFile.isRegularFile()) { "Config file $configFile does not exist" }
val config = argConfig.withFallback(ConfigFactory.parseFile(configFile.toFile(), ConfigParseOptions.defaults().setAllowMissing(true))).resolve() val config = argConfig.withFallback(ConfigFactory.parseFile(configFile.toFile(), ConfigParseOptions.defaults().setAllowMissing(true))).resolve()
return config.parseAs() return config.parseAs(false)
} }

View File

@ -36,25 +36,27 @@ operator fun <T : Any> Config.getValue(receiver: Any, metadata: KProperty<*>): T
return getValueInternal(metadata.name, metadata.returnType) return getValueInternal(metadata.name, metadata.returnType)
} }
fun <T : Any> Config.parseAs(clazz: KClass<T>): T { fun <T : Any> Config.parseAs(clazz: KClass<T>, strict: Boolean = true): T {
require(clazz.isData) { "Only Kotlin data classes can be parsed. Offending: ${clazz.qualifiedName}" } require(clazz.isData) { "Only Kotlin data classes can be parsed. Offending: ${clazz.qualifiedName}" }
val constructor = clazz.primaryConstructor!! val constructor = clazz.primaryConstructor!!
val parameters = constructor.parameters val parameters = constructor.parameters
val parameterNames = parameters.flatMap { param -> if (strict) {
mutableSetOf<String>().apply { val parameterNames = parameters.flatMap { param ->
param.name?.let(this::add) mutableSetOf<String>().apply {
clazz.memberProperties.singleOrNull { it.name == param.name }?.let { matchingProperty -> param.name?.let(this::add)
matchingProperty.annotations.filterIsInstance<OldConfig>().map { it.value }.forEach { this.add(it) } clazz.memberProperties.singleOrNull { it.name == param.name }?.let { matchingProperty ->
matchingProperty.annotations.filterIsInstance<OldConfig>().map { it.value }.forEach { this.add(it) }
}
} }
} }
} val unknownConfigurationKeys = this.entrySet()
val unknownConfigurationKeys = this.entrySet() .mapNotNull { it.key.split(".").firstOrNull() }
.mapNotNull { it.key.split(".").firstOrNull() } .filterNot { it == CUSTOM_NODE_PROPERTIES_ROOT }
.filterNot { it == CUSTOM_NODE_PROPERTIES_ROOT } .filterNot(parameterNames::contains)
.filterNot(parameterNames::contains) .toSortedSet()
.toSortedSet() if (unknownConfigurationKeys.isNotEmpty()) {
if (unknownConfigurationKeys.isNotEmpty()) { throw UnknownConfigurationKeysException.of(unknownConfigurationKeys)
throw UnknownConfigurationKeysException.of(unknownConfigurationKeys) }
} }
val args = parameters.filterNot { it.isOptional && !hasPath(it.name!!) }.associateBy({ it }) { param -> val args = parameters.filterNot { it.isOptional && !hasPath(it.name!!) }.associateBy({ it }) { param ->
// Get the matching property for this parameter // Get the matching property for this parameter
@ -79,7 +81,7 @@ class UnknownConfigurationKeysException private constructor(val unknownKeys: Set
} }
} }
inline fun <reified T : Any> Config.parseAs(): T = parseAs(T::class) inline fun <reified T : Any> Config.parseAs(strict: Boolean = true): T = parseAs(T::class, strict)
fun Config.toProperties(): Properties { fun Config.toProperties(): Properties {
return entrySet().associateByTo( return entrySet().associateByTo(