Skip to content

Commit

Permalink
Added ability to set environment variables (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail committed Jun 24, 2023
1 parent e6374c2 commit caed7bb
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 13 deletions.
6 changes: 3 additions & 3 deletions 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.7</version>
<version>1.0.9</version>
</dependency>
```

Expand All @@ -46,11 +46,11 @@ And thats it for getting the token.
```groovy
//kotlin
dependencies {
implementation("io.github.realyusufismail:jconfig:1.0.7")
implementation("io.github.realyusufismail:jconfig:1.0.9")
}
//groovy
dependencies {
implementation "io.github.realyusufismail:jconfig:1.0.7"
implementation "io.github.realyusufismail:jconfig:1.0.9"
}
```
6 changes: 3 additions & 3 deletions README.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.8</version>
<version>1.0.9</version>
</dependency>
```

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

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repositories { mavenCentral() }

dependencies {
// json
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.0")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.1")
testImplementation(kotlin("test"))
}

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-SNAPSHOT
version = 1.0.9
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=4159b938ec734a8388ce03f52aa8f3c7ed0d31f5438622545de4f83a89b79788
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
11 changes: 9 additions & 2 deletions src/main/kotlin/io/github/realyusufismail/jconfig/JConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ import io.github.realyusufismail.jconfig.classes.JConfigBuilder
import io.github.realyusufismail.jconfig.classes.JConfigException
import io.github.realyusufismail.jconfig.classes.JsonEntry

/** Used to get a value from the config.json file. Also creates a new JConfig instance. */
/** Gets a value from the config.json file. Also creates a new JConfig instance. */
interface JConfig {
/**
* Used to get all the entries in the config file.
* Gets all the entries in the config file.
*
* @return A list of all the entries in the config file.
*/
val entries: Set<JsonEntry>

/**
* Gets a set of JConfigObject with there associated keys.
*
* @return A set of JConfigObject with there associated keys.
*/
val values: Set<Map.Entry<String, JConfigObject>>

/**
* Gets the value of the key from the config file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,11 @@ interface JConfigObject {
* @return The value as a [JsonEntry].
*/
val asJsonEntry: JsonEntry

/**
* Parses the value as a String.
*
* @return The value as a String.
*/
val parseAsString: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class JConfigBuilder {
private var extension = ".json"
private var filename = "config$extension"
private var directoryPath = "./"
private var environmentVariable = false

fun setFilename(filename: String): JConfigBuilder {
this.filename = filename + extension
Expand All @@ -44,6 +45,11 @@ class JConfigBuilder {
return this
}

fun enableEnvironmentVariable(): JConfigBuilder {
this.environmentVariable = true
return this
}

@Throws(JConfigException::class)
fun build(): JConfig {
val mapper = ObjectMapper()
Expand Down Expand Up @@ -73,7 +79,15 @@ class JConfigBuilder {
entries.add(JsonEntry(key, value))
}

JConfigImpl(entries)
val config = JConfigImpl(entries)

if (environmentVariable) {
config.values.forEach { (key, value) ->
System.setProperty(key, value.parseAsString)
}
}

config
} catch (e: IOException) {
throw JConfigException("Could not read the config file.", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,26 @@ 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 val values: Set<Map.Entry<String, JConfigObject>>
get() {
val map = HashMap<String, JConfigObject>()
for (entry in jsonEntries) {
map[entry.key] = get(entry.key)!!
}
return map.entries
}


override fun get(key: String): JConfigObject? {
// mapEntries[key] ?: throw NoSuchElementException("No value present for key: $key")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,16 @@ class JConfigObjectImpl(private val value: Any) : JConfigObject {

override val asJsonEntry: JsonEntry
get() = value as JsonEntry


override val parseAsString: String
get() = when (value) {
is String -> value
is Number -> value.toString()
is Boolean -> value.toString()
is Char -> value.toString()
is JsonEntry -> value.toString()
else -> throw ClassCastException("Cannot cast $value to String")
}

}

0 comments on commit caed7bb

Please sign in to comment.