10

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

Vadzim
  • 24,954
  • 11
  • 143
  • 151
Ysak
  • 2,601
  • 6
  • 29
  • 53
  • Similar question: http://stackoverflow.com/questions/26648416/how-set-default-list-value-if-system-variable-is-not-present-in-typesafe-configu – Vadzim Oct 05 '16 at 14:31

1 Answers1

15

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}
}
Vadzim
  • 24,954
  • 11
  • 143
  • 151