HOCON Substitution default value
Asked Answered
C

1

11

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

Cyd answered 8/8, 2016 at 6:59 Comment(1)
Similar question: #26648916Horrid
H
16

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}
}
Horrid answered 17/8, 2016 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.