mirror of
https://github.com/corda/corda.git
synced 2025-06-17 06:38:21 +00:00
[CORDA-2224]: Removed field value
from Validated
. Renamed function orThrow()
to value()
. (#4231)
This commit is contained in:
committed by
GitHub
parent
aa0ccecfde
commit
8aaf120881
@ -117,7 +117,7 @@ private class OptionalPropertyWithDefault<TYPE : Any>(delegate: Configuration.Pr
|
|||||||
|
|
||||||
private class FunctionalProperty<TYPE, MAPPED : Any>(delegate: Configuration.Property.Definition.Standard<TYPE>, private val mappedTypeName: String, internal val extractListValue: (Config, String) -> List<TYPE>, private val convert: (TYPE) -> Valid<MAPPED>) : RequiredDelegatedProperty<MAPPED, Configuration.Property.Definition.Standard<TYPE>>(delegate), Configuration.Property.Definition.Standard<MAPPED> {
|
private class FunctionalProperty<TYPE, MAPPED : Any>(delegate: Configuration.Property.Definition.Standard<TYPE>, private val mappedTypeName: String, internal val extractListValue: (Config, String) -> List<TYPE>, private val convert: (TYPE) -> Valid<MAPPED>) : RequiredDelegatedProperty<MAPPED, Configuration.Property.Definition.Standard<TYPE>>(delegate), Configuration.Property.Definition.Standard<MAPPED> {
|
||||||
|
|
||||||
override fun valueIn(configuration: Config) = convert.invoke(delegate.valueIn(configuration)).orThrow()
|
override fun valueIn(configuration: Config) = convert.invoke(delegate.valueIn(configuration)).value()
|
||||||
|
|
||||||
override val typeName: String = if (super.typeName == "#$mappedTypeName") super.typeName else "$mappedTypeName(${super.typeName})"
|
override val typeName: String = if (super.typeName == "#$mappedTypeName") super.typeName else "$mappedTypeName(${super.typeName})"
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ class SpecificationTest {
|
|||||||
val rpcSettings = RpcSettingsSpec.parse(configuration)
|
val rpcSettings = RpcSettingsSpec.parse(configuration)
|
||||||
|
|
||||||
assertThat(rpcSettings.isValid).isTrue()
|
assertThat(rpcSettings.isValid).isTrue()
|
||||||
assertThat(rpcSettings.orThrow()).satisfies { value ->
|
assertThat(rpcSettings.value()).satisfies { value ->
|
||||||
|
|
||||||
assertThat(value.useSsl).isEqualTo(useSslValue)
|
assertThat(value.useSsl).isEqualTo(useSslValue)
|
||||||
assertThat(value.addresses).satisfies { addresses ->
|
assertThat(value.addresses).satisfies { addresses ->
|
||||||
|
@ -18,7 +18,7 @@ class VersionExtractorTest {
|
|||||||
val versionValue = Configuration.Version.Extractor.DEFAULT_VERSION_VALUE + 1
|
val versionValue = Configuration.Version.Extractor.DEFAULT_VERSION_VALUE + 1
|
||||||
val rawConfiguration = configObject("configuration" to configObject("metadata" to configObject("version" to versionValue), "node" to configObject("p2pAddress" to "localhost:8080"))).toConfig()
|
val rawConfiguration = configObject("configuration" to configObject("metadata" to configObject("version" to versionValue), "node" to configObject("p2pAddress" to "localhost:8080"))).toConfig()
|
||||||
|
|
||||||
val version = extractVersion.invoke(rawConfiguration).orThrow()
|
val version = extractVersion.invoke(rawConfiguration).value()
|
||||||
assertThat(version).isEqualTo(versionValue)
|
assertThat(version).isEqualTo(versionValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ class VersionExtractorTest {
|
|||||||
|
|
||||||
val rawConfiguration = configObject("configuration" to configObject("node" to configObject("p2pAddress" to "localhost:8080"))).toConfig()
|
val rawConfiguration = configObject("configuration" to configObject("node" to configObject("p2pAddress" to "localhost:8080"))).toConfig()
|
||||||
|
|
||||||
val version = extractVersion.invoke(rawConfiguration).orThrow()
|
val version = extractVersion.invoke(rawConfiguration).value()
|
||||||
assertThat(version).isEqualTo(Configuration.Version.Extractor.DEFAULT_VERSION_VALUE)
|
assertThat(version).isEqualTo(Configuration.Version.Extractor.DEFAULT_VERSION_VALUE)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ class VersionExtractorTest {
|
|||||||
|
|
||||||
val rawConfiguration = configObject("configuration" to configObject("metadata" to configObject(), "node" to configObject("p2pAddress" to "localhost:8080"))).toConfig()
|
val rawConfiguration = configObject("configuration" to configObject("metadata" to configObject(), "node" to configObject("p2pAddress" to "localhost:8080"))).toConfig()
|
||||||
|
|
||||||
val version = extractVersion.invoke(rawConfiguration).orThrow()
|
val version = extractVersion.invoke(rawConfiguration).value()
|
||||||
|
|
||||||
assertThat(version).isEqualTo(Configuration.Version.Extractor.DEFAULT_VERSION_VALUE)
|
assertThat(version).isEqualTo(Configuration.Version.Extractor.DEFAULT_VERSION_VALUE)
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ class VersionExtractorTest {
|
|||||||
|
|
||||||
val rawConfiguration = configObject("configuration" to configObject("metadata" to configObject("version" to null), "node" to configObject("p2pAddress" to "localhost:8080"))).toConfig()
|
val rawConfiguration = configObject("configuration" to configObject("metadata" to configObject("version" to null), "node" to configObject("p2pAddress" to "localhost:8080"))).toConfig()
|
||||||
|
|
||||||
val version = extractVersion.invoke(rawConfiguration).orThrow()
|
val version = extractVersion.invoke(rawConfiguration).value()
|
||||||
|
|
||||||
assertThat(version).isEqualTo(Configuration.Version.Extractor.DEFAULT_VERSION_VALUE)
|
assertThat(version).isEqualTo(Configuration.Version.Extractor.DEFAULT_VERSION_VALUE)
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ class VersionExtractorTest {
|
|||||||
|
|
||||||
val rawConfiguration = configObject().toConfig()
|
val rawConfiguration = configObject().toConfig()
|
||||||
|
|
||||||
val version = extractVersion.invoke(rawConfiguration).orThrow()
|
val version = extractVersion.invoke(rawConfiguration).value()
|
||||||
|
|
||||||
assertThat(version).isEqualTo(Configuration.Version.Extractor.DEFAULT_VERSION_VALUE)
|
assertThat(version).isEqualTo(Configuration.Version.Extractor.DEFAULT_VERSION_VALUE)
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ class VersionedParsingExampleTest {
|
|||||||
private fun assertResult(result: Valid<RpcSettings>, principalAddressValue: Address, adminAddressValue: Address) {
|
private fun assertResult(result: Valid<RpcSettings>, principalAddressValue: Address, adminAddressValue: Address) {
|
||||||
|
|
||||||
assertThat(result.isValid).isTrue()
|
assertThat(result.isValid).isTrue()
|
||||||
assertThat(result.orThrow()).satisfies { value ->
|
assertThat(result.value()).satisfies { value ->
|
||||||
|
|
||||||
assertThat(value.principal).isEqualTo(principalAddressValue)
|
assertThat(value.principal).isEqualTo(principalAddressValue)
|
||||||
assertThat(value.admin).isEqualTo(adminAddressValue)
|
assertThat(value.admin).isEqualTo(adminAddressValue)
|
||||||
@ -94,7 +94,7 @@ class VersionedParsingExampleTest {
|
|||||||
val adminAddress = addressFor(adminHost, adminPort)
|
val adminAddress = addressFor(adminHost, adminPort)
|
||||||
|
|
||||||
return if (principalAddress.isValid && adminAddress.isValid) {
|
return if (principalAddress.isValid && adminAddress.isValid) {
|
||||||
return valid(RpcSettings(principalAddress.value, adminAddress.value))
|
return valid(RpcSettings(principalAddress.value(), adminAddress.value()))
|
||||||
} else {
|
} else {
|
||||||
invalid(principalAddress.errors + adminAddress.errors)
|
invalid(principalAddress.errors + adminAddress.errors)
|
||||||
}
|
}
|
||||||
@ -128,4 +128,4 @@ class VersionedParsingExampleTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Configuration.Version.Extractor.parseRequired(config: Config, options: Configuration.Validation.Options = Configuration.Validation.Options.defaults) = parse(config, options).map { it ?: throw IllegalStateException("Absent version value.") }
|
private fun Configuration.Version.Extractor.parseRequired(config: Config, options: Configuration.Validation.Options = Configuration.Validation.Options.defaults) = parse(config, options).map { it }
|
@ -8,11 +8,11 @@ import java.util.Collections.emptySet
|
|||||||
*/
|
*/
|
||||||
interface Validated<TARGET, ERROR> {
|
interface Validated<TARGET, ERROR> {
|
||||||
/**
|
/**
|
||||||
* The valid [TARGET] value.
|
* Returns a valid [TARGET] if no validation errors are present. Otherwise, it throws the exception produced by [exceptionOnErrors], defaulting to [IllegalStateException].
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if accessed in presence of validation errors.
|
* @throws IllegalStateException or the result of [exceptionOnErrors] if there are errors.
|
||||||
*/
|
*/
|
||||||
val value: TARGET
|
fun value(exceptionOnErrors: (Set<ERROR>) -> Exception = { errors -> IllegalStateException(errors.joinToString(System.lineSeparator())) }): TARGET
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The errors produced during validation, if any.
|
* The errors produced during validation, if any.
|
||||||
@ -32,14 +32,7 @@ interface Validated<TARGET, ERROR> {
|
|||||||
/**
|
/**
|
||||||
* Returns the underlying value as optional, with a null result instead of an exception if validation rules were violated.
|
* Returns the underlying value as optional, with a null result instead of an exception if validation rules were violated.
|
||||||
*/
|
*/
|
||||||
val optional: TARGET? get() = if (isValid) value else null
|
val optional: TARGET? get() = if (isValid) value() else null
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a valid [TARGET] if no validation errors are present. Otherwise, it throws the exception produced by [exceptionOnErrors], defaulting to [IllegalStateException].
|
|
||||||
*
|
|
||||||
* @throws IllegalStateException or the result of [exceptionOnErrors] if there are errors.
|
|
||||||
*/
|
|
||||||
fun orThrow(exceptionOnErrors: (Set<ERROR>) -> Exception = { errors -> IllegalStateException(errors.joinToString(System.lineSeparator())) }): TARGET
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies the [convert] function to the [TARGET] value, if valid. Otherwise, returns a [Validated] monad with a [MAPPED] generic type and the current errors set.
|
* Applies the [convert] function to the [TARGET] value, if valid. Otherwise, returns a [Validated] monad with a [MAPPED] generic type and the current errors set.
|
||||||
@ -62,7 +55,7 @@ interface Validated<TARGET, ERROR> {
|
|||||||
*/
|
*/
|
||||||
fun doIfValid(action: (TARGET) -> Unit): Validated<TARGET, ERROR> {
|
fun doIfValid(action: (TARGET) -> Unit): Validated<TARGET, ERROR> {
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
action.invoke(value)
|
action.invoke(value())
|
||||||
}
|
}
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@ -110,10 +103,10 @@ interface Validated<TARGET, ERROR> {
|
|||||||
/**
|
/**
|
||||||
* A successful validation result, containing a valid [TARGET] value and no [ERROR]s.
|
* A successful validation result, containing a valid [TARGET] value and no [ERROR]s.
|
||||||
*/
|
*/
|
||||||
class Successful<TARGET, ERROR>(override val value: TARGET) : Result<TARGET, ERROR>(), Validated<TARGET, ERROR> {
|
class Successful<TARGET, ERROR>(private val value: TARGET) : Result<TARGET, ERROR>(), Validated<TARGET, ERROR> {
|
||||||
override val errors: Set<ERROR> = emptySet<ERROR>()
|
override val errors: Set<ERROR> = emptySet<ERROR>()
|
||||||
|
|
||||||
override fun orThrow(exceptionOnErrors: (Set<ERROR>) -> Exception) = value
|
override fun value(exceptionOnErrors: (Set<ERROR>) -> Exception) = value
|
||||||
|
|
||||||
override fun <MAPPED> map(convert: (TARGET) -> MAPPED): Validated<MAPPED, ERROR> {
|
override fun <MAPPED> map(convert: (TARGET) -> MAPPED): Validated<MAPPED, ERROR> {
|
||||||
return valid(convert.invoke(value))
|
return valid(convert.invoke(value))
|
||||||
@ -136,9 +129,7 @@ interface Validated<TARGET, ERROR> {
|
|||||||
require(errors.isNotEmpty())
|
require(errors.isNotEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
override val value: TARGET get() = throw IllegalStateException("Invalid state.")
|
override fun value(exceptionOnErrors: (Set<ERROR>) -> Exception) = throw exceptionOnErrors.invoke(errors)
|
||||||
|
|
||||||
override fun orThrow(exceptionOnErrors: (Set<ERROR>) -> Exception) = throw exceptionOnErrors.invoke(errors)
|
|
||||||
|
|
||||||
override fun <MAPPED> map(convert: (TARGET) -> MAPPED): Validated<MAPPED, ERROR> {
|
override fun <MAPPED> map(convert: (TARGET) -> MAPPED): Validated<MAPPED, ERROR> {
|
||||||
return invalid(errors)
|
return invalid(errors)
|
||||||
|
@ -234,7 +234,7 @@ class NodeConfigurationImplTest {
|
|||||||
@Test
|
@Test
|
||||||
fun `jmxReporterType is null and defaults to Jokolia`() {
|
fun `jmxReporterType is null and defaults to Jokolia`() {
|
||||||
val rawConfig = getConfig("working-config.conf", ConfigFactory.parseMap(mapOf("devMode" to true)))
|
val rawConfig = getConfig("working-config.conf", ConfigFactory.parseMap(mapOf("devMode" to true)))
|
||||||
val nodeConfig = rawConfig.parseAsNodeConfiguration().orThrow()
|
val nodeConfig = rawConfig.parseAsNodeConfiguration().value()
|
||||||
assertTrue(JmxReporterType.JOLOKIA.toString() == nodeConfig.jmxReporterType.toString())
|
assertTrue(JmxReporterType.JOLOKIA.toString() == nodeConfig.jmxReporterType.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,7 +242,7 @@ class NodeConfigurationImplTest {
|
|||||||
fun `jmxReporterType is not null and is set to New Relic`() {
|
fun `jmxReporterType is not null and is set to New Relic`() {
|
||||||
var rawConfig = getConfig("working-config.conf", ConfigFactory.parseMap(mapOf("devMode" to true)))
|
var rawConfig = getConfig("working-config.conf", ConfigFactory.parseMap(mapOf("devMode" to true)))
|
||||||
rawConfig = rawConfig.withValue("jmxReporterType", ConfigValueFactory.fromAnyRef("NEW_RELIC"))
|
rawConfig = rawConfig.withValue("jmxReporterType", ConfigValueFactory.fromAnyRef("NEW_RELIC"))
|
||||||
val nodeConfig = rawConfig.parseAsNodeConfiguration().orThrow()
|
val nodeConfig = rawConfig.parseAsNodeConfiguration().value()
|
||||||
assertTrue(JmxReporterType.NEW_RELIC.toString() == nodeConfig.jmxReporterType.toString())
|
assertTrue(JmxReporterType.NEW_RELIC.toString() == nodeConfig.jmxReporterType.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,7 +250,7 @@ class NodeConfigurationImplTest {
|
|||||||
fun `jmxReporterType is not null and set to Jokolia`() {
|
fun `jmxReporterType is not null and set to Jokolia`() {
|
||||||
var rawConfig = getConfig("working-config.conf", ConfigFactory.parseMap(mapOf("devMode" to true)))
|
var rawConfig = getConfig("working-config.conf", ConfigFactory.parseMap(mapOf("devMode" to true)))
|
||||||
rawConfig = rawConfig.withValue("jmxReporterType", ConfigValueFactory.fromAnyRef("JOLOKIA"))
|
rawConfig = rawConfig.withValue("jmxReporterType", ConfigValueFactory.fromAnyRef("JOLOKIA"))
|
||||||
val nodeConfig = rawConfig.parseAsNodeConfiguration().orThrow()
|
val nodeConfig = rawConfig.parseAsNodeConfiguration().value()
|
||||||
assertTrue(JmxReporterType.JOLOKIA.toString() == nodeConfig.jmxReporterType.toString())
|
assertTrue(JmxReporterType.JOLOKIA.toString() == nodeConfig.jmxReporterType.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -694,7 +694,7 @@ class DriverDSLImpl(
|
|||||||
* Keeping [Config] around is needed as the user may specify extra config options not specified in [NodeConfiguration].
|
* Keeping [Config] around is needed as the user may specify extra config options not specified in [NodeConfiguration].
|
||||||
*/
|
*/
|
||||||
private class NodeConfig(val typesafe: Config) {
|
private class NodeConfig(val typesafe: Config) {
|
||||||
val corda: NodeConfiguration = typesafe.parseAsNodeConfiguration().orThrow()
|
val corda: NodeConfiguration = typesafe.parseAsNodeConfiguration().value()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -116,7 +116,7 @@ abstract class NodeBasedTest(private val cordappPackages: List<String> = emptyLi
|
|||||||
|
|
||||||
val specificConfig = config.withValue(NodeConfiguration.cordappDirectoriesKey, ConfigValueFactory.fromIterable(cordappDirectories.toSet()))
|
val specificConfig = config.withValue(NodeConfiguration.cordappDirectoriesKey, ConfigValueFactory.fromIterable(cordappDirectories.toSet()))
|
||||||
|
|
||||||
val parsedConfig = specificConfig.parseAsNodeConfiguration().orThrow()
|
val parsedConfig = specificConfig.parseAsNodeConfiguration().value()
|
||||||
|
|
||||||
defaultNetworkParameters.install(baseDirectory)
|
defaultNetworkParameters.install(baseDirectory)
|
||||||
val node = InProcessNode(parsedConfig, MOCK_VERSION_INFO.copy(platformVersion = platformVersion), flowManager = flowManager)
|
val node = InProcessNode(parsedConfig, MOCK_VERSION_INFO.copy(platformVersion = platformVersion), flowManager = flowManager)
|
||||||
|
@ -39,7 +39,7 @@ class NodeConfigTest {
|
|||||||
.withFallback(ConfigFactory.parseResources("reference.conf"))
|
.withFallback(ConfigFactory.parseResources("reference.conf"))
|
||||||
.withFallback(ConfigFactory.parseMap(mapOf("devMode" to true)))
|
.withFallback(ConfigFactory.parseMap(mapOf("devMode" to true)))
|
||||||
.resolve()
|
.resolve()
|
||||||
val fullConfig = nodeConfig.parseAsNodeConfiguration().orThrow()
|
val fullConfig = nodeConfig.parseAsNodeConfiguration().value()
|
||||||
|
|
||||||
// No custom configuration is created by default.
|
// No custom configuration is created by default.
|
||||||
assertFailsWith<ConfigException.Missing> { nodeConfig.getConfig("custom") }
|
assertFailsWith<ConfigException.Missing> { nodeConfig.getConfig("custom") }
|
||||||
|
@ -32,7 +32,7 @@ open class NodeBuilder {
|
|||||||
.withBaseDirectory(nodeDir)
|
.withBaseDirectory(nodeDir)
|
||||||
.exec(BuildImageResultCallback()).awaitImageId()
|
.exec(BuildImageResultCallback()).awaitImageId()
|
||||||
LOG.info("finished building docker image for: $nodeDir with id: $nodeImageId")
|
LOG.info("finished building docker image for: $nodeDir with id: $nodeImageId")
|
||||||
val config = nodeConfig.parseAsNodeConfigWithFallback(ConfigFactory.parseFile(copiedNode.configFile)).orThrow()
|
val config = nodeConfig.parseAsNodeConfigWithFallback(ConfigFactory.parseFile(copiedNode.configFile)).value()
|
||||||
return copiedNode.builtNode(config, nodeImageId)
|
return copiedNode.builtNode(config, nodeImageId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user