使用 PureConfig 将空 属性 解析为空 Map

Parsing an empty property into an empty Map with PureConfig

我有一个案例 class 如下所示:

case class MyConfig(arg1:String, extraArgs:Map[String,String])

我希望可以从 HOCON 配置中解析以下内容

{
    arg1 = 'hello world'
}

然而这失败了,因为 extraArgs 没有定义。在我的例子 class 中,我可以使 extraArgs 可选,但这将是多余的。有没有办法指示 PureConfig 将不存在的字段解析为空集合?

正如@LuisMiguelMejíaSuárez 在评论中建议的那样,您应该添加一个默认参数:

import pureconfig._
import pureconfig.generic.auto._
import com.typesafe.config.ConfigFactory

case class MyConfig(url: String, m: Map[String, String] = Map.empty)

val config1 = ConfigFactory.parseString("""{ "url": "https" }""")
val config2 = ConfigSource.fromConfig(config1).load[MyConfig]
println(config2)

代码 运行 在 Scastie