Skip to content

Commit 2c4fe8d

Browse files
1.0.4
1 parent eb1cff0 commit 2c4fe8d

File tree

11 files changed

+340
-24
lines changed

11 files changed

+340
-24
lines changed

Package.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ And thats it for getting the token.
3737
<dependency>
3838
<groupId>io.github.realyusufismail</groupId>
3939
<artifactId>jconfig</artifactId>
40-
<version>1.0.3</version>
40+
<version>1.0.4</version>
4141
</dependency>
4242
```
4343

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ And thats it for getting the token.
3939
<dependency>
4040
<groupId>io.github.realyusufismail</groupId>
4141
<artifactId>jconfig</artifactId>
42-
<version>1.0.3</version>
42+
<version>1.0.4</version>
4343
</dependency>
4444
```
4545

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extra.apply {
3737
}
3838

3939
group = "io.github.realyusufismail"
40-
version = "1.0.3"
40+
version = "1.0.4"
4141

4242
repositories {
4343
mavenCentral()

src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package io.github.realyusufismail.jconfig
2020

21+
import io.github.realyusufismail.jconfig.classes.JConfigBuilder
22+
import io.github.realyusufismail.jconfig.classes.JsonEntry
23+
2124
/** Used to get a value from the config.json file. Also creates a new JConfig instance. */
2225
interface JConfig {
2326
/**
@@ -33,7 +36,7 @@ interface JConfig {
3336
* @param key The key of the value.
3437
* @return The value of the key.
3538
*/
36-
operator fun get(key: String): Any
39+
operator fun get(key: String): JConfigObject
3740

3841
/**
3942
* Gets the value of the key from the config file.
@@ -42,7 +45,15 @@ interface JConfig {
4245
* @param defaultValue The default value to return if the key does not exist.
4346
* @return The value of the key.
4447
*/
45-
operator fun get(key: String, defaultValue: Any): Any
48+
operator fun get(key: String, defaultValue: Any): JConfigObject
49+
50+
/**
51+
* Used to check if the value is present
52+
*
53+
* @param key The key of the value.
54+
* @return True if the value is present, false otherwise.
55+
*/
56+
operator fun contains(key: String): Boolean
4657

4758
companion object {
4859
/**

src/main/kotlin/io/github/realyusufismail/jconfig/JConfigImpl.kt renamed to src/main/kotlin/io/github/realyusufismail/jconfig/JConfigObject.kt

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,38 @@
1818
*/
1919
package io.github.realyusufismail.jconfig
2020

21-
class JConfigImpl(entries: List<JsonEntry>) : JConfig {
21+
import java.math.BigDecimal
22+
import java.math.BigInteger
2223

23-
private var mapEntries: Map<String, Any> = HashMap()
24-
private var jsonEntries: Set<JsonEntry> = HashSet()
24+
/** Used to get a value as a certain type. */
25+
interface JConfigObject {
26+
var string: String
2527

26-
init {
27-
this.mapEntries = JsonEntry.toMap(entries)
28-
jsonEntries = this.mapEntries.map { JsonEntry(it.key, it.value) }.toSet()
29-
}
28+
var int: Int
3029

31-
override val entries: Set<JsonEntry>
32-
get() = jsonEntries
30+
var bigInt: BigInteger
3331

34-
override fun get(key: String): Any {
35-
return mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key")
36-
}
32+
var double: Double
3733

38-
override fun get(key: String, defaultValue: Any): Any {
39-
return mapEntries[key] ?: defaultValue
40-
}
34+
var boolean: Boolean
35+
36+
var byte: Byte
37+
38+
var short: Short
39+
40+
var long: Long
41+
42+
var float: Float
43+
44+
var char: Char
45+
46+
var number: Number
47+
48+
var decimal: BigDecimal
49+
50+
var array: Array<JConfigObject>
51+
52+
var map: Map<String, JConfigObject>
53+
54+
var any: Any
4155
}

src/main/kotlin/io/github/realyusufismail/jconfig/JConfigBuilder.kt renamed to src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigBuilder.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package io.github.realyusufismail.jconfig
19+
package io.github.realyusufismail.jconfig.classes
2020

2121
import com.fasterxml.jackson.databind.ObjectMapper
22+
import io.github.realyusufismail.jconfig.JConfig
2223
import java.io.File
2324
import java.io.IOException
2425

src/main/kotlin/io/github/realyusufismail/jconfig/JConfigException.kt renamed to src/main/kotlin/io/github/realyusufismail/jconfig/classes/JConfigException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package io.github.realyusufismail.jconfig
19+
package io.github.realyusufismail.jconfig.classes
2020

2121
import java.io.IOException
2222

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2022 Yusuf Arfan Ismail (RealYusufIsmail)
3+
*
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
*
7+
* you may not use this file except in compliance with the License.
8+
*
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package io.github.realyusufismail.jconfig.classes
20+
21+
import com.fasterxml.jackson.databind.JsonNode
22+
import io.github.realyusufismail.jconfig.JConfig
23+
import io.github.realyusufismail.jconfig.JConfigObject
24+
25+
class JConfigImpl(entries: List<JsonEntry>) : JConfig {
26+
27+
private var mapEntries: Map<String, Any> = HashMap()
28+
private var jsonEntries: Set<JsonEntry> = HashSet()
29+
30+
init {
31+
this.mapEntries = JsonEntry.toMap(entries)
32+
jsonEntries = this.mapEntries.map { JsonEntry(it.key, it.value) }.toSet()
33+
}
34+
35+
override val entries: Set<JsonEntry>
36+
get() = jsonEntries
37+
38+
override fun get(key: String): JConfigObject {
39+
// mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key")
40+
41+
val value =
42+
mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key")
43+
44+
if (value is JsonNode) {
45+
return when {
46+
value.isInt -> JConfigObjectImpl(value.asInt())
47+
value.isLong -> JConfigObjectImpl(value.asLong())
48+
value.isDouble -> JConfigObjectImpl(value.asDouble())
49+
value.isBoolean -> JConfigObjectImpl(value.asBoolean())
50+
value.isTextual -> JConfigObjectImpl(value.asText())
51+
value.isArray -> JConfigObjectImpl(value.map { it.asText() })
52+
value.isBigDecimal -> JConfigObjectImpl(value.decimalValue())
53+
value.isBigInteger -> JConfigObjectImpl(value.bigIntegerValue())
54+
value.isFloat -> JConfigObjectImpl(value.floatValue())
55+
value.isShort -> JConfigObjectImpl(value.shortValue())
56+
value.isBinary -> JConfigObjectImpl(value.binaryValue())
57+
value.isObject ->
58+
JConfigObjectImpl(
59+
value.fields().asSequence().map { it.key to it.value.asText() }.toMap())
60+
else -> throw IllegalArgumentException("Unknown type: ${value.javaClass}")
61+
}
62+
} else {
63+
throw IllegalArgumentException("Unknown type: ${value.javaClass}")
64+
}
65+
}
66+
67+
override fun get(key: String, defaultValue: Any): JConfigObject {
68+
return try {
69+
get(key)
70+
} catch (e: NoSuchElementException) {
71+
JConfigObjectImpl(defaultValue)
72+
}
73+
}
74+
75+
override fun contains(key: String): Boolean {
76+
return mapEntries.containsKey(key)
77+
}
78+
}

0 commit comments

Comments
 (0)