From 21bd21a6bb736fd829c94222ad5450d9b4ca2f00 Mon Sep 17 00:00:00 2001 From: Roman Gräf Date: Fri, 1 May 2020 00:14:07 +0200 Subject: Kotlin delegates --- README.md | 6 +++++- src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt | 4 ++++ src/test/kotlin/com/romangraef/jrconfig/MainKt.kt | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d5f1fa..ed46a2d 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,14 @@ public class ConfigurableClass1 { Kotlin: ```kotlin val someConfigProp = Config.get("someProp") +var someOtherOption by Config.get("someOtherOption") + fun main() { Config.use(FilePropertiesProvider.create("config.properties")) println(someConfigProp.get()) + println(someOtherOption) + someOtherOption = "lul" } ``` @@ -59,7 +63,7 @@ repositories { } dependencies { - implementation("com.github.romangraef", "jrconfig", "v0.3") + implementation("com.github.romangraef", "jrconfig", "v0.4") } ``` The version can be either a git shortref, or a [tag](https://github.com/romangraef/jrconfig/tags). diff --git a/src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt b/src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt index 5874e76..8a9d375 100644 --- a/src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt +++ b/src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt @@ -1,6 +1,10 @@ package com.romangraef.jrconfig +import kotlin.reflect.KProperty + interface ConfigVariable { fun get(): T fun set(value: T) + operator fun getValue(thisRef: Any?, property: KProperty<*>) = get() + operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = set(value) } \ No newline at end of file diff --git a/src/test/kotlin/com/romangraef/jrconfig/MainKt.kt b/src/test/kotlin/com/romangraef/jrconfig/MainKt.kt index 504b446..4fa593f 100644 --- a/src/test/kotlin/com/romangraef/jrconfig/MainKt.kt +++ b/src/test/kotlin/com/romangraef/jrconfig/MainKt.kt @@ -1,8 +1,11 @@ package com.romangraef.jrconfig val token = Config.get("token") +var someOtherOption by Config.get("someOtherOption") fun main() { Config.use(FilePropertiesProvider.create("config.properties")) println(token.get()) + println(someOtherOption) + someOtherOption = "lul" } \ No newline at end of file -- cgit