简体   繁体   English

如何使用环境变量覆盖Typesafe配置中的数组?

[英]How to override array in Typesafe config with environment variables?

I've been using the environment variable substitution feature in Typesafe config: 我一直在Typesafe配置中使用环境变量替换功能:

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

This results in a "default" value of "foo" if there is no environment variable named FOO . 如果没有名为FOO环境变量,则结果为“ foo”的“默认”值。 In this case, the second value declaration ( foo = ${?FOO} is simply discarded). 在这种情况下,第二个值声明( foo = ${?FOO}被简单地丢弃)。 However, if a variable named FOO exists, the library will "substitute" the value of FOO and assign it to foo . 但是,如果存在一个名为FOO的变量,该库将“替换” FOO的值并将其分配给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. 在未定义f1f2的情况下,这只会导致foo为空数组。 My goal is to have a similar effect as above (discard the second foo if no environment variables f1 and f2 are defined). 我的目标是产生与上述类似的效果(如果未定义环境变量f1f2则丢弃第二个foo )。 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. 我发现在HOCON配置文件中使用a = ${?VARNAME}且Scala代码期望使用List ,并使用-DVARNAME.0=something (或简称VARNAME.0=something )将得到正确的["something"]值。 (Tested with Play 2.6.13 and the AllowedHosts filter.) (已在Play 2.6.13和AllowedHosts过滤器中进行了测试。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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