地图的标量负载配置地图

问题描述 投票:2回答:1

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

配置

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

现在我用目标映射中的MULTIPLE表更新了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")

        }

      }
  }
scala scala-collections typesafe-config
1个回答
0
投票
将“ 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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.