https://kotlinlang.org logo
#http4k
Title
# http4k
m

Matt

11/08/2021, 2:57 PM
Hello all, is it possible to have a path param that maps to a enum and is not case sensitive?
d

dave

11/08/2021, 3:12 PM
sort of - you can map it:
Copy code
enum class Example {
    First, Second;
    
    fun toExt() = name.decapitalize()
    
    companion object {
        fun fromCode(value: String) = Example.valueOf(value.capitalize())
    }
}

val a = Path.enum<Example>().map(Example::toExt, Example::fromCode).of("example")
m

Matt

11/08/2021, 3:13 PM
Thanks @dave I'll give this a shot
d

dave

11/08/2021, 3:14 PM
that's also safer because if you refactor the enum name you'll break the contract
m

Matt

11/08/2021, 3:17 PM
a
will be a String here though?
d

dave

11/08/2021, 3:19 PM
nope - the output type of the lens is Example - it just maps the value inside from the string that it gets from the input type (which is a string)
m

Matt

11/08/2021, 3:28 PM
Intellij seems to think it's a String but this may be a misunderstanding on my part
Copy code
val a: BiDiPathLens<String> = Path.enum<Example>().map(Example::toExt, Example::fromCode).of("example")
d

dave

11/08/2021, 3:31 PM
whoops - my mistake:
Copy code
enum class Example {
    First, Second;

    companion object {
        fun fromCode(value: String) = valueOf(value.capitalize())
    }
}

val foobar = Path.map(Example::fromCode).of("example")
I was mapping the enum by mistake
you don't need the injection
👍 1
m

Matt

11/08/2021, 3:32 PM
Thanks @dave
4 Views