Skip to content

Commit 3f0c888

Browse files
committed
Rework on ConfigLoader settings API
1 parent 8f74c7e commit 3f0c888

File tree

3 files changed

+161
-54
lines changed

3 files changed

+161
-54
lines changed

core/src/main/kotlin/io/github/rothes/esu/core/colorscheme/ColorSchemes.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ object ColorSchemes {
1414
}
1515

1616
private fun load(): MultiColorSchemeConfiguration {
17-
return ConfigLoader.loadMulti(
17+
return ConfigLoader.loadMulti<MultiColorSchemeConfiguration, ColorScheme>(
1818
EsuCore.instance.baseConfigPath().resolve("color_schemes"),
19-
"${EsuConfig.get().defaultColorScheme}.yml",
20-
builder = {
21-
it.defaultOptions {
22-
it.header("""
19+
ConfigLoader.LoaderSettingsMulti(
20+
"${EsuConfig.get().defaultColorScheme}.yml",
21+
yamlLoader = {
22+
it.defaultOptions {
23+
it.header("""
2324
|The default color scheme for ESU.
2425
|To use the color defined in color scheme, use <primary_color>, <primary_dim_color> or so.
2526
|Alternatively, you can abridge them like <pc>, <pdc>.
2627
|
2728
|We make dim color of its original of HSL(+0°, -10%, -8%), error color excluded.
2829
""".trimMargin())
30+
}
2931
}
30-
})
32+
))
3133
}
3234

3335
}

core/src/main/kotlin/io/github/rothes/esu/core/configuration/ConfigLoader.kt

Lines changed: 151 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,41 @@ object ConfigLoader {
4545

4646
inline fun <reified T: MultiConfiguration<D>, reified D: ConfigurationPart>
4747
loadMulti(path: Path, vararg forceLoad: String): T {
48-
return loadMultiSimple(path, T::class.java, D::class.java, forceLoad = forceLoad)
48+
return loadMultiSimple(path, T::class.java, D::class.java, forceLoad.toList())
4949
}
5050

5151
inline fun <reified T: MultiConfiguration<D>, reified D: ConfigurationPart>
52-
loadMulti(path: Path, vararg forceLoad: String,
53-
create: Array<String>? = null, loadSubDir: Boolean = false,
54-
noinline nameMapper: (Path) -> String = { it.nameWithoutExtension },
55-
noinline builder: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it },
56-
noinline nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it },
57-
noinline modifier: (D, Path) -> D = { it, _ -> it }): T {
58-
return loadMulti(path, T::class.java, D::class.java, forceLoad = forceLoad, create, loadSubDir, nameMapper, builder, nodeMapper, modifier)
52+
loadMulti(path: Path, forceLoad: List<String>): T {
53+
return loadMultiSimple(path, T::class.java, D::class.java, forceLoad)
54+
}
55+
56+
inline fun <reified T: MultiConfiguration<D>, reified D: ConfigurationPart>
57+
loadMulti(path: Path, settings: LoaderSettingsMulti<D>): T {
58+
return loadMulti(path, T::class.java, D::class.java, settings)
5959
}
6060

6161
inline fun <reified T: MultiConfiguration<D>, D: ConfigurationPart>
62-
loadMulti(path: Path, dataClass: Class<D>, vararg forceLoad: String,
63-
create: Array<String>? = null, loadSubDir: Boolean = false,
64-
noinline nameMapper: (Path) -> String = { it.nameWithoutExtension },
65-
noinline builder: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it },
66-
noinline nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it },
67-
noinline modifier: (D, Path) -> D = { it, _ -> it }): T {
68-
return loadMulti(path, T::class.java, dataClass, forceLoad = forceLoad, create, loadSubDir, nameMapper, builder, nodeMapper, modifier)
62+
loadMulti(path: Path, dataClass: Class<D>, settings: LoaderSettingsMulti<D>): T {
63+
return loadMulti(path, T::class.java, dataClass, settings)
64+
}
65+
66+
inline fun <reified T: MultiConfiguration<D>, reified D: ConfigurationPart>
67+
loadMulti(path: Path, settings: LoaderSettingsMulti.Builder<D>): T {
68+
return loadMulti(path, T::class.java, D::class.java, settings.build())
69+
}
70+
71+
inline fun <reified T: MultiConfiguration<D>, D: ConfigurationPart>
72+
loadMulti(path: Path, dataClass: Class<D>, settings: LoaderSettingsMulti.Builder<D>): T {
73+
return loadMulti(path, T::class.java, dataClass, settings.build())
6974
}
7075

7176
fun <T: MultiConfiguration<D>, D: ConfigurationPart>
72-
loadMultiSimple(path: Path, configClass: Class<T>, dataClass: Class<D>, forceLoad: Array<out String>): T {
73-
return loadMulti(path, configClass, dataClass, forceLoad = forceLoad)
77+
loadMultiSimple(path: Path, configClass: Class<T>, dataClass: Class<D>, forceLoad: List<String>): T {
78+
return loadMulti(path, configClass, dataClass, LoaderSettingsMulti(forceLoad))
7479
}
7580

7681
fun <T: MultiConfiguration<D>, D: ConfigurationPart>
77-
loadMulti(path: Path, configClass: Class<T>, dataClass: Class<D>, vararg forceLoad: String,
78-
create: Array<String>? = null, loadSubDir: Boolean = false,
79-
nameMapper: (Path) -> String = { it.nameWithoutExtension },
80-
builder: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it },
81-
nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it },
82-
modifier: (D, Path) -> D = { it, _ -> it }): T {
82+
loadMulti(path: Path, configClass: Class<T>, dataClass: Class<D>, settings: LoaderSettingsMulti<D>): T {
8383
if (dataClass.isInstance(EmptyConfiguration)) {
8484
return configClass.getConstructor(Map::class.java).newInstance(emptyMap<String, D>())
8585
}
@@ -96,16 +96,16 @@ object ConfigLoader {
9696
}
9797
return configClass.getConstructor(Map::class.java).newInstance(
9898
buildMap {
99-
val files = forceLoad.map { path.resolve(it) }.toMutableSet()
100-
if (create?.isNotEmpty() == true && path.notExists()) {
101-
files.addAll(create.map { path.resolve(it) })
99+
val files = settings.forceLoad?.map { path.resolve(it) }?.toMutableSet() ?: mutableSetOf()
100+
if (settings.createKeys?.isNotEmpty() == true && path.notExists()) {
101+
files.addAll(settings.createKeys.map { path.resolve(it) })
102102
}
103103

104104
if (path.isDirectory()) {
105-
loadDirectory(path, files, loadSubDir)
105+
loadDirectory(path, files, settings.loadSubDirectories)
106106
}
107107
files.forEach { file ->
108-
put(nameMapper(file), load(file, dataClass, builder, nodeMapper, modifier))
108+
put(settings.keyMapper(file), load(file, dataClass, settings))
109109
}
110110
}
111111
)
@@ -125,30 +125,28 @@ object ConfigLoader {
125125
return loadSimple(path, T::class.java)
126126
}
127127

128-
inline fun <reified T> load(path: Path,
129-
noinline builder: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it },
130-
noinline nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it },
131-
noinline modifier: (T, Path) -> T = { it, _ -> it }): T {
132-
return load(path, T::class.java, builder, nodeMapper, modifier)
128+
inline fun <reified T> load(path: Path, settings: LoaderSettings<T>): T {
129+
return load(path, T::class.java, settings)
130+
}
131+
132+
inline fun <reified T> load(path: Path, settings: LoaderSettings.Builder<T>): T {
133+
return load(path, T::class.java, settings.build())
133134
}
134135

135136
fun <T> loadSimple(path: Path, clazz: Class<T>): T {
136-
return load(path, clazz)
137+
return load(path, clazz, LoaderSettings())
137138
}
138139

139-
fun <T> load(path: Path, clazz: Class<T>,
140-
builder: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it },
141-
nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it },
142-
modifier: (T, Path) -> T = { it, _ -> it }): T {
140+
fun <T> load(path: Path, clazz: Class<T>, settings: LoaderSettings<T>): T {
143141
if (clazz.isInstance(EmptyConfiguration)) {
144142
return clazz.cast(EmptyConfiguration)
145143
}
146144
if (path.isDirectory()) {
147145
throw IllegalArgumentException("Path '$path' is a directory")
148146
}
149-
val loader = createBuilder().path(path).let(builder).build()
150-
val node = nodeMapper(loader.load())
151-
val t = modifier.invoke(node.require(clazz), path)
147+
val loader = createBuilder().path(path).let(settings.yamlLoader).build()
148+
val node = settings.nodeMapper(loader.load())
149+
val t = settings.modifier.invoke(node.require(clazz), path)
152150
node.set(clazz, t)
153151
loader.save(node)
154152
if (t is SavableConfiguration) {
@@ -164,15 +162,13 @@ object ConfigLoader {
164162
println((serial as ObjectMapper.Factory).get(clazz).load(BasicConfigurationNode.root(node.options().shouldCopyDefaults(false))))
165163
}
166164

167-
fun save(path: Path, obj: Any,
168-
builder: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it }) {
165+
fun save(path: Path, obj: Any, yamlLoader: YamlConfigurationLoader = createBuilder().path(path).build()) {
169166
if (path.isDirectory()) {
170167
throw IllegalArgumentException("Path '$path' is a directory")
171168
}
172-
val loader = createBuilder().path(path).let(builder).build()
173-
val node = loader.load() ?: CommentedConfigurationNode.root(loader.defaultOptions())
169+
val node = yamlLoader.load() ?: CommentedConfigurationNode.root(yamlLoader.defaultOptions())
174170
node.set(obj.javaClass, obj)
175-
loader.save(node)
171+
yamlLoader.save(node)
176172
}
177173

178174
fun createBuilder(): YamlConfigurationLoader.Builder {
@@ -322,4 +318,113 @@ object ConfigLoader {
322318
}
323319
}
324320

321+
open class LoaderSettings<T>(
322+
val yamlLoader: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it },
323+
val nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it },
324+
val modifier: (T, Path) -> T = { it, _ -> it }
325+
) {
326+
327+
class Builder<T> {
328+
329+
var yamlLoader: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it }
330+
var nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it }
331+
var modifier: (T, Path) -> T = { it, _ -> it }
332+
333+
fun yamlLoader(yamlLoader: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder): Builder<T> {
334+
this.yamlLoader = yamlLoader
335+
return this
336+
}
337+
338+
fun nodeMapper(mapper: (ConfigurationNode) -> ConfigurationNode): Builder<T> {
339+
this.nodeMapper = mapper
340+
return this
341+
}
342+
343+
fun modifier(modifier: (T, Path) -> T): Builder<T> {
344+
this.modifier = modifier
345+
return this
346+
}
347+
348+
fun build() = LoaderSettings(yamlLoader, nodeMapper, modifier)
349+
350+
}
351+
}
352+
353+
354+
class LoaderSettingsMulti<T>(
355+
val forceLoad: List<String>? = null,
356+
val createKeys: List<String>? = null,
357+
val loadSubDirectories: Boolean = false,
358+
val keyMapper: (Path) -> String = { it.nameWithoutExtension },
359+
yamlLoader: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it },
360+
nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it },
361+
modifier: (T, Path) -> T = { it, _ -> it }
362+
): LoaderSettings<T>(yamlLoader, nodeMapper, modifier) {
363+
364+
constructor(
365+
vararg forceLoad: String,
366+
create: List<String>? = null,
367+
loadSubDirectories: Boolean = false,
368+
yamlLoader: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it },
369+
keyMapper: (Path) -> String = { it.nameWithoutExtension },
370+
nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it },
371+
modifier: (T, Path) -> T = { it, _ -> it }
372+
): this(forceLoad.toList(), create, loadSubDirectories, keyMapper, yamlLoader, nodeMapper, modifier)
373+
374+
375+
class Builder<T> {
376+
377+
var forceLoad: List<String>? = null
378+
var createKeys: List<String>? = null
379+
var loadSubDirectories: Boolean = false
380+
var keyMapper: (Path) -> String = { it.nameWithoutExtension }
381+
var yamlLoader: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder = { it }
382+
var nodeMapper: (ConfigurationNode) -> ConfigurationNode = { it }
383+
var modifier: (T, Path) -> T = { it, _ -> it }
384+
385+
fun forceLoad(vararg forceLoad: String): Builder<T> {
386+
this.forceLoad = forceLoad.toList()
387+
return this
388+
}
389+
390+
fun forceLoad(forceLoad: List<String>?): Builder<T> {
391+
this.forceLoad = forceLoad
392+
return this
393+
}
394+
395+
fun create(create: List<String>?): Builder<T> {
396+
this.createKeys = create
397+
return this
398+
}
399+
400+
fun loadSubDirectories(loadSubDirectories: Boolean): Builder<T> {
401+
this.loadSubDirectories = loadSubDirectories
402+
return this
403+
}
404+
405+
fun keyMapper(mapper: (Path) -> String): Builder<T> {
406+
this.keyMapper = mapper
407+
return this
408+
}
409+
410+
fun yamlLoader(yamlLoader: (YamlConfigurationLoader.Builder) -> YamlConfigurationLoader.Builder): Builder<T> {
411+
this.yamlLoader = yamlLoader
412+
return this
413+
}
414+
415+
fun nodeMapper(mapper: (ConfigurationNode) -> ConfigurationNode): Builder<T> {
416+
this.nodeMapper = mapper
417+
return this
418+
}
419+
420+
fun modifier(modifier: (T, Path) -> T): Builder<T> {
421+
this.modifier = modifier
422+
return this
423+
}
424+
425+
fun build() = LoaderSettingsMulti(forceLoad, createKeys, loadSubDirectories, keyMapper, yamlLoader, nodeMapper, modifier)
426+
427+
}
428+
}
429+
325430
}

core/src/main/kotlin/io/github/rothes/esu/core/module/CommonModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ abstract class CommonModule<C: ConfigurationPart, L: ConfigurationPart>(
5858
}
5959

6060
override fun reloadConfig() {
61-
config = ConfigLoader.load(configPath, configClass, builder = configLoader)
62-
locale = ConfigLoader.loadMulti(localePath, localeClass, "en_us.yml", builder = localeLoader)
61+
config = ConfigLoader.load(configPath, configClass, ConfigLoader.LoaderSettings(yamlLoader = configLoader))
62+
locale = ConfigLoader.loadMulti(localePath, localeClass, ConfigLoader.LoaderSettingsMulti("en_us.yml", yamlLoader = localeLoader))
6363
}
6464

6565
fun User.hasPerm(shortPerm: String): Boolean {

0 commit comments

Comments
 (0)