Hello all, is it possible to have a path param tha...
# http4k
m
Hello all, is it possible to have a path param that maps to a enum and is not case sensitive?
d
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
Thanks @dave I'll give this a shot
d
that's also safer because if you refactor the enum name you'll break the contract
m
a
will be a String here though?
d
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
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
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
Thanks @dave