TypeSafeConfig 和 PureConfig - 从配置中加载一个 Map[String, Any] 值

TypeSafeConfig and PureConfig - load a Map[String, Any] value from config

我正在尝试从配置文件加载 Map[String, Any]。目前是这样写的

map-name {
   stringValue = "firstValue"
   intValue = 1
   booleanValue = true
  }

Pureconfig 无法将此配置读取为 Map[String, Any]。它仅在将 Any 替换为某些严格类型时才有效,但我想要比这更灵活。

有什么解决办法吗?

Is there any way around this?

是的。您可以使用这种类型:Map[String, ConfigValue]

ConfigValue 来自其 Scala 文档:

An immutable value, following the JSON type schema.

但是你可以使用 ConfigObject 而不是 Map[String, ConfigValue],因为这是一回事。

您现在可以像 JSON-Object 结构一样处理它。

这里有一些例子:java-api-usage-examples

I just want to replace values of an existing config. If I read in the whole config from the config file, I'll have to specify all the other values, even the ones I don't want to change.

这是newConfig.withFallback(oldConfig)

你可以有这样的东西:

import scala.collection.JavaConverters._
implicit val mapReader = pureconfig.ConfigReader[ConfigObject].map(co => co.unwrapped().asScala.toMap)