*Ordered* objects in Play (Scala) typesafe config

2019-07-29 17:42发布

How do I access the ordered list of customers in the following .conf in Play 2.6.x (Scala):

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.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-29 18:21

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

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:

import pureconfig.loadConfigOrThrow

final case class Named(name: String)

loadConfigOrThrow[List[Named]]("customers").map(_.name)
查看更多
女痞
3楼-- · 2019-07-29 18:33
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.

查看更多
登录 后发表回答