【发布时间】:2018-07-12 17:47:19
【问题描述】:
我想访问以下配置
customers {
"cust1" {
clientId: "id1"
attrs {
att1: "str1"
att2: "str2"
att3: "str3"
att4: "str4"
}
}
"cust2" {
clientId: "id2"
attrs: {
att1: "faldfjalfj"
att2: "reqwrewrqrq"
}
}
"cust3" {
clientId: "id3"
attrs {
att2: "xvcbzxbv"
}
}
}
作为Map[String, CustomerConfig] CustomerConfig 是
package models
import play.api.ConfigLoader
import com.typesafe.config.Config // I added this import in order to make the documentation snippet compile to the best of my knowledge.
case class CustomerConfig(clientId: String, attrs: Map[String, String])
object CustomerConfig {
implicit val configLoader: ConfigLoader[CustomerConfig] = new ConfigLoader[CustomerConfig] {
def load(rootConfig: Config, path: String): CustomerConfig = {
val config = rootConfig.getConfig(path)
CustomerConfig(
clientId = config.getString("clientId"),
attrs = config.get[Map[String, String]]("attrs").map { case (attr, attrVal) =>
(attr, attrVal)
})
}
}
}
作为参考,这是我目前尝试引用它的方式:
val resellerEnvMap = conf.get[Map[String, CustomerConfig]]("customers").map {
case (customer, customerConfig) =>
customer -> customerConfig.attrs.map {
case (attr, attrVal) =>
attr -> new Obj(attrVal, customerConfig.clientId)
}
}
基于custom config loader documentation。
问题是config.get[A] 不存在(config.getMap[K, V] 也不存在),我认为是API docs。我想要一种从配置文件中填充 map 的方法。我的最终目标是填充 Map[String, Map[String, (String, String)]] 功能附近的任何内容,其中第一个 String 是客户名称,第二个是属性名称,第三个是属性值,最后,第四个是客户 ID。
【问题讨论】:
标签: scala playframework configuration typesafe-config