From caed7bb51c37069053994525438f8304f6fc9571 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Sat, 24 Jun 2023 10:47:03 +0100 Subject: [PATCH] Added ability to set environment variables (#38) --- Package.md | 6 +++--- README.md | 6 +++--- build.gradle.kts | 2 +- gradle.properties | 2 +- gradle/wrapper/gradle-wrapper.properties | 3 +-- .../io/github/realyusufismail/jconfig/JConfig.kt | 11 +++++++++-- .../realyusufismail/jconfig/JConfigObject.kt | 7 +++++++ .../jconfig/classes/JConfigBuilder.kt | 16 +++++++++++++++- .../jconfig/classes/JConfigImpl.kt | 12 ++++++++++++ .../jconfig/classes/JConfigObjectImpl.kt | 12 ++++++++++++ 10 files changed, 64 insertions(+), 13 deletions(-) diff --git a/Package.md b/Package.md index ff8fc1e..fa1051c 100644 --- a/Package.md +++ b/Package.md @@ -37,7 +37,7 @@ And thats it for getting the token. io.github.realyusufismail jconfig - 1.0.7 + 1.0.9 ``` @@ -46,11 +46,11 @@ And thats it for getting the token. ```groovy //kotlin dependencies { - implementation("io.github.realyusufismail:jconfig:1.0.7") + implementation("io.github.realyusufismail:jconfig:1.0.9") } //groovy dependencies { - implementation "io.github.realyusufismail:jconfig:1.0.7" + implementation "io.github.realyusufismail:jconfig:1.0.9" } ``` \ No newline at end of file diff --git a/README.md b/README.md index d5fadd7..6841315 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ And thats it for getting the token. io.github.realyusufismail jconfig - 1.0.8 + 1.0.9 ``` @@ -46,12 +46,12 @@ And thats it for getting the token. ```groovy //kotlin dependencies { - implementation("io.github.realyusufismail:jconfig:1.0.8") + implementation("io.github.realyusufismail:jconfig:1.0.9") } //groovy dependencies { - implementation "io.github.realyusufismail:jconfig:1.0.8" + implementation "io.github.realyusufismail:jconfig:1.0.9" } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 28efcad..5bed36a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,7 +42,7 @@ repositories { mavenCentral() } dependencies { // json - implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.0") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.1") testImplementation(kotlin("test")) } diff --git a/gradle.properties b/gradle.properties index 91f288e..a553f21 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ kotlin.code.style=official -version = 1.0.9-SNAPSHOT +version = 1.0.9 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index c9b0f7a..37aef8d 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,7 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionSha256Sum=4159b938ec734a8388ce03f52aa8f3c7ed0d31f5438622545de4f83a89b79788 -distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt index 6ae1579..f536fb5 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt @@ -22,15 +22,22 @@ import io.github.realyusufismail.jconfig.classes.JConfigBuilder import io.github.realyusufismail.jconfig.classes.JConfigException import io.github.realyusufismail.jconfig.classes.JsonEntry -/** Used to get a value from the config.json file. Also creates a new JConfig instance. */ +/** Gets a value from the config.json file. Also creates a new JConfig instance. */ interface JConfig { /** - * Used to get all the entries in the config file. + * Gets all the entries in the config file. * * @return A list of all the entries in the config file. */ val entries: Set + /** + * Gets a set of JConfigObject with there associated keys. + * + * @return A set of JConfigObject with there associated keys. + */ + val values: Set> + /** * Gets the value of the key from the config file. * diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt index 1ebc3ca..2d8bac4 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt @@ -135,4 +135,11 @@ interface JConfigObject { * @return The value as a [JsonEntry]. */ val asJsonEntry: JsonEntry + + /** + * Parses the value as a String. + * + * @return The value as a String. + */ + val parseAsString: String } diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigBuilder.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigBuilder.kt index af5c4db..2568e16 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigBuilder.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigBuilder.kt @@ -28,6 +28,7 @@ class JConfigBuilder { private var extension = ".json" private var filename = "config$extension" private var directoryPath = "./" + private var environmentVariable = false fun setFilename(filename: String): JConfigBuilder { this.filename = filename + extension @@ -44,6 +45,11 @@ class JConfigBuilder { return this } + fun enableEnvironmentVariable(): JConfigBuilder { + this.environmentVariable = true + return this + } + @Throws(JConfigException::class) fun build(): JConfig { val mapper = ObjectMapper() @@ -73,7 +79,15 @@ class JConfigBuilder { entries.add(JsonEntry(key, value)) } - JConfigImpl(entries) + val config = JConfigImpl(entries) + + if (environmentVariable) { + config.values.forEach { (key, value) -> + System.setProperty(key, value.parseAsString) + } + } + + config } catch (e: IOException) { throw JConfigException("Could not read the config file.", e) } diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt index c9206c3..bf424bb 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt @@ -27,14 +27,26 @@ class JConfigImpl(entries: List) : JConfig { private var mapEntries: Map = HashMap() private var jsonEntries: Set = HashSet() + init { this.mapEntries = JsonEntry.toMap(entries) jsonEntries = this.mapEntries.map { JsonEntry(it.key, it.value) }.toSet() + } override val entries: Set get() = jsonEntries + override val values: Set> + get() { + val map = HashMap() + for (entry in jsonEntries) { + map[entry.key] = get(entry.key)!! + } + return map.entries + } + + override fun get(key: String): JConfigObject? { // mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key") diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigObjectImpl.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigObjectImpl.kt index 1f71c19..24e9ace 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigObjectImpl.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigObjectImpl.kt @@ -171,4 +171,16 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { override val asJsonEntry: JsonEntry get() = value as JsonEntry + + + override val parseAsString: String + get() = when (value) { + is String -> value + is Number -> value.toString() + is Boolean -> value.toString() + is Char -> value.toString() + is JsonEntry -> value.toString() + else -> throw ClassCastException("Cannot cast $value to String") + } + }