scalaconfigtypesafetypesafe-configtypesafe-stack

write optional value programmatically to Typesafe Config Object


I am trying to create a TypeSafe config with optional values ​​to end up writing the configuration into a file.

The problem is that when I do

 val newConfig = config
.withValue("something",ConfigValueFactory.fromAnyRef("defaultvalue"))
.withValue("something", ConfigValueFactory.fromAnyRef("${?optional}"))

the result is something like:

{something: ${?optional}}

but I want something like:

{something: defaultValue, something: ${?optional}}

Is possible to do that programamtically??


Solution

  • The problem with withValue is that the last call will just return a new object with only the new value. You could use withFallback, except it will have the same problem because the value is being resolved while you construct the object.

    I played with this for a while, but it seems the Typesafe Config API is expressed in terms of the resolved object model, rather than the HOCON AST, making yours an unsupported use case. I would recommend you open a feature request in their GitHub repo here: https://github.com/lightbend/config/issues