【问题标题】:Typesafe Config: How to get list of listsTypesafe Config:如何获取列表列表
【发布时间】:2021-05-14 17:39:56
【问题描述】:

我有一个结构如下的配置文件:

# ExampleConfig
exampleConfig {
  steps = [
    ["app_one", "step_one", "step_two"],
    ["app_two", "step_one", "step_two"]
  ]
  tags = [
    ["owner", "me"],
    ["env", "prod"],
    ["tenant", "me"]
  ]
}

我要做的是将配置文件加载到应用程序中,然后从配置文件中提取列表(例如步骤、标签)。我有点坚持如何去做。我尝试使用以下方法,但它们没有返回我想要的结果:

val config: Config = ConfigFactory.load(configFile).getConfig(configValue)
val steps = config.getList("steps")

我想要的最终结果如下:

val steps: List[(String, String, String)] = List(("app_one", "step_one", "step_two"), ...))
val tags: List[(String, String)] = List(("owner", "me"),("env", "prod"), ...))

【问题讨论】:

  • 在此之上使用pureconfig 怎么样?
  • @LuisMiguelMejíaSuárez 我对 pureconfig 不熟悉。我得稍微研究一下。谢谢。
  • 弗莱彻,我的回答对你有帮助吗?

标签: list scala typesafe-config


【解决方案1】:

一般来说,json中不存在元组的概念。因此,您必须在应用程序中强制转换为元组,而不是在 json 中。最后一点的含义是,您不能编写可以转换任意数量参数的通用代码。

你可以试试这样的:

val config = ConfigFactory.parseString(configString)
val steps = config.getList("steps").asScala.map {
  case internalList: ConfigList =>
    if (internalList.size() != 3) ???
    internalList.asScala.map(_.unwrapped()) match {
      case mutable.Buffer(a1, a2, a3) =>
        (a1, a2, a3)
    }
}.toList

val tags = config.getList("tags").asScala.map {
  case internalList: ConfigList =>
    if (internalList.size() != 2) ???
    internalList.asScala.map(_.unwrapped()) match {
      case mutable.Buffer(a1, a2) =>
        (a1, a2)
    }
}.toList

println(steps)
println(tags)

代码在Scastie 运行。

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 2016-11-27
    • 2014-03-11
    • 2019-09-03
    • 2019-12-31
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多