scala - Pureconfig 将配置读取为属性映射

标签 scala pureconfig

是否可以使 pureconfig 读取属性为 Map[String, String]?我有以下内容

application.conf:

cfg{
  some.property.name: "value"
  some.another.property.name: "another value"
}

这是我尝试读取配置的应用程序:

import pureconfig.generic.auto._
import pureconfig.ConfigSource
import pureconfig.error.ConfigReaderException

object Model extends App {
  case class Config(cfg: Map[String, String])

  val result = ConfigSource.default
    .load[Config]
    .left
    .map(err => new ConfigReaderException[Config](err))
    .toTry

  val config = result.get
  println(config)
}

问题是它抛出以下异常:

Exception in thread "main" pureconfig.error.ConfigReaderException: Cannot convert configuration to a Model$Config. Failures are:
  at 'cfg.some':
    - (application.conf @ file:/home/somename/prcfg/target/classes/application.conf: 2-3) Expected type STRING. Found OBJECT instead.

    at Model$.$anonfun$result$2(Model.scala:11)
    at scala.util.Either$LeftProjection.map(Either.scala:614)
    at Model$.delayedEndpoint$Model$1(Model.scala:11)
    at Model$delayedInit$body.apply(Model.scala:5)
    at scala.Function0.apply$mcV$sp(Function0.scala:39)
    at scala.Function0.apply$mcV$sp$(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
    at scala.App.$anonfun$main$1(App.scala:73)
    at scala.App.$anonfun$main$1$adapted(App.scala:73)
    at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:553)
    at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:551)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:920)
    at scala.App.main(App.scala:73)
    at scala.App.main$(App.scala:71)
    at Model$.main(Model.scala:5)
    at Model.main(Model.scala)

有办法解决吗?我预计 Map[String, String] 将包含以下映射:

some.property.name -> "value"
some.another.property.name -> "another value"

最佳答案

您的问题不是 pureconfig。你的问题是,根据 HOCON 规范,你写了什么:

cfg {
  some.property.name: "value"
  some.another.property.name: "another value"
}

是语法糖:

cfg {
  some {
    property {
      name = "value"
    }
  }
  
  another {
    property {
      name = "another value"
    }
  }
}

是 TypeSafe Config/Lightbend Config 决定你的 cfg 有两个属性,而且它们都是嵌套配置。 Pureconfig 只接受这些嵌套的配置并将它们映射到案例类中。但它无法映射结构与预期完全不同的东西。

如果你写:

cfg {
  some-property-name: "value"
  some-another-property-name: "another value"
}

您将能够将 "cfg" 路径解码为 Map[String, String] 并将顶级配置解码为 case class Config(cfg: Map [字符串,字符串])。如果您想将 . 视为 key 的一部分而不是嵌套......那么恐怕您必须自己编写一个 ConfigReader 因为这是非标准用法.

关于scala - Pureconfig 将配置读取为属性映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64395800/

相关文章:

scala - 哪些特定功能使Scala成为比Groovy更具“功能性”的语言?

java - 给定它所在的类的类型别名,如何从 Scala 访问 Java 静态方法

scala - Play WS - 检查压缩头

Scala:使用 PureConfig 创建通用工具

java - ActionBarSherlock 不再编译

scala - 为什么在 Scala Breeze 中对这个 3x2 double 矩阵调用均值是非法的?

scala - 在 pureconfig 中表示 Either

scala - SBT 在编译期间抛出 StackOverflowError

scala - pureconfig无法派生具有许多属性的案例类

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