Play (Scala) 类型安全配置中的*有序*对象
*Ordered* objects in Play (Scala) typesafe config
如何在 Play 2.6.x (Scala) 的以下 .conf 中访问 customers
的 ordered 列表:
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")
,在这个例子中。
class SomeClass @Inject()(config: Configuration) {
Logger.debug("Customers from config: " + config.underlying.getConfig("customers"))
}
会给你
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"}}}))
如果你想使用对象,你显然必须转换它。
以下示例应该有效:
val config : Configuration = ???
config.getObject("customers").entrySet().asScala.map(_.getKey).toList
编辑
如果客户按字典顺序排列,您可以打电话订购 .sorted
如果更改您的配置不会影响您已经实现的逻辑,那么您可以像这样重构您的配置:
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"
}
}
]
并且使用 pureconfig 您可以执行以下操作:
import pureconfig.loadConfigOrThrow
final case class Named(name: String)
loadConfigOrThrow[List[Named]]("customers").map(_.name)
如何在 Play 2.6.x (Scala) 的以下 .conf 中访问 customers
的 ordered 列表:
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")
,在这个例子中。
class SomeClass @Inject()(config: Configuration) {
Logger.debug("Customers from config: " + config.underlying.getConfig("customers"))
}
会给你
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"}}}))
如果你想使用对象,你显然必须转换它。
以下示例应该有效:
val config : Configuration = ???
config.getObject("customers").entrySet().asScala.map(_.getKey).toList
编辑
如果客户按字典顺序排列,您可以打电话订购 .sorted
如果更改您的配置不会影响您已经实现的逻辑,那么您可以像这样重构您的配置:
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"
}
}
]
并且使用 pureconfig 您可以执行以下操作:
import pureconfig.loadConfigOrThrow
final case class Named(name: String)
loadConfigOrThrow[List[Named]]("customers").map(_.name)