Hi I have a json env variable which I read with En...
# http4k
n
Hi I have a json env variable which I read with EnvironmentKey.string() etc .. it seems that reading stops at the first comma, would it be possible?
eg the var content is:
Copy code
{
  "username": "<username>",
  "password": "<password>",
  "host": "<host>"
}
when I print it after reading from env I get
Copy code
{"username": "root"
it seems truncated at the first comma? Maybe?
d
it's because we support lists of values in the environment keys
there is a way around it - le me check
n
Thank you
d
you need to override the separator - it looks like you need to do something like
Copy code
MapEnvironment.from(System.getenv().toProperties(), ";")
n
I'll try thanks
It doesn't seem to work but I am certainly missing something could you please point me to the relevant code? I am searching LensSpec.kt but haven't found the part related to separator ....
d
Copy code
import org.http4k.cloudnative.env.EnvironmentKey
import org.http4k.cloudnative.env.MapEnvironment

fun main() {
    val key = EnvironmentKey.required("FOO")
    System.setProperty("FOO", """{"username": "<username>","password": "<password>","host": "<host>"}""")
    val env = MapEnvironment.from(System.getProperties(), ";")
    println(key(env))
}
prints:
Copy code
{
  "username": "<username>",
  "password": "<password>",
  "host": "<host>"
}
n
Ok I'll try thanks
a
I've run into this issue before, which prevented me from injecting secrets manager values. I assumed it was just JSON not working nicely as an ENV var in general. Interesting workaround. ๐Ÿค”
n
Env variables are just strings, we used them in that format quite commonly in other projects ...
Quite surprised it didn't work with http4k
but the workaround works, though not the most elegant one ...
d
The problem you're hitting here is that the definition of the lenses support lists of parameters as well as singular values. The lens infra is entirely generic - we define it only once and then it's inherited across all contexts (queries, headers, form values, env variables, values from dynamoDB etc).
Copy code
// with: export FOO=first,second,third

fun main() {
    val key = EnvironmentKey.multi.required("FOO")
    val message: List<String> = key(Environment.ENV)
    println(message)
}
We needed a way of cutting up single variable definitions into many items and choosing a comma as a delimited seemed to make sense by default.. TBF, passing JSON in as an env parameter isn't particularly nice either ๐Ÿ˜‰
n
I see ๐Ÿ‘ I can agree that using json as env parameter could not seem particularly nice but it seems not so bad for strictly related parameters (like jdbc database connection parameters)
d
we are both blessed and cursed by the high level of reusability that we've achieved with the lens system ๐Ÿ˜‚
n
๐Ÿ˜„
the problem, to me, is that, when things like that happens, if there is no David which answers so kindly, the codebase is quite obscure to me .. perhaps need to study more kotlin ..
d
if it's not documented, then the best place to start is the tests in http4k ๐Ÿ™‚
n
ok ๐Ÿ‘