diff --git a/src/main/kotlin/com/uchuhimo/konf/Config.kt b/src/main/kotlin/com/uchuhimo/konf/Config.kt index cb521e45..781ec659 100644 --- a/src/main/kotlin/com/uchuhimo/konf/Config.kt +++ b/src/main/kotlin/com/uchuhimo/konf/Config.kt @@ -311,7 +311,15 @@ private class ConfigImpl constructor( } is ValueState.Value -> valueState.value as T is ValueState.Lazy<*> -> { - val value = valueState.thunk(lazyContext)!! + val value = try { + valueState.thunk(lazyContext) + } catch (exception: UnsetValueException) { + if (errorWhenUnset) { + throw exception + } else { + return null + } + } if (item.type.rawClass.isInstance(value)) { value as T } else { @@ -558,7 +566,7 @@ private class ConfigImpl constructor( private sealed class ValueState { object Unset : ValueState() - data class Lazy(var thunk: (config: ItemContainer) -> T) : ValueState() + data class Lazy(var thunk: (config: ItemContainer) -> T) : ValueState() data class Value(var value: Any) : ValueState() } } diff --git a/src/test/kotlin/com/uchuhimo/konf/ConfigSpek.kt b/src/test/kotlin/com/uchuhimo/konf/ConfigSpek.kt index 2103698b..41632161 100644 --- a/src/test/kotlin/com/uchuhimo/konf/ConfigSpek.kt +++ b/src/test/kotlin/com/uchuhimo/konf/ConfigSpek.kt @@ -134,6 +134,29 @@ object ConfigSpek : SubjectSpek({ assertThat(newConfig[spec.type], equalTo(NetworkBuffer.Type.ON_HEAP)) assertThat(newConfig, equalTo(subject)) } + it("should not contain unset items in map") { + val config = Config { addSpec(spec) } + assertThat(config.toMap(), equalTo(mapOf( + spec.name.name to "buffer", + spec.type.name to NetworkBuffer.Type.OFF_HEAP))) + } + } + on("object methods") { + val map = mapOf( + spec.name.name to "buffer", + spec.type.name to NetworkBuffer.Type.OFF_HEAP) + it("should not equal to object of other class") { + assertFalse(subject.equals(1)) + } + it("should equal to itself") { + assertThat(subject, equalTo(subject)) + } + it("should have same hash code with map with same content") { + assertThat(subject.hashCode(), equalTo(map.hashCode())) + } + it("should convert to string in map-like format") { + assertThat(subject.toString(), equalTo("Config(items=$map)")) + } } group("get operation") { on("get with valid item") {