Typesafe/Hocon config: variable substitution: reference path
Asked Answered
P

2

17

We have a project with huge configuration files built using hocon configs. There is an intention to use variables to create template_section and set up some values in the template based on some options.

The problem is that while using variables in this config, I have to refer to the absolute path all the time. Is it possible somehow to use the canonical name (if properties located on the same level)?

Example:

foo {
    bar = 4
    baz = ${foo.bar} // work perfect
    baz = ${[this].bar} // can I do smth like that? Any ideas.
}

A more real-life example. What I'm actually looking for is general OOP abilities in building hocon configs. I have some parent configuration template_config with important_option inside that really depends on implementation: custom_config1 or custom_config2, I currently have to implement important_option in both child configurations because with absolute paths, I have to refer to custom config sections names.

custom_config1: $template_config {
    child_option = child_value1
}

custom_config2: $template_config {
    child_option = child_value2
}

template_config {
    important_option = ${child_option} // NOT POSSIBLE
    child_option = not_implemented
}
Presently answered 15/2, 2016 at 14:54 Comment(5)
What you mean by 'reach config.'? Was that meant to be 'rich'? By reading the specs, it seems that what you want is not possible. paths are absolute, not relative. github.com/typesafehub/config/blob/master/…Lippe
thx, I've checked spec as well and had not found any options.Presently
I think your best bet here is to wrap this logic you want in the Scala configuration object.Lippe
Shouldn't you write the full path from the root, i.e. template_config.child_option.Octopus
This is also a problem when the variable is inside an unnamed config, for example inside an array of configs. Then you can't refer to it by it's path e.g. outer { list : [ { anonymous : config }, { another : config } ] }Hawaii
C
2

Sadly, your hypothetical baz = ${[this].bar} path self-reference is not a thing that is supported by HOCON (as of 2022)

Chemisette answered 24/2, 2022 at 22:5 Comment(0)
G
0

I was able to do this!

application.conf:

application {
  child: ${other.test.someKey.someValue}
}

other: { include "test.conf" }

test.conf:

test {
 someKey: "Hello World!"
}

resulting config:

application {
 child: "Hello World!"
}
Gossip answered 6/6 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.