Specication update

This commit is contained in:
Alexander Nozik 2019-04-30 14:28:45 +03:00
parent 6298d741ff
commit d23c0d3781
4 changed files with 76 additions and 15 deletions

View File

@ -46,7 +46,11 @@ allprojects {
group = "hep.dataforge" group = "hep.dataforge"
version = "0.1.2-dev-5" version = "0.1.2-dev-5"
}
subprojects {
if(name.startsWith("dataforge")){
// apply bintray configuration // apply bintray configuration
apply(from = "${rootProject.rootDir}/gradle/bintray.gradle") apply(from = "${rootProject.rootDir}/gradle/bintray.gradle")
@ -55,8 +59,6 @@ allprojects {
} }
subprojects {
// dokka { // dokka {
// outputFormat = "html" // outputFormat = "html"
// outputDirectory = javadoc.destinationDir // outputDirectory = javadoc.destinationDir

View File

@ -22,9 +22,23 @@ fun Configurable.boolean(default: Boolean? = null, key: String? = null) =
fun Configurable.number(default: Number? = null, key: String? = null) = fun Configurable.number(default: Number? = null, key: String? = null) =
MutableNumberDelegate(config, key, default) MutableNumberDelegate(config, key, default)
fun Configurable.node(key: String? = null) = MutableNodeDelegate(config, key) /* Number delegates*/
fun Configurable.int(default: Int? = null, key: String? = null) =
number(default, key).int
fun Configurable.double(default: Double? = null, key: String? = null) =
number(default, key).double
fun Configurable.long(default: Long? = null, key: String? = null) =
number(default, key).long
fun Configurable.short(default: Short? = null, key: String? = null) =
number(default, key).short
fun Configurable.float(default: Float? = null, key: String? = null) =
number(default, key).float
//fun <T : Configurable> Configurable.spec(spec: Specification<T>, key: String? = null) = ChildConfigDelegate<T>(key) { spec.wrap(this) }
@JvmName("safeString") @JvmName("safeString")
fun Configurable.string(default: String, key: String? = null) = fun Configurable.string(default: String, key: String? = null) =
@ -51,5 +65,38 @@ fun Configurable.number(key: String? = null, default: () -> Number) =
MutableSafeNumberDelegate(config, key, default) MutableSafeNumberDelegate(config, key, default)
/* Safe number delegates*/
@JvmName("safeInt")
fun Configurable.int(default: Int, key: String? = null) =
number(default, key).int
@JvmName("safeDouble")
fun Configurable.double(default: Double, key: String? = null) =
number(default, key).double
@JvmName("safeLong")
fun Configurable.long(default: Long, key: String? = null) =
number(default, key).long
@JvmName("safeShort")
fun Configurable.short(default: Short, key: String? = null) =
number(default, key).short
@JvmName("safeFloat")
fun Configurable.float(default: Float, key: String? = null) =
number(default, key).float
/**
* Enum delegate
*/
inline fun <reified E : Enum<E>> Configurable.enum(default: E, key: String? = null) = inline fun <reified E : Enum<E>> Configurable.enum(default: E, key: String? = null) =
MutableSafeEnumvDelegate(config, key, default) { enumValueOf(it) } MutableSafeEnumvDelegate(config, key, default) { enumValueOf(it) }
fun Configurable.node(key: String? = null) = MutableNodeDelegate(config, key)
fun <T : Specification> Configurable.spec(spec: SpecificationCompanion<T>, key: String? = null) =
MutableMorphDelegate(config, key) { spec.wrap(it) }
fun <T : Specification> Configurable.spec(builder: (Config) -> T, key: String? = null) =
MutableMorphDelegate(config, key) { specification(builder).wrap(it) }

View File

@ -59,5 +59,4 @@ fun <C : Specification, S : SpecificationCompanion<C>> S.createStyle(action: C.(
fun <C : Specification> Specification.spec( fun <C : Specification> Specification.spec(
spec: SpecificationCompanion<C>, spec: SpecificationCompanion<C>,
key: String? = null key: String? = null
) = ) = MutableMorphDelegate(config, key) { spec.wrap(it) }
MutableMorphDelegate(config, key) { spec.wrap(config) }

View File

@ -12,17 +12,30 @@ class MetaDelegateTest {
@Test @Test
fun delegateTest() { fun delegateTest() {
class InnerSpec(override val config: Config) : Specification {
var innerValue by string()
}
val innerSpec = specification(::InnerSpec)
val testObject = object : Specification { val testObject = object : Specification {
override val config: Config = Config() override val config: Config = Config()
var myValue by config.string() var myValue by string()
var safeValue by config.number(2.2) var safeValue by double(2.2)
var enumValue by config.enum(TestEnum.YES) var enumValue by config.enum(TestEnum.YES)
var inner by spec(innerSpec)
} }
testObject.config["myValue"] = "theString" testObject.config["myValue"] = "theString"
testObject.enumValue = TestEnum.NO testObject.enumValue = TestEnum.NO
testObject.inner = innerSpec.build { innerValue = "ddd"}
assertEquals("theString", testObject.myValue) assertEquals("theString", testObject.myValue)
assertEquals(TestEnum.NO, testObject.enumValue) assertEquals(TestEnum.NO, testObject.enumValue)
assertEquals(2.2, testObject.safeValue) assertEquals(2.2, testObject.safeValue)
assertEquals("ddd", testObject.inner?.innerValue)
} }
} }