带有周围文本的 HOCON 条件替换

HOCON conditional substitution with surrounding text

我目前没有办法自己测试这个语法,所以问题是:

如果值被文本包围,值 substitution/overriding 是否仍然会出现?

例如,我知道:

foo.baseUrl = "http://foo:1234/" foo.baseUrl = ${?FOO_BASE_URL}

如果 ENV 变量 FOO_BASE_URL 存在,

将覆盖 foo.baseURL

这里发生了什么?

foo.baseUrl = "http://foo:1234/" foo.baseUrl = "http://${?FOO_BASE_URL}:1234/"

如果 ENV 变量存在,覆盖是否仍然发生? 把它放在一个字符串中会否定这个吗?

只是 运行 一个快速测试,第二个值将替换第一个,缺少中间部分。所以如果FOO_BASE_URL没有定义,而application.conf包含如下(注意第二行省略",双引号内不进行变量替换):

foo.baseUrl = "http://foo:1234/"
foo.baseUrl = http://${?FOO_BASE_URL}:1234/
foo.baseUrl = ${?FOO_BASE_URL}

foo.baseUrl 的值将是 http://:1234/。未找到的变量在第二行中被视为空字符串。文档 Substitution 部分的相关引用:

If a substitution with the ${?foo} syntax is undefined:

  • if it is the value of an object field then the field should not be created. If the field would have overridden a previously-set value for the same field, then the previous value remains.
  • if it is an array element then the element should not be added.
  • if it is part of a value concatenation with another string then it should become an empty string; if part of a value concatenation with an object or array it should become an empty object or array.
  • foo : ${?bar} would avoid creating field foo if bar is undefined. foo : ${?bar}${?baz} would also avoid creating the field if both bar and baz are undefined.