Skip to content

Commit

Permalink
Allow the ConfigManagerBuilder to give its logger to its ConfigElemen…
Browse files Browse the repository at this point in the history
…ts, thereby allowing them to use it without having a reference to the parent ConfigManagerBuilder. Add missing log warnings and errors.
  • Loading branch information
ArkoSammy12 committed Dec 22, 2024
1 parent 4bff8a7 commit 05877e4
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package io.arkosammy12.monkeyconfig.builders

import io.arkosammy12.monkeyconfig.base.ConfigElement
import io.arkosammy12.monkeyconfig.util.ElementPath
import org.slf4j.Logger

sealed class ConfigElementBuilder<I : ConfigElement, T : ConfigElementBuilder<I, T>>(
val name: String,
) {

internal abstract val path: ElementPath

internal open var logger: Logger? = null

open var comment: String? = null

open var onInitialized: ((I) -> Unit)? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ open class ConfigManagerBuilder(
val path = ElementPath(settingName)
val settingBuilder: T = builderInstanceProvider(settingName, defaultValue, path)
builder(settingBuilder)
settingBuilder.logger = this.logger
this.internalConfigElementBuilders.add(settingBuilder)
return path
}
Expand Down Expand Up @@ -67,12 +68,14 @@ open class ConfigManagerBuilder(
fun section(sectionName: String, builderInstanceProvider: (String, ConfigManagerBuilder) -> SectionBuilder = ::SectionBuilder, builder: SectionBuilder.() -> Unit) {
val sectionBuilder: SectionBuilder = builderInstanceProvider(sectionName, this)
builder(sectionBuilder)
sectionBuilder.logger = this.logger
this.internalConfigElementBuilders.add(sectionBuilder)
}

fun <V : Any, S : SerializableType<*>, T : MapSectionBuilder<V, S>> mapSection(sectionName: String, builderInstanceProvider: (String) -> T, builder: T.() -> Unit): ElementPath {
val mapSectionBuilder = builderInstanceProvider(sectionName)
builder(mapSectionBuilder)
mapSectionBuilder.logger = this.logger
this.internalConfigElementBuilders.add(mapSectionBuilder)
return mapSectionBuilder.path
}
Expand All @@ -82,7 +85,6 @@ open class ConfigManagerBuilder(

}

// TODO: Add some way to add the file extension or atleast warn the user to use the correct file extension in the file path
@JvmOverloads
fun <C : Config, T : ConfigFormat<C>> configManager(fileName: String, fileFormat: T, filePath: Path, builderInstanceProvider: (String, T, Path) -> ConfigManagerBuilder = ::ConfigManagerBuilder, builder: ConfigManagerBuilder.() -> Unit): ConfigManager {
if (fileName.contains(".")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ open class MapSectionBuilder<V : Any, S : SerializableType<*>>(

internal val defaultEntries: MutableMap<String, V> = mutableMapOf()

init {
this.logger = parent?.logger
}

fun addDefaultEntry(entry: Pair<String, V>) {
val key: String = entry.first
val value: V = entry.second
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ open class SectionBuilder(

override var implementation: (SectionBuilder) -> Section = ::DefaultSection

init {
this.logger = parent?.logger
}

fun <V : Any, S : SerializableType<*>, I : Setting<V, S>, T : SettingBuilder<V, S, I, T>> setting(settingName: String, defaultValue: V, builderInstanceProvider: (String, V, ElementPath) -> T, builder: T.() -> Unit): ElementPath {
val path: ElementPath = this.path.withAppendedNode(settingName)
val settingBuilder = builderInstanceProvider(settingName, defaultValue, path)
builder(settingBuilder)
settingBuilder.logger = this.logger
this.internalConfigElementBuilders.add(settingBuilder)
return path
}
Expand Down Expand Up @@ -56,12 +61,14 @@ open class SectionBuilder(
fun section(sectionName: String, builderInstanceProvider: (String, ConfigManagerBuilder, SectionBuilder?) -> SectionBuilder = ::SectionBuilder, builder: SectionBuilder.() -> Unit) {
val sectionBuilder: SectionBuilder = builderInstanceProvider(sectionName, this.manager, this)
builder(sectionBuilder)
sectionBuilder.logger = this.logger
this.internalConfigElementBuilders.add(sectionBuilder)
}

fun <V : Any, S : SerializableType<*>, T : MapSectionBuilder<V, S>> mapSection(sectionName: String, builderInstanceProvider: (String, SectionBuilder) -> T, builder: T.() -> Unit): ElementPath {
val mapSectionBuilder = builderInstanceProvider(sectionName, this)
builder(mapSectionBuilder)
mapSectionBuilder.logger = this.logger
this.internalConfigElementBuilders.add(mapSectionBuilder)
return mapSectionBuilder.path
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.arkosammy12.monkeyconfig.base.sections
import io.arkosammy12.monkeyconfig.base.settings
import io.arkosammy12.monkeyconfig.builders.SectionBuilder
import io.arkosammy12.monkeyconfig.util.ElementPath
import org.slf4j.Logger

abstract class AbstractSection(
sectionBuilder: SectionBuilder,
Expand All @@ -26,6 +27,8 @@ abstract class AbstractSection(

protected val onSavedFunction: ((Section) -> Unit)? = sectionBuilder.onSaved

protected val logger: Logger? = sectionBuilder.logger

override val isInitialized: Boolean
get() = this.internalIsInitialized

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ class DefaultSection(
}

this.configElements = configElements.toList()

// TODO: Make sure there are no accidental duplicates
//sectionBuilder.internalSettingBuilders.clear()
//sectionBuilder.subSections.clear()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.arkosammy12.monkeyconfig.types.ListType
import io.arkosammy12.monkeyconfig.types.SerializableType
import io.arkosammy12.monkeyconfig.types.toSerializedType
import io.arkosammy12.monkeyconfig.util.ElementPath
import org.slf4j.Logger

abstract class AbstractMapSection<V : Any, S : SerializableType<*>>(
mapSectionBuilder: MapSectionBuilder<V, S>
Expand Down Expand Up @@ -46,6 +47,8 @@ abstract class AbstractMapSection<V : Any, S : SerializableType<*>>(

protected val onSavedFunction: ((MapSection<V, S>) -> Unit)? = mapSectionBuilder.onSaved

protected val logger: Logger? = mapSectionBuilder.logger

init {
val mapEntries: MutableList<Setting<V, S>> = mutableListOf()
val defaultMapEntries: MutableList<Setting<V, S>> = mutableListOf()
Expand Down Expand Up @@ -113,9 +116,9 @@ abstract class AbstractMapSection<V : Any, S : SerializableType<*>>(
}
}

override fun updateValue(fileConfig: FileConfig) {
override fun updateValue(fileConfig: FileConfig) {
val config: Config = fileConfig.get(this.path.string) ?: run {
// TODO: LOG
this.logger?.error("Found no Section with name ${this.name} to load values from!")
return
}
val tempEntries: MutableList<Setting<V, S>> = mutableListOf()
Expand All @@ -124,7 +127,7 @@ abstract class AbstractMapSection<V : Any, S : SerializableType<*>>(
val serializedEntryValue: SerializableType<*> = toSerializedType(rawEntryValue)
val newMapEntry: Setting<V, S>? = this.getEntryFromSerialized(ElementPath(*this.path.asArray, entry.key), serializedEntryValue)
if (newMapEntry == null) {
// TODO: LOG
this.logger?.error("Unable to read value of entry ${entry.key}, from MapSection ${this.name}! This entry will be skipped.")
continue
}
tempEntries.add(newMapEntry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.arkosammy12.monkeyconfig.base.Setting
import io.arkosammy12.monkeyconfig.builders.SettingBuilder
import io.arkosammy12.monkeyconfig.types.SerializableType
import io.arkosammy12.monkeyconfig.util.ElementPath
import org.slf4j.Logger

abstract class AbstractSetting<T : Any, S : SerializableType<*>, I : AbstractSetting<T, S, I>>(
settingBuilder: SettingBuilder<T, S, I, *>
Expand All @@ -21,6 +22,8 @@ abstract class AbstractSetting<T : Any, S : SerializableType<*>, I : AbstractSet

protected val onUpdatedFunction: ((I) -> Unit)? = settingBuilder.onUpdated

protected val logger: Logger? = settingBuilder.logger

override fun toString(): String =
"${this::class.simpleName}{name=$name, path=$path, value=$value, comment=$comment}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
package io.arkosammy12.monkeyconfig.values

import io.arkosammy12.monkeyconfig.types.NumberType
import org.slf4j.Logger

class NumberSettingValue<T : Number>(
class NumberSettingValue<T : Number> @JvmOverloads constructor(
default: T,
raw: T = default,
val minValue: T?,
val maxValue: T?,
logger: Logger? = null,
serializer: (T) -> NumberType<T>,
deserializer: (NumberType<T>) -> T
) : SettingValue<T, NumberType<T>>(default, raw, serializer, deserializer) {
) : SettingValue<T, NumberType<T>>(default, raw, logger, serializer, deserializer) {

override var raw: T
set(value) {
if (minValue != null && value < this.minValue) {
// TODO: LOG
this.logger?.error("Value $value is below the minimum value for $this")
return
}
if (this.maxValue != null && value > this.maxValue) {
// TODO: LOG
this.logger?.error("Value $value is above the maximum value for $this")
return
}
super.raw = value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.arkosammy12.monkeyconfig.values

import io.arkosammy12.monkeyconfig.types.SerializableType
import org.slf4j.Logger

open class SettingValue<V : Any, S : SerializableType<*>>(
open class SettingValue<V : Any, S : SerializableType<*>> @JvmOverloads constructor(
val default: V,
open var raw: V = default,
protected val logger: Logger? = null,
private val serializer: (V) -> S,
private val deserializer: (S) -> V
) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/io/arkosammy12/monkeyconfig/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object Main {
comment = "Test comment"
minValue = 0
maxValue = 10
implementation = { builder -> TestNumberSetting(builder) }
implementation = ::TestNumberSetting
}
section("testSubsection") {
comment = "Test comment"
Expand Down

0 comments on commit 05877e4

Please sign in to comment.