2

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

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

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

有没有办法解决?

4

3 回答 3

7

有没有办法解决?

就在这里。您可以使用这种类型:Map[String, ConfigValue]

ConfigValue来自其 Scala 文档:

一个不可变的值,遵循JSON类型架构。

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

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

以下是一些示例:java-api-usage-examples

于 2019-12-30T15:23:18.490 回答
1

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.

This is newConfig.withFallback(oldConfig).

于 2019-12-30T20:18:14.023 回答
0

你可以有这样的东西:

import scala.collection.JavaConverters._
implicit val mapReader = pureconfig.ConfigReader[ConfigObject].map(co => co.unwrapped().asScala.toMap)
于 2020-04-16T06:58:30.270 回答