Compare commits

...

2 Commits

Author SHA1 Message Date
Robinson e583381f3b
version 2.8 2023-08-21 22:58:18 +02:00
Robinson ff8c8d75b1
Fixed issues when not able to properly load/process data, but still want to save it. 2023-08-21 22:57:49 +02:00
3 changed files with 31 additions and 23 deletions

View File

@ -34,7 +34,7 @@ Maven Info
<dependency>
<groupId>com.dorkbox</groupId>
<artifactId>Config</artifactId>
<version>2.7</version>
<version>2.8</version>
</dependency>
</dependencies>
```
@ -44,7 +44,7 @@ Gradle Info
```
dependencies {
...
implementation("com.dorkbox:Config:2.7")
implementation("com.dorkbox:Config:2.8")
}
```

View File

@ -38,7 +38,7 @@ object Extras {
// set for the project
const val description = "CLI, system properties, environment variables, or JSON text/file input processing."
const val group = "com.dorkbox"
const val version = "2.7"
const val version = "2.8"
// set as project.ext
const val name = "Config"

View File

@ -123,7 +123,7 @@ class ConfigProcessor<T : Any>
/**
* Gets the version number.
*/
const val version = "2.7"
const val version = "2.8"
init {
// Add this project to the updates system, which verifies this class + UUID + version information
@ -323,8 +323,8 @@ class ConfigProcessor<T : Any>
// DEEP COPY of the original values from the file, so when saving, we know what the
// overridden values are and can skip saving them
// NOTE: overridden values are set via CLI/Sys/Env!!
private lateinit var origCopyConfig: T
private lateinit var origCopyConfigMap: Map<String, ConfigProp>
private var origCopyConfig: T? = null
private var origCopyConfigMap: Map<String, ConfigProp>? = null
init {
@ -392,6 +392,17 @@ class ConfigProcessor<T : Any>
return this
}
private fun loadOrigCopyConfig(configObject: T) {
// now setup the "original" objects. ORIGINAL means...
// 1) the original, passed in config object IFF no other config data was loaded
// 2) the file (as an object)
// 3) the text (as an object)
val origCopyConfig = json.fromJson(objectType.java, json.toJson(configObject))!!
this.origCopyConfig = origCopyConfig
origCopyConfigMap = createConfigMap(origCopyConfig)
}
/**
* Specify the baseline data (as a JSON string) used to populate the values of the config object.
*
@ -427,6 +438,7 @@ class ConfigProcessor<T : Any>
*
* If the specified file DOES NOT load, then it will not be used or processed!
*/
@Suppress("DuplicatedCode")
@Synchronized
fun loadAndProcess(configFile: File): ConfigProcessor<T> {
val configObject =
@ -496,6 +508,7 @@ class ConfigProcessor<T : Any>
*
* If the specified file DOES NOT load, then it will not be used!
*/
@Suppress("DuplicatedCode")
@Synchronized
fun load(configFile: File): Boolean {
val configObject =
@ -534,7 +547,7 @@ class ConfigProcessor<T : Any>
// support
// thing[0].flag = true (array + list)
// thing[0].bah.flag = true (array + list)
@Suppress("UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST", "UNUSED_DESTRUCTURED_PARAMETER_ENTRY")
@Synchronized
private fun load(configObject: T) {
val incomingDataConfigMap = createConfigMap(configObject)
@ -640,13 +653,7 @@ class ConfigProcessor<T : Any>
}
}
// now setup the "original" objects. ORIGINAL means...
// 1) the original, passed in config object IFF no other config data was loaded
// 2) the file (as an object)
// 3) the text (as an object)
origCopyConfig = json.fromJson(objectType.java, json.toJson(configObject))!!
origCopyConfigMap = createConfigMap(origCopyConfig)
loadOrigCopyConfig(configObject)
}
@ -662,17 +669,13 @@ class ConfigProcessor<T : Any>
configString?.also { load(it) }
if (configFile == null && configString == null) {
// now setup the "original" objects. ORIGINAL means...
// 1) the original, passed in config object IFF no other config data was loaded
// 2) the file (as an object)
// 3) the text (as an object)
origCopyConfig = json.fromJson(objectType.java, json.toJson(configObject))!!
origCopyConfigMap = createConfigMap(origCopyConfig)
loadOrigCopyConfig(configObject)
}
return postProcess()
}
@Suppress("UNUSED_DESTRUCTURED_PARAMETER_ENTRY")
private fun postProcess() : ConfigProcessor<T> {
// this permits a bash/batch/CLI invocation of "get xyz" or "set xyz" so we can be interactive with the CLI
// if there is no get/set CLI argument, then this does nothing.
@ -865,7 +868,7 @@ class ConfigProcessor<T : Any>
return originalArray
}
@Suppress("UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST", "CascadeIf")
private fun expandArrays(prop: ConfigProp, newProps: MutableMap<String, ConfigProp>) {
val returnType = prop.returnType
@ -974,8 +977,13 @@ class ConfigProcessor<T : Any>
*/
@Synchronized
fun originalJson(): String {
if (origCopyConfigMap == null) {
// we might not have loaded the file yet (because invalid configs/etc), so make sure it's loaded!
loadOrigCopyConfig(configObject)
}
// we use configCopy to save the state of everything as a snapshot (and then we serialize it)
origCopyConfigMap.forEach { (k,v) ->
origCopyConfigMap!!.forEach { (k,v) ->
val configured = configMap[k]
if (configured != null && !configured.ignore && !configured.override) {
// this will change what the "original copy" is recorded as having.
@ -983,7 +991,7 @@ class ConfigProcessor<T : Any>
}
}
return json.toJson(origCopyConfig)
return json.toJson(origCopyConfig!!)
}
/**