类型安全配置中的

类型安全配置中的

本文介绍了Play(Scala)类型安全配置中的* Ordered *对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Play 2.6.x(Scala)的以下.conf中访问>的排序列表:

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"),在此示例中.

推荐答案

以下示例应该有效:

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

修改

如果客户按词典顺序排序,则可以致电.sorted

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"
    }
  }
]

,并使用 pureconfig ,您可以执行以下操作:

and with pureconfig you can do the following:

import pureconfig.loadConfigOrThrow

final case class Named(name: String)

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

这篇关于Play(Scala)类型安全配置中的* Ordered *对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 10:23