Hey, I stumbled upon some interesting situation us...
# http4k
d
Hey, I stumbled upon some interesting situation using
Environment
and
lenses
. When running following test:
Copy code
@Test
    fun `extract a list of domain ids from environment`() {
        data class MyDomainId(val value: String)

        val DOMAIN_ID by EnvironmentKey.csv(",")
            .map(
                { strings: List<String> -> strings.map(::MyDomainId) },
                { next -> next.map(MyDomainId::value) })
            .of()
            .required()


        val domainIds = listOf(MyDomainId("1"), MyDomainId("2"))
        val environment = Environment.defaults(DOMAIN_ID of domainIds)

        environment[DOMAIN_ID] shouldBe domainIds
    }
it ends up in
Copy code
Missing elements from index 1
expected:<[MyDomainId(value=1), MyDomainId(value=2)]> but was:<[MyDomainId(value=1)]>
Expected :[MyDomainId(value=1), MyDomainId(value=2)]
Actual   :[MyDomainId(value=1)]
<Click to see difference>

io.kotest.assertions.AssertionFailedError: Missing elements from index 1
Replacing the delimiter in
csv()
to for example
":"
makes the test pass. My findings so far: it’s not the
csv()
part - this happens when I
split
the string by hand. How can I use both type-safety of the
lens
and
,
as separator? Where is my misunderstanding?
d
This is because the default separator for environment is a comma. if you want to have multiple values, you can probably use
EnvironmentKey.value(MyDomainId.multi.required("")
🙏 1
d
Thanks for the hint 😄 I use now
Copy code
val DOMAIN_ID by EnvironmentKey
            .map(::MyDomainId, MyDomainId::value)
            .multi
            .of()
            .required()