Skip to content

Commit

Permalink
1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail committed Sep 11, 2022
1 parent eb1cff0 commit 2c4fe8d
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ And thats it for getting the token.
<dependency>
<groupId>io.github.realyusufismail</groupId>
<artifactId>jconfig</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ And thats it for getting the token.
<dependency>
<groupId>io.github.realyusufismail</groupId>
<artifactId>jconfig</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extra.apply {
}

group = "io.github.realyusufismail"
version = "1.0.3"
version = "1.0.4"

repositories {
mavenCentral()
Expand Down
15 changes: 13 additions & 2 deletions src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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.
Expand All @@ -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 {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,38 @@
*/
package io.github.realyusufismail.jconfig

class JConfigImpl(entries: List<JsonEntry>) : JConfig {
import java.math.BigDecimal
import java.math.BigInteger

private var mapEntries: Map<String, Any> = HashMap()
private var jsonEntries: Set<JsonEntry> = 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<JsonEntry>
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<JConfigObject>

var map: Map<String, JConfigObject>

var any: Any
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<JsonEntry>) : JConfig {

private var mapEntries: Map<String, Any> = HashMap()
private var jsonEntries: Set<JsonEntry> = HashSet()

init {
this.mapEntries = JsonEntry.toMap(entries)
jsonEntries = this.mapEntries.map { JsonEntry(it.key, it.value) }.toSet()
}

override val entries: Set<JsonEntry>
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)
}
}
Loading

0 comments on commit 2c4fe8d

Please sign in to comment.