Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 976
naivedeveloper Avatar asked Jan 09 '17 21:01

naivedeveloper


1 Answers

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.)

like image 96
PAStheLoD Avatar answered Nov 15 '22 11:11

PAStheLoD