【问题标题】:*Ordered* objects in Play (Scala) typesafe configPlay (Scala) 类型安全配置中的 *Ordered* 对象
【发布时间】:2018-07-20 19:38:46
【问题描述】:

如何在 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"),在这个例子中。

【问题讨论】:

    标签: scala playframework typesafe-config


    【解决方案1】:

    下面的例子应该可以工作:

    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)
    

    【讨论】:

      【解决方案2】:
      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"}}}))
      

      如果您想使用对象,显然必须对其进行转换。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-20
        • 2018-06-15
        • 1970-01-01
        相关资源
        最近更新 更多