【问题标题】:How to define an object as environment variable with hocon config如何使用 hocon config 将对象定义为环境变量
【发布时间】:2021-08-03 18:04:21
【问题描述】:

我想做什么

定义可以通过环境变量增强的基本对象集。 在下面你会看到我的 hocon 配置文件的一个小例子。

foo.bar-base= [
  {a: "hello", b: "world"},
  {a: "hello", b: "stackoverflow"}
]
foo.bar-extended = ${?EXTENDED}
foo.bar = ${foo.bar-base} ${foo.bar-extended}

问题

在尝试定义要添加为环境变量的元素时出现异常 这表明 foo.bar 具有 STRING 类型列表而不是 OBJECT 列表。

有没有办法让环境变量的值被解析为对象?

EXTENDED.0={a:"hello", b:"rest"}

【问题讨论】:

    标签: hocon


    【解决方案1】:

    我使用了一种解决方法。我没有使用直接返回Config 对象列表的config.getConfigList,而是使用了返回ConfigValues 列表的config.getList

    然后我手动解析:

    final ConfigList list = config.getList("foo.bar");
    final Stream<Config> configsFromString = list.stream()
                        .filter(value -> ConfigValueType.STRING.equals(value.valueType()))
                        .map(ConfigValue::unwrapped)
                        .map(Object::toString)
                        .map(ConfigFactory::parseString);
    final Stream<Config> configsFromMap = list.stream()
                        .filter(value -> ConfigValueType.OBJECT.equals(value.valueType()))
                        .map(ConfigValue::render)
                        .map(ConfigFactory::parseString);
    final List<Config> configs = Stream.concat(configsFromString, configsFromMap)
                        .collect(Collectors.toList());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多