HOCON替换默认值

HOCON Substitution default value

在HOCON和Typesafe Config中,替换时如何设置默认值

它支持这样的东西吗??

${server.host: 'localhost'} -> 如果 server.host 设置(在相同的配置文件中或通过环境设置)它替代如果没有设置选择默认值

来自 substitutions 上的官方文档:

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.

所以这是一种可能的解决方法,使用 object merging:

defaults {
  foo: "default Value"
}

item = ${defaults} {
  foo: ${?bar}
}

或者更简单:

item = {
  foo: "default Value"
  foo: ${?bar}
}