简体   繁体   中英

*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):

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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