【发布时间】:2016-05-26 13:04:02
【问题描述】:
我们有一个项目,其中包含使用 hocon configs 构建的大量配置文件。
打算使用变量来创建template_section,并根据一些选项在模板中设置一些值。
问题是在这个配置中使用variables 时,我必须一直引用绝对路径。
是否可以以某种方式使用规范名称(如果属性位于同一级别)?
例子:
foo {
bar = 4
baz = ${foo.bar} // work perfect
baz = ${[this].bar} // can I do smth like that? Any ideas.
}
一个更真实的例子。我真正在寻找的是构建 hocon 配置的一般 OOP 能力。
我有一些父配置 template_config 和 important_option 里面真的取决于实现:
custom_config1 或 custom_config2,我目前必须在两个子配置中实现 important_option,因为使用绝对路径,我必须引用自定义配置部分的名称。
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
}
【问题讨论】:
-
你所说的“到达配置”是什么意思?那是不是意味着“富有”?通过阅读规格,您想要的似乎是不可能的。路径是绝对的,而不是相对的。 github.com/typesafehub/config/blob/master/…
-
谢谢,我也检查了规范,但没有找到任何选项。
-
我认为你最好的选择是将这个逻辑包装在 Scala 配置对象中。
-
你不应该从根写完整路径,即
template_config.child_option。 -
当变量在未命名的配置中时,这也是一个问题,例如在配置数组中。然后你不能通过它的路径来引用它,例如
outer { list : [ { anonymous : config }, { another : config } ] }
标签: scala typesafe typesafe-config