Reputation: 842
I need to read from a config file and map the config to a case class .It works fine if i have one table as below
CONFIG
mapping {
target {
oracle = {
type = "oracle"
schema = "orcl"
tableName = "my_table"
query = "select key from my_table where dob='2020-01-01'
}
}
SCALA CODE SNIPPET
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")
}
}
NOW i updated CONFIG with MULTIPLE tables in the target mapping.
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'
}
}
}
CODE SNIPPET for the multiple table scenario
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 =>
CODE SNIPPET
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")
}
}
}
Upvotes: 2
Views: 519
Reputation: 14803
Wrap "oracle" in curly brackets as it is a 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'
}
}}
}
Does it work now?
If not use for example PureConfig.
Your solution could be as simple as this:
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]
Upvotes: 5