简体   繁体   English

Play(Scala)类型安全配置中的* Ordered *对象

[英]*Ordered* objects in Play (Scala) typesafe config

How do I access the ordered list of customers in the following .conf in Play 2.6.x (Scala): 如何在Play 2.6.x(Scala)的以下.conf中访问已订购customers列表:

customers {
  "cust1" {
    env1 {
      att1: "str1"
      att2: "str2"
    }
    env2 {
      att1: "str3"
      att2: "str5"
    }
    env3 {
      att1: "str2"
      att2: "str6"
    }
    env4 {
      att1: "str1"
      att2: "str2"
    }
  }
  "cust2" {
    env1 {
      att1: "faldfjalfj"
      att2: "reqwrewrqrq"
    }
    env2 {
      att1: "falalfj"
      att2: "reqwrrq"
    }
  }
  "cust3" {
    env3 {
      att1: "xvcbzxbv"
      att2: "hello"
    }
  }
}

List("cust1", "cust2", "cust3") , in this example. 在此示例中为List("cust1", "cust2", "cust3")

class SomeClass @Inject()(config: Configuration) {
  Logger.debug("Customers from config: " + config.underlying.getConfig("customers"))
}

Will give you. 会给你。

Customers from config: Config(SimpleConfigObject(
    {"cust1":{
        "env1":{"att1":"str1","att2":"str2"},
        "env2":{"att1":"str3","att2":"str5"},
        "env3":{"att1":"str2","att2":"str6"},
        "env4":{"att1":"str1","att2":"str2"}},
    "cust2":{
        "env1":{"att1":"faldfjalfj","att2":"reqwrewrqrq"},
        "env2":{"att1":"falalfj","att2":"reqwrrq"}},
   "cust3":{
        "env3":{"att1":"xvcbzxbv","att2":"hello"}}}))

You obviously have to then transform this if you want to work with objects. 如果要使用对象,显然必须转换此格式。

The following example should work: 下面的示例应该起作用:

val config : Configuration = ???
config.getObject("customers").entrySet().asScala.map(_.getKey).toList

Edit 编辑

If customers are in lexicographical order than you can order call .sorted 如果客户按词典顺序排序,则可以订购.sorted

If changing your config doesn't affect your already implemented logic than you can restructure your config like this: 如果更改配置不会影响已经实现的逻辑,则可以像下面这样重构配置:

customers : [
  {
    name : "cust1"
    env1 {
      att1: "str1"
      att2: "str2"
    }
    env2 {
      att1: "str3"
      att2: "str5"
    }
    env3 {
      att1: "str2"
      att2: "str6"
    }
    env4 {
      att1: "str1"
      att2: "str2"
    }
  }
   {
    name : "cust2"
    env1 {
      att1: "faldfjalfj"
      att2: "reqwrewrqrq"
    }
    env2 {
      att1: "falalfj"
      att2: "reqwrrq"
    }
  }
  {
    name: "cust3"
    env3 {
      att1: "xvcbzxbv"
      att2: "hello"
    }
  }
  {
    name : "bob"
    env1 {
      att1: "str1"
      att2: "str2"
    }
    env2 {
      att1: "str3"
      att2: "str5"
    }
    env3 {
      att1: "str2"
      att2: "str6"
    }
    env4 {
      att1: "str1"
      att2: "str2"
    }
  }
  {
    name : "john"
    env1 {
      att1: "faldfjalfj"
      att2: "reqwrewrqrq"
    }
    env2 {
      att1: "falalfj"
      att2: "reqwrrq"
    }
  }
  {
    name: "jack"
    env3 {
      att1: "xvcbzxbv"
      att2: "hello"
    }
  }
]

and with pureconfig you can do the following: 使用pureconfig可以执行以下操作:

import pureconfig.loadConfigOrThrow

final case class Named(name: String)

loadConfigOrThrow[List[Named]]("customers").map(_.name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM