Skip to content

Commit 53d23c5

Browse files
committed
internal functions with public api to allow overriding instead of goofy onPreLoad onPostLoad functions
1 parent 4ceee4e commit 53d23c5

File tree

3 files changed

+29
-89
lines changed

3 files changed

+29
-89
lines changed

src/main/kotlin/com/lambda/config/Configuration.kt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ abstract class Configuration : Jsonable, Loadable {
105105
}
106106
}
107107

108+
protected open fun internalTrySave(logToChat: Boolean) {
109+
save()
110+
.onSuccess {
111+
val message = "Saved ${configName.capitalize()} config."
112+
LOG.info(message)
113+
if (logToChat) info(message)
114+
}
115+
.onFailure {
116+
val message = "Failed to save ${configName.capitalize()} config"
117+
LOG.error(message, it)
118+
logError(message)
119+
}
120+
}
121+
108122
/**
109123
* Loads the config from the [file]
110124
* Encapsulates [JsonIOException] and [JsonSyntaxException] in a runCatching block
@@ -114,7 +128,7 @@ abstract class Configuration : Jsonable, Loadable {
114128
.ifExists { loadFromJson(JsonParser.parseReader(it.reader()).asJsonObject) }
115129
}
116130

117-
open fun tryLoad() = runIO {
131+
protected open fun internalTryLoad() {
118132
load(primary)
119133
.onSuccess {
120134
val message = "${configName.capitalize()} config loaded."
@@ -137,19 +151,8 @@ abstract class Configuration : Jsonable, Loadable {
137151
}
138152
}
139153

140-
open fun trySave(logToChat: Boolean = false) = runIO {
141-
save()
142-
.onSuccess {
143-
val message = "Saved ${configName.capitalize()} config."
144-
LOG.info(message)
145-
if (logToChat) info(message)
146-
}
147-
.onFailure {
148-
val message = "Failed to save ${configName.capitalize()} config"
149-
LOG.error(message, it)
150-
logError(message)
151-
}
152-
}
154+
fun tryLoad() = runIO { internalTryLoad() }
155+
fun trySave(logToChat: Boolean = false) = runIO { internalTrySave(logToChat) }
153156

154157
companion object {
155158
val configurations = mutableSetOf<Configuration>()

src/main/kotlin/com/lambda/config/DynamicConfiguration.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/main/kotlin/com/lambda/config/configurations/UserAutomationConfigs.kt

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,30 @@
1818
package com.lambda.config.configurations
1919

2020
import com.google.gson.JsonParser
21-
import com.lambda.config.DynamicConfiguration
21+
import com.lambda.config.Configuration
2222
import com.lambda.config.UserAutomationConfig
2323
import com.lambda.module.ModuleRegistry.moduleNameMap
2424
import com.lambda.util.FileUtils.ifExists
2525
import com.lambda.util.FolderRegister
2626
import java.io.File
2727

28-
object UserAutomationConfigs : DynamicConfiguration() {
28+
object UserAutomationConfigs : Configuration() {
2929
override val configName = "custom-automation"
3030
override val primary: File = FolderRegister.config.resolve("${configName}.json").toFile()
3131

32-
override fun load(): String {
33-
onPreLoad {
34-
primary.ifExists {
35-
JsonParser.parseReader(it.reader()).asJsonObject.entrySet().forEach { (name, _) ->
36-
if (configurables.any { config -> config.name == name }) return@forEach
37-
UserAutomationConfig(name)
38-
}
32+
override fun internalTryLoad() {
33+
primary.ifExists {
34+
JsonParser.parseReader(it.reader()).asJsonObject.entrySet().forEach { (name, _) ->
35+
if (configurables.any { config -> config.name == name }) return@forEach
36+
UserAutomationConfig(name)
3937
}
4038
}
41-
42-
onPostLoad {
43-
configurables.forEach {
44-
val config = it as? UserAutomationConfig ?: throw IllegalStateException("UserAutomationConfigs contains non-AutomationConfig")
45-
config.linkedModules.value.forEach { moduleName ->
46-
moduleNameMap[moduleName]?.automationConfig = config
47-
}
39+
super.internalTryLoad()
40+
configurables.forEach {
41+
val config = it as? UserAutomationConfig ?: throw IllegalStateException("UserAutomationConfigs contains non-AutomationConfig")
42+
config.linkedModules.value.forEach { moduleName ->
43+
moduleNameMap[moduleName]?.automationConfig = config
4844
}
4945
}
50-
51-
return super.load()
5246
}
5347
}

0 commit comments

Comments
 (0)