简体   繁体   中英

How to override array in Typesafe config with environment variables?

I've been using the environment variable substitution feature in Typesafe config:

foo = "foo"
foo = ${?FOO}

This results in a "default" value of "foo" if there is no environment variable named FOO . In this case, the second value declaration ( foo = ${?FOO} is simply discarded). However, if a variable named FOO exists, the library will "substitute" the value of FOO and assign it to foo .

I would like similar behavior with arrays, but unfortunately, this does not work as expected:

foo = [ "1", "2" ]
foo = [ ${?f1}, ${?f2} ]

In the case where f1 and f2 are not defined, this simply results in foo being an empty array. My goal is to have a similar effect as above (discard the second foo if no environment variables f1 and f2 are defined). Any ideas/suggestions are appreciated. Thanks.

I found that using a = ${?VARNAME} in the HOCON config file with the Scala code expecting a List , and using -DVARNAME.0=something (or simply VARNAME.0=something ) will result in the correct ["something"] value. (Tested with Play 2.6.13 and the AllowedHosts filter.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM