From 740d41b361cfc7b9a096fdb580ba088425331920 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Sun, 11 Sep 2022 15:46:03 +0100 Subject: [PATCH] fixed an issue with JConfigUtils --- Package.md | 2 +- README.md | 2 +- build.gradle.kts | 4 +- .../realyusufismail/jconfig/JConfigObject.kt | 48 ++++++--- .../jconfig/classes/JConfigObjectImpl.kt | 84 +++++---------- .../jconfig/util/JConfigUtils.kt | 101 +++--------------- 6 files changed, 78 insertions(+), 163 deletions(-) diff --git a/Package.md b/Package.md index 90d192f..94ab2c0 100644 --- a/Package.md +++ b/Package.md @@ -37,7 +37,7 @@ And thats it for getting the token. io.github.realyusufismail jconfig - 1.0.4 + 1.0.5 ``` diff --git a/README.md b/README.md index 45e72c8..17e7820 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ And thats it for getting the token. io.github.realyusufismail jconfig - 1.0.4 + 1.0.5 ``` diff --git a/build.gradle.kts b/build.gradle.kts index 232c2ab..d31b245 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,7 +24,7 @@ extra.apply { set("name", "JConfig") set("description", "Json Configurations used to store tokens and other sensitive data") set("group", "io.github.realyusufismail") - set("version", "1.0.4") + set("version", "1.0.5") set("dev_id", "yusuf") set("dev_name", "Yusuf Ismail") set("dev_email", "yusufgamer222@gmail.com") @@ -37,7 +37,7 @@ extra.apply { } group = "io.github.realyusufismail" -version = "1.0.4" +version = "1.0.5" repositories { mavenCentral() diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt index dd48fde..8c78779 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt @@ -23,33 +23,51 @@ import java.math.BigInteger /** Used to get a value as a certain type. */ interface JConfigObject { - var string: String + /** Get the value as a [String]. */ + val string: String - var int: Int + /** Get the value as an [Int]. */ + val int: Int - var bigInt: BigInteger + /** Get the value as a [BigInteger]. */ + val bigInt: BigInteger - var double: Double + /** Get the value as a [Double]. */ + val double: Double - var boolean: Boolean + /** Get the value as a [Boolean]. */ + val boolean: Boolean - var byte: Byte + /** Get the value as a [Byte]. */ + val byte: Byte - var short: Short + /** Get the value as a [Short]. */ + val short: Short - var long: Long + /** Get the value as a [Long]. */ + val long: Long - var float: Float + /** Get the value as a [Float]. */ + val float: Float - var char: Char + /** Get the value as a [Char]. */ + val char: Char - var number: Number + /** Get the value as a [Number]. */ + val number: Number - var decimal: BigDecimal + /** Get the value as a [BigDecimal]. */ + val decimal: BigDecimal - var array: Array + /** Get the value as an [Array]. */ + val array: Array - var map: Map + /** Get the value as a [List]. */ + val list: List - var any: Any + /** Get the value as a [Map]. */ + val map: Map + + /** Get the value as an [Any]. */ + val any: Any } 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 2350b4b..6efe032 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigObjectImpl.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigObjectImpl.kt @@ -24,7 +24,7 @@ import java.math.BigInteger class JConfigObjectImpl(private val value: Any) : JConfigObject { - override var string: String + override val string: String get() { if (value is String) { return value @@ -32,11 +32,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast ${value::class.java} to String") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var int: Int + override val int: Int get() { if (value is Int) { return value @@ -44,11 +41,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Int") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var bigInt: BigInteger + override val bigInt: BigInteger get() { if (value is BigInteger) { return value @@ -56,11 +50,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to BigInteger") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var double: Double + override val double: Double get() { if (value is Double) { return value @@ -68,11 +59,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Double") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var boolean: Boolean + override val boolean: Boolean get() { if (value is Boolean) { return value @@ -80,11 +68,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Boolean") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var byte: Byte + override val byte: Byte get() { if (value is Byte) { return value @@ -92,11 +77,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Byte") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var short: Short + override val short: Short get() { if (value is Short) { return value @@ -104,11 +86,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Short") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var long: Long + override val long: Long get() { if (value is Long) { return value @@ -116,11 +95,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Long") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var float: Float + override val float: Float get() { if (value is Float) { return value @@ -128,11 +104,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Float") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var char: Char + override val char: Char get() { if (value is Char) { return value @@ -140,11 +113,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Char") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var number: Number + override val number: Number get() { if (value is Number) { return value @@ -152,11 +122,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Number") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var decimal: BigDecimal + override val decimal: BigDecimal get() { if (value is BigDecimal) { return value @@ -164,11 +131,8 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to BigDecimal") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var array: Array + override val array: Array get() { if (value is Array<*>) { return value @@ -178,11 +142,19 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Array") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") + + override val list: List + get() { + if (value is List<*>) { + return value.map { + it?.let { it1 -> JConfigObjectImpl(it1) } ?: JConfigObjectImpl("") + } + } else { + throw ClassCastException("Cannot cast $value to List") + } } - override var map: Map + override val map: Map get() { if (value is Map<*, *>) { return value @@ -196,15 +168,9 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject { throw ClassCastException("Cannot cast $value to Map") } } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } - override var any: Any + override val any: Any get() { return value } - set(value) { - throw UnsupportedOperationException("Cannot set value of JConfigObject") - } } diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/util/JConfigUtils.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/util/JConfigUtils.kt index 625bf01..01dc4e4 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/util/JConfigUtils.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/util/JConfigUtils.kt @@ -18,11 +18,8 @@ */ package io.github.realyusufismail.jconfig.util -import com.fasterxml.jackson.databind.node.* import io.github.realyusufismail.jconfig.JConfig.Companion.builder -import io.github.realyusufismail.jconfig.classes.JConfigException -// TODO: Might not work /** A utility class used to get values from the config file. */ class JConfigUtils { companion object { @@ -36,11 +33,7 @@ class JConfigUtils { */ @JvmStatic fun getString(key: String): String { - if (jConfig[key] is TextNode) { - return (jConfig[key] as TextNode).asText() - } else { - throw JConfigException("The value at the key $key is not a string.") - } + return jConfig[key].string } /** @@ -53,11 +46,7 @@ class JConfigUtils { */ @JvmStatic fun getString(key: String, defaultValue: String): String { - return if (jConfig[key] is TextNode) { - (jConfig[key] as TextNode).asText() - } else { - defaultValue - } + return jConfig[key, defaultValue].string } /** @@ -68,11 +57,7 @@ class JConfigUtils { */ @JvmStatic fun getInt(key: String): Int { - return if (jConfig[key] is IntNode) { - (jConfig[key] as IntNode).asInt() - } else { - throw JConfigException("The value at the key $key is not an integer.") - } + return jConfig[key].int } /** @@ -84,11 +69,7 @@ class JConfigUtils { */ @JvmStatic fun getInt(key: String, defaultValue: Int): Int { - return if (jConfig[key] is IntNode) { - (jConfig[key] as IntNode).intValue() - } else { - defaultValue - } + return jConfig[key, defaultValue].int } /** @@ -99,11 +80,7 @@ class JConfigUtils { */ @JvmStatic fun getBoolean(key: String): Boolean { - return if (jConfig[key] is BooleanNode) { - (jConfig[key] as BooleanNode).booleanValue() - } else { - throw JConfigException("The value at the key $key is not a boolean.") - } + return jConfig[key].boolean } /** @@ -116,11 +93,7 @@ class JConfigUtils { */ @JvmStatic fun getBoolean(key: String, defaultValue: Boolean): Boolean { - return if (jConfig[key, defaultValue] is BooleanNode) { - (jConfig[key, defaultValue] as BooleanNode).booleanValue() - } else { - defaultValue - } + return jConfig[key, defaultValue].boolean } /** @@ -131,11 +104,7 @@ class JConfigUtils { */ @JvmStatic fun getDouble(key: String): Double { - return if (jConfig[key] is DoubleNode) { - (jConfig[key] as DoubleNode).doubleValue() - } else { - throw JConfigException("The value at the key $key is not a double.") - } + return jConfig[key].double } /** @@ -148,11 +117,7 @@ class JConfigUtils { */ @JvmStatic fun getDouble(key: String, defaultValue: Double): Double { - return if (jConfig[key] is DoubleNode) { - (jConfig[key] as DoubleNode).doubleValue() - } else { - defaultValue - } + return jConfig[key, defaultValue].double } /** @@ -163,11 +128,7 @@ class JConfigUtils { */ @JvmStatic fun getLong(key: String): Long { - return if (jConfig[key] is LongNode) { - (jConfig[key] as LongNode).longValue() - } else { - throw JConfigException("The value at the key $key is not a long.") - } + return jConfig[key].long } /** @@ -179,11 +140,7 @@ class JConfigUtils { */ @JvmStatic fun getLong(key: String, defaultValue: Long): Long { - return if (jConfig[key] is LongNode) { - (jConfig[key] as LongNode).longValue() - } else { - defaultValue - } + return jConfig[key, defaultValue].long } /** @@ -194,11 +151,7 @@ class JConfigUtils { */ @JvmStatic fun getFloat(key: String): Float { - return if (jConfig[key] is FloatNode) { - (jConfig[key] as FloatNode).floatValue() - } else { - throw JConfigException("The value at the key $key is not a float.") - } + return jConfig[key].float } /** @@ -210,11 +163,7 @@ class JConfigUtils { */ @JvmStatic fun getFloat(key: String, defaultValue: Float): Float { - return if (jConfig[key, defaultValue] is FloatNode) { - (jConfig[key, defaultValue] as FloatNode).floatValue() - } else { - defaultValue - } + return jConfig[key, defaultValue].float } /** @@ -225,11 +174,7 @@ class JConfigUtils { */ @JvmStatic fun getListNode(key: String): List<*> { - return if (jConfig[key] is ArrayNode) { - (jConfig[key] as ArrayNode).toList() - } else { - throw JConfigException("The value at the key $key is not a list.") - } + return jConfig[key].list } /** @@ -241,11 +186,7 @@ class JConfigUtils { */ @JvmStatic fun getListNode(key: String, defaultValue: List<*>): List<*> { - return if (jConfig[key, defaultValue] is ArrayNode) { - (jConfig[key, defaultValue] as ArrayNode).toList() - } else { - defaultValue - } + return jConfig[key, defaultValue].list } /** @@ -256,11 +197,7 @@ class JConfigUtils { */ @JvmStatic fun getMapNode(key: String): Map<*, *> { - return if (jConfig[key] is ObjectNode) { - (jConfig[key] as ObjectNode).fields().asSequence().associate { it.key to it.value } - } else { - throw JConfigException("The value at the key $key is not a map.") - } + return jConfig[key].map } /** @@ -272,13 +209,7 @@ class JConfigUtils { */ @JvmStatic fun getMapNode(key: String, defaultValue: Map<*, *>): Map<*, *> { - return if (jConfig[key, defaultValue] is ObjectNode) { - (jConfig[key, defaultValue] as ObjectNode).fields().asSequence().associate { - it.key to it.value - } - } else { - defaultValue - } + return jConfig[key, defaultValue].map } /**