Skip to content

Commit

Permalink
changed the way values are retreived.
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail committed Oct 31, 2023
1 parent caed7bb commit c9ac1b6
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 400 deletions.
19 changes: 9 additions & 10 deletions Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ Supporting both Kotlin and Java.

For example, say you are making a discord bot and you want to store the token in a json file, you can do this:

Firstly, we need to create a config.json file in the root of your project. If you want you can change the files name,
but I will shows you how to do it with the default name.
<br>
1. create a config.json file in the root of your project. If you want you can change the files name, but I will shows
you how to do it with the default name.

```json
{
"token": "YOUR_TOKEN_HERE"
}
```

Secondly, we know need to get the token, so we will use JConfigUtils which has a standard builder.
<br>
2. We know need to get the token, so we will use JConfigUtils which has a standard builder.

```kotlin
val token = JConfigUtils.getString("token")
val token = JConfig.build()
.get("token")
.asText()
```

<br>
And thats it for getting the token.

## Installation
Expand All @@ -37,7 +36,7 @@ And thats it for getting the token.
<dependency>
<groupId>io.github.realyusufismail</groupId>
<artifactId>jconfig</artifactId>
<version>1.0.9</version>
<version>1.1.0</version>
</dependency>
```

Expand All @@ -46,11 +45,11 @@ And thats it for getting the token.
```groovy
//kotlin
dependencies {
implementation("io.github.realyusufismail:jconfig:1.0.9")
implementation("io.github.realyusufismail:jconfig:1.1.0")
}
//groovy
dependencies {
implementation "io.github.realyusufismail:jconfig:1.0.9"
implementation "io.github.realyusufismail:jconfig:1.1.0"
}
```
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ For example, say you are making a discord bot and you want to store the token in
2. We know need to get the token, so we will use JConfigUtils which has a standard builder.

```kotlin
val token = JConfigUtils.getString("token")
val token = JConfig.build()
.get("token")
.asText()
```

And thats it for getting the token.
Expand All @@ -37,7 +39,7 @@ And thats it for getting the token.
<dependency>
<groupId>io.github.realyusufismail</groupId>
<artifactId>jconfig</artifactId>
<version>1.0.9</version>
<version>1.1.0</version>
</dependency>
```

Expand All @@ -46,12 +48,12 @@ And thats it for getting the token.
```groovy
//kotlin
dependencies {
implementation("io.github.realyusufismail:jconfig:1.0.9")
implementation("io.github.realyusufismail:jconfig:1.1.0")
}
//groovy
dependencies {
implementation "io.github.realyusufismail:jconfig:1.0.9"
implementation "io.github.realyusufismail:jconfig:1.1.0"
}
```

Expand Down
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import org.jetbrains.dokka.base.DokkaBaseConfiguration
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript { dependencies { classpath("org.jetbrains.dokka:dokka-base:1.7.20") } }
buildscript { dependencies { classpath("org.jetbrains.dokka:dokka-base:1.9.10") } }

plugins {
kotlin("jvm") version "1.7.21"
id("com.diffplug.spotless") version "6.11.0"
id("org.jetbrains.dokka") version "1.7.20"
kotlin("jvm") version "1.9.10"
id("com.diffplug.spotless") version "6.22.0"
id("org.jetbrains.dokka") version "1.9.10"
application
`maven-publish`
signing
Expand All @@ -20,7 +20,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.8")
set("version", "1.1.0")
set("dev_id", "yusuf")
set("dev_name", "Yusuf Ismail")
set("dev_email", "yusufgamer222@gmail.com")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
version = 1.0.9
version = 1.1.0
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
7 changes: 4 additions & 3 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ done
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum

Expand Down Expand Up @@ -197,6 +194,10 @@ if "$cygwin" || "$msys" ; then
done
fi


# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
Expand Down
27 changes: 22 additions & 5 deletions src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package io.github.realyusufismail.jconfig

import com.fasterxml.jackson.databind.JsonNode
import io.github.realyusufismail.jconfig.classes.JConfigBuilder
import io.github.realyusufismail.jconfig.classes.JConfigException
import io.github.realyusufismail.jconfig.classes.JConfigObjectImpl
import io.github.realyusufismail.jconfig.classes.JsonEntry

/** Gets a value from the config.json file. Also creates a new JConfig instance. */
Expand All @@ -32,19 +34,32 @@ interface JConfig {
val entries: Set<JsonEntry>

/**
* Gets a set of JConfigObject with there associated keys.
* Gets the value of the key from the config file.
*
* @return A set of JConfigObject with there associated keys.
* @param key The key of the value.
*/
val values: Set<Map.Entry<String, JConfigObject>>
operator fun get(key: String): JsonNode?

/**
* Gets the value of the key from the config file.
*
* @param key The key of the value.
* @param defaultValue The default value to return if the key does not exist.
* @return The value of the key.
*/
operator fun get(key: String): JConfigObject?
operator fun get(key: String, defaultValue: Any): Any {
return get(key) ?: defaultValue
}

/**
* Gets the value of the key from the config file.
*
* @param key The key of the value.
* @return The value of the key.
*/
fun getAsJConfigObject(key: String): JConfigObject? {
return get(key)?.let { JConfigObjectImpl(it) }
}

/**
* Gets the value of the key from the config file.
Expand All @@ -53,7 +68,9 @@ 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): JConfigObject
fun getAsJConfigObject(key: String, defaultValue: Any): Any {
return getAsJConfigObject(key) ?: defaultValue
}

/**
* Used to set a value in the config file.
Expand Down
27 changes: 7 additions & 20 deletions src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package io.github.realyusufismail.jconfig

import com.fasterxml.jackson.databind.JsonNode
import io.github.realyusufismail.jconfig.classes.JsonEntry
import java.math.BigDecimal
import java.math.BigInteger
Expand Down Expand Up @@ -109,32 +110,18 @@ interface JConfigObject {
val asDecimal: BigDecimal

/**
* Get the value as an [Array] of [JConfigObject].
*
* @return The value as an [Array] of [JConfigObject].
*/
val asArray: Array<JConfigObject>

/**
* Get the value as a [List] of [JConfigObject].
*
* @return The value as a [List] of [JConfigObject].
*/
val asList: List<JConfigObject>

/**
* Get the value as a [Map] of [String] and [JConfigObject].
* Get the value as a [JsonEntry].
*
* @return The value as a [Map] of [String] and [JConfigObject].
* @return The value as a [JsonEntry].
*/
val asMap: Map<String, JConfigObject>
val asJsonEntry: JsonEntry

/**
* Get the value as a [JsonEntry].
* Gets the value as a [JsonNode].
*
* @return The value as a [JsonEntry].
* @return The value as a [JsonNode].
*/
val asJsonEntry: JsonEntry
val asJsonNode: JsonNode

/**
* Parses the value as a String.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,51 +37,28 @@ class JConfigImpl(entries: List<JsonEntry>) : JConfig {
override val entries: Set<JsonEntry>
get() = jsonEntries

override val values: Set<Map.Entry<String, JConfigObject>>
val values: Set<Map.Entry<String, JConfigObject>>
get() {
val map = HashMap<String, JConfigObject>()
for (entry in jsonEntries) {
map[entry.key] = get(entry.key)!!
map[entry.key] = getAsJConfigObject(entry.key)!!
}
return map.entries
}


override fun get(key: String): JConfigObject? {
override fun get(key: String): JsonNode? {
// 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())
value.isNull -> null
else ->
throw JConfigException("The key $key is not a valid type or is not supported")
}
return if (value is JsonNode) {
value
} else {
throw JConfigException("Unknown type: ${value.javaClass}")
null
}
}

override fun get(key: String, defaultValue: Any): JConfigObject {
return get(key) ?: JConfigObjectImpl(defaultValue)
}

override fun set(key: String, value: Any) {
mapEntries = mapEntries.toMutableMap().apply { put(key, value) }
jsonEntries = mapEntries.map { JsonEntry(it.key, it.value) }.toSet()
Expand Down
Loading

0 comments on commit c9ac1b6

Please sign in to comment.