[CORDA-1482] Make boolean config variables case insensitive (#3622)

* Make boolean config variables case insensitive

* Address review comments
This commit is contained in:
Anthony Keenan
2018-07-21 11:54:02 +01:00
committed by GitHub
parent d2446be69e
commit 7853cfe003
6 changed files with 44 additions and 17 deletions

View File

@ -121,7 +121,7 @@ private fun Config.getSingleValue(path: String, type: KType, onUnknownKeys: (Set
Int::class -> getInt(path)
Long::class -> getLong(path)
Double::class -> getDouble(path)
Boolean::class -> getBoolean(path)
Boolean::class -> getBooleanCaseInsensitive(path)
LocalDate::class -> LocalDate.parse(getString(path))
Duration::class -> getDuration(path)
Instant::class -> Instant.parse(getString(path))
@ -276,6 +276,19 @@ private fun Iterable<*>.toConfigIterable(field: Field): Iterable<Any?> {
}
}
// The typesafe .getBoolean function is case sensitive, this is a case insensitive version
fun Config.getBooleanCaseInsensitive(path: String): Boolean {
try {
return getBoolean(path)
} catch(e:Exception) {
val stringVal = getString(path).toLowerCase()
if (stringVal == "true" || stringVal == "false") {
return stringVal.toBoolean()
}
throw e
}
}
private val logger = LoggerFactory.getLogger("net.corda.nodeapi.internal.config")
enum class UnknownConfigKeysPolicy(private val handle: (Set<String>, logger: Logger) -> Unit) {

View File

@ -21,6 +21,7 @@ import net.corda.core.utilities.days
import net.corda.core.utilities.getOrThrow
import net.corda.core.utilities.seconds
import net.corda.nodeapi.internal.*
import net.corda.nodeapi.internal.config.getBooleanCaseInsensitive
import net.corda.nodeapi.internal.network.NodeInfoFilesCopier.Companion.NODE_INFO_FILE_NAME_PREFIX
import net.corda.serialization.internal.AMQP_P2P_CONTEXT
import net.corda.serialization.internal.CordaSerializationMagic
@ -289,7 +290,7 @@ class NetworkBootstrapper
// The config contains the notary type
val nodeConfig = configs[nodeInfoFile.parent]!!
if (nodeConfig.hasPath("notary")) {
val validating = nodeConfig.getBoolean("notary.validating")
val validating = nodeConfig.getBooleanCaseInsensitive("notary.validating")
// And the node-info file contains the notary's identity
val nodeInfo = nodeInfoFile.readObject<SignedNodeInfo>().verified()
NotaryInfo(nodeInfo.notaryIdentity(), validating)

View File

@ -1,6 +1,7 @@
package net.corda.nodeapi.internal.config
import com.typesafe.config.Config
import com.typesafe.config.ConfigException
import com.typesafe.config.ConfigFactory.empty
import com.typesafe.config.ConfigRenderOptions.defaults
import com.typesafe.config.ConfigValueFactory
@ -8,6 +9,7 @@ import net.corda.core.identity.CordaX500Name
import net.corda.core.internal.div
import net.corda.core.utilities.NetworkHostAndPort
import org.assertj.core.api.Assertions.*
import org.hibernate.exception.DataException
import org.junit.Test
import java.net.URL
import java.nio.file.Path
@ -41,8 +43,16 @@ class ConfigParsingTest {
@Test
fun Boolean() {
testPropertyType<BooleanData, BooleanListData, Boolean>(true, false)
assertThat(config(Pair("value", "false")).parseAs<BooleanData>().value).isEqualTo(false)
assertThat(config(Pair("value", "False")).parseAs<BooleanData>().value).isEqualTo(false)
assertThat(config(Pair("value", "FALSE")).parseAs<BooleanData>().value).isEqualTo(false)
assertThat(config(Pair("value", "true")).parseAs<BooleanData>().value).isEqualTo(true)
assertThat(config(Pair("value", "True")).parseAs<BooleanData>().value).isEqualTo(true)
assertThat(config(Pair("value", "TRUE")).parseAs<BooleanData>().value).isEqualTo(true)
assertThatThrownBy { config(Pair("value", "stilton")).parseAs<BooleanData>().value }
.isInstanceOf(ConfigException.WrongType::class.java)
.hasMessageContaining("hardcoded value: value has type STRING rather than BOOLEAN")
}
@Test
fun Enum() {
testPropertyType<EnumData, EnumListData, TestEnum>(TestEnum.Value2, TestEnum.Value1, valuesToString = true)