From 48747e0bf123f4514d72a36baa6d64fa4405b479 Mon Sep 17 00:00:00 2001 From: Yusuf Arfan Ismail Date: Sun, 11 Sep 2022 13:55:45 +0100 Subject: [PATCH] 1.0.4 --- Package.md | 2 +- README.md | 2 +- build.gradle.kts | 4 +- .../github/realyusufismail/jconfig/JConfig.kt | 15 +- .../{JConfigImpl.kt => JConfigObject.kt} | 44 ++-- .../jconfig/{ => classes}/JConfigBuilder.kt | 3 +- .../jconfig/{ => classes}/JConfigException.kt | 2 +- .../jconfig/classes/JConfigImpl.kt | 78 +++++++ .../jconfig/classes/JConfigObjectImpl.kt | 210 ++++++++++++++++++ .../jconfig/{ => classes}/JsonEntry.kt | 2 +- .../jconfig/{ => util}/JConfigUtils.kt | 4 +- 11 files changed, 341 insertions(+), 25 deletions(-) rename src/main/kotlin/io/github/realyusufismail/jconfig/{JConfigImpl.kt => JConfigObject.kt} (51%) rename src/main/kotlin/io/github/realyusufismail/jconfig/{ => classes}/JConfigBuilder.kt (94%) rename src/main/kotlin/io/github/realyusufismail/jconfig/{ => classes}/JConfigException.kt (94%) create mode 100644 src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt create mode 100644 src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigObjectImpl.kt rename src/main/kotlin/io/github/realyusufismail/jconfig/{ => classes}/JsonEntry.kt (97%) rename src/main/kotlin/io/github/realyusufismail/jconfig/{ => util}/JConfigUtils.kt (98%) diff --git a/Package.md b/Package.md index a90850f..90d192f 100644 --- a/Package.md +++ b/Package.md @@ -37,7 +37,7 @@ And thats it for getting the token. io.github.realyusufismail jconfig - 1.0.3 + 1.0.4 ``` diff --git a/README.md b/README.md index 3f933e2..45e72c8 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ And thats it for getting the token. io.github.realyusufismail jconfig - 1.0.3 + 1.0.4 ``` diff --git a/build.gradle.kts b/build.gradle.kts index 0811a2b..232c2ab 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.3") + set("version", "1.0.4") 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.3" +version = "1.0.4" repositories { mavenCentral() diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt index 0f136dc..8c98c6e 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt @@ -18,6 +18,9 @@ */ package io.github.realyusufismail.jconfig +import io.github.realyusufismail.jconfig.classes.JConfigBuilder +import io.github.realyusufismail.jconfig.classes.JsonEntry + /** Used to get a value from the config.json file. Also creates a new JConfig instance. */ interface JConfig { /** @@ -33,7 +36,7 @@ interface JConfig { * @param key The key of the value. * @return The value of the key. */ - operator fun get(key: String): Any + operator fun get(key: String): JConfigObject /** * Gets the value of the key from the config file. @@ -42,7 +45,15 @@ interface JConfig { * @param defaultValue The default value to return if the key does not exist. * @return The value of the key. */ - operator fun get(key: String, defaultValue: Any): Any + operator fun get(key: String, defaultValue: Any): JConfigObject + + /** + * Used to check if the value is present + * + * @param key The key of the value. + * @return True if the value is present, false otherwise. + */ + operator fun contains(key: String): Boolean companion object { /** diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigImpl.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt similarity index 51% rename from src/main/kotlin/io/github/realyusufismail/jconfig/JConfigImpl.kt rename to src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt index 1e49875..dd48fde 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigImpl.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt @@ -18,24 +18,38 @@ */ package io.github.realyusufismail.jconfig -class JConfigImpl(entries: List) : JConfig { +import java.math.BigDecimal +import java.math.BigInteger - private var mapEntries: Map = HashMap() - private var jsonEntries: Set = HashSet() +/** Used to get a value as a certain type. */ +interface JConfigObject { + var string: String - init { - this.mapEntries = JsonEntry.toMap(entries) - jsonEntries = this.mapEntries.map { JsonEntry(it.key, it.value) }.toSet() - } + var int: Int - override val entries: Set - get() = jsonEntries + var bigInt: BigInteger - override fun get(key: String): Any { - return mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key") - } + var double: Double - override fun get(key: String, defaultValue: Any): Any { - return mapEntries[key] ?: defaultValue - } + var boolean: Boolean + + var byte: Byte + + var short: Short + + var long: Long + + var float: Float + + var char: Char + + var number: Number + + var decimal: BigDecimal + + var array: Array + + var map: Map + + var any: Any } diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigBuilder.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigBuilder.kt similarity index 94% rename from src/main/kotlin/io/github/realyusufismail/jconfig/JConfigBuilder.kt rename to src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigBuilder.kt index 84db386..1571c05 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigBuilder.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigBuilder.kt @@ -16,9 +16,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.realyusufismail.jconfig +package io.github.realyusufismail.jconfig.classes import com.fasterxml.jackson.databind.ObjectMapper +import io.github.realyusufismail.jconfig.JConfig import java.io.File import java.io.IOException diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigException.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigException.kt similarity index 94% rename from src/main/kotlin/io/github/realyusufismail/jconfig/JConfigException.kt rename to src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigException.kt index f468ffa..d661cee 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigException.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigException.kt @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.realyusufismail.jconfig +package io.github.realyusufismail.jconfig.classes import java.io.IOException diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt new file mode 100644 index 0000000..6845847 --- /dev/null +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigImpl.kt @@ -0,0 +1,78 @@ +/* + * Copyright 2022 Yusuf Arfan Ismail (RealYusufIsmail) + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.realyusufismail.jconfig.classes + +import com.fasterxml.jackson.databind.JsonNode +import io.github.realyusufismail.jconfig.JConfig +import io.github.realyusufismail.jconfig.JConfigObject + +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 fun get(key: String): JConfigObject { + // mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key") + + val value = + mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key") + + if (value is JsonNode) { + return when { + value.isInt -> JConfigObjectImpl(value.asInt()) + value.isLong -> JConfigObjectImpl(value.asLong()) + value.isDouble -> JConfigObjectImpl(value.asDouble()) + value.isBoolean -> JConfigObjectImpl(value.asBoolean()) + value.isTextual -> JConfigObjectImpl(value.asText()) + value.isArray -> JConfigObjectImpl(value.map { it.asText() }) + value.isBigDecimal -> JConfigObjectImpl(value.decimalValue()) + value.isBigInteger -> JConfigObjectImpl(value.bigIntegerValue()) + value.isFloat -> JConfigObjectImpl(value.floatValue()) + value.isShort -> JConfigObjectImpl(value.shortValue()) + value.isBinary -> JConfigObjectImpl(value.binaryValue()) + value.isObject -> + JConfigObjectImpl( + value.fields().asSequence().map { it.key to it.value.asText() }.toMap()) + else -> throw IllegalArgumentException("Unknown type: ${value.javaClass}") + } + } else { + throw IllegalArgumentException("Unknown type: ${value.javaClass}") + } + } + + override fun get(key: String, defaultValue: Any): JConfigObject { + return try { + get(key) + } catch (e: NoSuchElementException) { + JConfigObjectImpl(defaultValue) + } + } + + override fun contains(key: String): Boolean { + return mapEntries.containsKey(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 new file mode 100644 index 0000000..2350b4b --- /dev/null +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigObjectImpl.kt @@ -0,0 +1,210 @@ +/* + * Copyright 2022 Yusuf Arfan Ismail (RealYusufIsmail) + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.realyusufismail.jconfig.classes + +import io.github.realyusufismail.jconfig.JConfigObject +import java.math.BigDecimal +import java.math.BigInteger + +class JConfigObjectImpl(private val value: Any) : JConfigObject { + + override var string: String + get() { + if (value is String) { + return value + } else { + throw ClassCastException("Cannot cast ${value::class.java} to String") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var int: Int + get() { + if (value is Int) { + return value + } else { + throw ClassCastException("Cannot cast $value to Int") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var bigInt: BigInteger + get() { + if (value is BigInteger) { + return value + } else { + throw ClassCastException("Cannot cast $value to BigInteger") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var double: Double + get() { + if (value is Double) { + return value + } else { + throw ClassCastException("Cannot cast $value to Double") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var boolean: Boolean + get() { + if (value is Boolean) { + return value + } else { + throw ClassCastException("Cannot cast $value to Boolean") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var byte: Byte + get() { + if (value is Byte) { + return value + } else { + throw ClassCastException("Cannot cast $value to Byte") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var short: Short + get() { + if (value is Short) { + return value + } else { + throw ClassCastException("Cannot cast $value to Short") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var long: Long + get() { + if (value is Long) { + return value + } else { + throw ClassCastException("Cannot cast $value to Long") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var float: Float + get() { + if (value is Float) { + return value + } else { + throw ClassCastException("Cannot cast $value to Float") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var char: Char + get() { + if (value is Char) { + return value + } else { + throw ClassCastException("Cannot cast $value to Char") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var number: Number + get() { + if (value is Number) { + return value + } else { + throw ClassCastException("Cannot cast $value to Number") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var decimal: BigDecimal + get() { + if (value is BigDecimal) { + return value + } else { + throw ClassCastException("Cannot cast $value to BigDecimal") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var array: Array + get() { + if (value is Array<*>) { + return value + .map { it?.let { it1 -> JConfigObjectImpl(it1) } ?: JConfigObjectImpl("") } + .toTypedArray() + } else { + throw ClassCastException("Cannot cast $value to Array") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var map: Map + get() { + if (value is Map<*, *>) { + return value + .map { + it.key.toString() to + (it.value?.let { it1 -> JConfigObjectImpl(it1) } + ?: JConfigObjectImpl("")) + } + .toMap() + } else { + throw ClassCastException("Cannot cast $value to Map") + } + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } + + override var any: Any + get() { + return value + } + set(value) { + throw UnsupportedOperationException("Cannot set value of JConfigObject") + } +} diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JsonEntry.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JsonEntry.kt similarity index 97% rename from src/main/kotlin/io/github/realyusufismail/jconfig/JsonEntry.kt rename to src/main/kotlin/io/github/realyusufismail/jconfig/classes/JsonEntry.kt index 16e3a44..2fb0bb5 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JsonEntry.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/classes/JsonEntry.kt @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.realyusufismail.jconfig +package io.github.realyusufismail.jconfig.classes import java.util.* import java.util.stream.Collectors diff --git a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigUtils.kt b/src/main/kotlin/io/github/realyusufismail/jconfig/util/JConfigUtils.kt similarity index 98% rename from src/main/kotlin/io/github/realyusufismail/jconfig/JConfigUtils.kt rename to src/main/kotlin/io/github/realyusufismail/jconfig/util/JConfigUtils.kt index fb936d8..625bf01 100644 --- a/src/main/kotlin/io/github/realyusufismail/jconfig/JConfigUtils.kt +++ b/src/main/kotlin/io/github/realyusufismail/jconfig/util/JConfigUtils.kt @@ -16,11 +16,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.realyusufismail.jconfig +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 {