Search code examples
javapropertiestypesafe-confighocon

HOCON Substitution default value


In HOCON and Typesafe Config, How do I set the default value in case of substitution.

Does it supports something like this ??

${server.host: 'localhost'} -> If server.host set(Either in the same configu files or through environement setting) it substitutes that if not set choose the default value


Solution

  • From the official docs on 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.

    So here is one possible workaround using object merging:

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

    Or even simplier:

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