gpt4 book ai didi

scala 加载配置 Map 的 Map

转载 作者:行者123 更新时间:2023-12-02 19:41:27 25 4
gpt4 key购买 nike

我需要从配置文件中读取并将配置映射到案例类。如果我有一个如下表,它就可以正常工作

配置

mapping {
target {
oracle = {
type = "oracle"
schema = "orcl"
tableName = "my_table"
query = "select key from my_table where dob='2020-01-01'
}
}

SCALA 代码片段

 val targetConfig:Map[String,QueryEngine] = config.getObject("mapping.target")
.entrySet()
.asScala
.foldLeft(Map.empty[String , QueryEngine]) { case ( acc , entry ) =>
val target = entry.getKey
val targetConfig = entry.getValue match {
case validElement if validElement.valueType() == ConfigValueType.OBJECT => validElement.asInstanceOf[ConfigObject].toConfig
case invalidElement => sys.error("illegal syntax at $invalidElement")
}

targetConfig.getString("type") match {

case "oracle" => acc + (target -> new OracleQueryEngine(vars,target,targetConfig.getString("schema"),targetConfig.getString("tableName"),targetConfig.getString("query"),targetConfig.getString("param")))

case x => sys.error(s"unknow target not defined $targetConfig with $targetConfig")
}
}

现在我用目标映射中的多个表更新了 CONFIG。

mapping {
target {
oracle =
emp = {
type = "oracle"
schema = "orcl"
tableName = "emp"
query = "select key from emp where dob='2020-01-01'
}
dept = {
type = "oracle"
schema = "orcl"
tableName = "dept"
query = "select key from dept where dob='2020-01-01'
}
}
}

多表场景的代码片段

This is giving error 
Error:(22, 28) type mismatch;
found : scala.collection.mutable.Buffer[scala.collection.immutable.Map[String,QueryEngine]]
required: scala.collection.immutable.Map[String,QueryEngine]
objs.asScala.map { obj =>

代码片段

val sourcesConfig: Map[String, QueryEngine] = 
config.getObject("mapping.target")
.entrySet()
.asScala
.foldLeft(Map.empty[String, QueryEngine]) { case (acc, entry) =>
val source = entry.getKey

entry.getValue match {
case objs:ConfigList =>
objs.asScala.map { obj =>
val sourceConfig = obj.asInstanceOf[ConfigObject].toConfig
sourceConfig.getString("type") match {
case "oracle" => acc + (source -> new OracleQueryEngine(vars,source,sourceConfig.getString("schema"), sourceConfig.getString("tableName"), sourceConfig.getString("query"), sourceConfig.getString("param")))
case x => sys.error(s"unknown source not defined $source with $sourceConfig")

}

}
}

最佳答案

将“oracle”括在大括号中,因为它是 JsObject:

mapping {
target {
oracle = {
emp = {
type = "oracle"
schema = "orcl"
tableName = "emp"
query = "select key from emp where dob='2020-01-01'
}
dept = {
type = "oracle"
schema = "orcl"
tableName = "dept"
query = "select key from dept where dob='2020-01-01'
}
}}
}

现在可以用了吗?

如果不使用例如 PureConfig .

您的解决方案可以像这样简单:

import pureconfig._
import pureconfig.generic.auto._

case class Mappings(mapping: Mapping)
case class Mapping(target: Oracle)
case class Oracle(oracle: Map[String, Map[String, String]])

ConfigSource.default.load[Mappings]

关于scala 加载配置 Map 的 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60075457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com