-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: EnumDecoder: support case-insensitive decoding (#391)
Co-authored-by: Sam <sam@sksamuel.com>
- Loading branch information
Showing
8 changed files
with
154 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
hoplite-core/src/test/kotlin/com/sksamuel/hoplite/decoder/EnumDecoderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.sksamuel.hoplite.decoder | ||
|
||
import com.sksamuel.hoplite.ConfigFailure | ||
import com.sksamuel.hoplite.DecoderConfig | ||
import com.sksamuel.hoplite.DecoderContext | ||
import com.sksamuel.hoplite.Pos | ||
import com.sksamuel.hoplite.PrimitiveNode | ||
import com.sksamuel.hoplite.StringNode | ||
import com.sksamuel.hoplite.defaultParamMappers | ||
import com.sksamuel.hoplite.fp.Validated | ||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.types.shouldBeInstanceOf | ||
import kotlin.reflect.full.createType | ||
|
||
class EnumDecoderTest : BehaviorSpec({ | ||
|
||
given("a value matching enum case") { | ||
val node = StringNode("ONE", Pos.NoPos, DotPath.root) | ||
|
||
`when`("using case sensitive decoding") { | ||
val actual = EnumDecoder<TestEnum>().decode(node, ignoreCase = false) | ||
|
||
then("should decode it") { | ||
actual.shouldBeInstanceOf<Validated.Valid<TestEnum>>() | ||
.value shouldBe TestEnum.ONE | ||
} | ||
} | ||
|
||
`when`("using case insensitive decoding") { | ||
val actual = EnumDecoder<TestEnum>().decode(node, ignoreCase = true) | ||
|
||
then("should decode it") { | ||
actual.shouldBeInstanceOf<Validated.Valid<TestEnum>>() | ||
.value shouldBe TestEnum.ONE | ||
} | ||
} | ||
} | ||
|
||
given("a value not matching enum case") { | ||
val node = StringNode("Two", Pos.NoPos, DotPath.root) | ||
|
||
`when`("using case sensitive decoding") { | ||
val actual = EnumDecoder<TestEnum>().decode(node, ignoreCase = false) | ||
|
||
then("should return error") { | ||
actual.shouldBeInstanceOf<Validated.Invalid<ConfigFailure>>() | ||
} | ||
} | ||
|
||
`when`("using case insensitive decoding") { | ||
val actual = EnumDecoder<TestEnum>().decode(node, ignoreCase = true) | ||
|
||
then("should decode it") { | ||
actual.shouldBeInstanceOf<Validated.Valid<TestEnum>>() | ||
.value shouldBe TestEnum.TWO | ||
} | ||
} | ||
} | ||
}) { | ||
private companion object { | ||
fun <T : Any> EnumDecoder<T>.decode(node: PrimitiveNode, ignoreCase: Boolean) = decode( | ||
node, | ||
TestEnum::class.createType(), | ||
DecoderContext( | ||
decoders = defaultDecoderRegistry(), | ||
paramMappers = defaultParamMappers(), | ||
config = DecoderConfig(flattenArraysToString = false, resolveTypesCaseInsensitive = ignoreCase) | ||
) | ||
) | ||
|
||
enum class TestEnum { | ||
ONE, TWO | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
hoplite-yaml/src/test/kotlin/com/sksamuel/hoplite/decoder/EnumDecoderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.sksamuel.hoplite.decoder | ||
|
||
import com.sksamuel.hoplite.ConfigFailure | ||
import com.sksamuel.hoplite.ConfigLoaderBuilder | ||
import com.sksamuel.hoplite.fp.Validated | ||
import io.kotest.assertions.asClue | ||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.types.shouldBeInstanceOf | ||
|
||
class EnumDecoderTest : BehaviorSpec({ | ||
|
||
data class Test(val a: Wine, val b: Wine) | ||
|
||
given("yml file with invalid enum name case") { | ||
val yaml = "/enums.yml" | ||
|
||
`when`("using case sensitive decoding") { | ||
val config = ConfigLoaderBuilder.default() | ||
.build() | ||
.loadConfig<Test>(yaml) | ||
|
||
then("should return an error") { | ||
config.shouldBeInstanceOf<Validated.Invalid<ConfigFailure>>() | ||
.error.shouldBeInstanceOf<ConfigFailure.DataClassFieldErrors>() | ||
.errors.list.shouldHaveSize(1) | ||
.first().shouldBeInstanceOf<ConfigFailure.ParamFailure>() | ||
.error.shouldBeInstanceOf<ConfigFailure.InvalidEnumConstant>() | ||
} | ||
} | ||
|
||
`when`("using case insensitive decoding") { | ||
val config = ConfigLoaderBuilder.default() | ||
.withResolveTypesCaseInsensitive() | ||
.build() | ||
.loadConfig<Test>(yaml) | ||
|
||
then("should load the config") { | ||
config.shouldBeInstanceOf<Validated.Valid<Test>>() | ||
.value.asClue { | ||
it.a shouldBe Wine.Malbec | ||
it.b shouldBe Wine.Merlot | ||
} | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
a: Malbec | ||
b: MERLOT |