Commit ff2ecf79 authored by Peter van 't Hof's avatar Peter van 't Hof

Moved methods from config object to utils

parent a10513a4
......@@ -2,6 +2,7 @@ package nl.lumc.sasc.biopet.core.config
import java.io.File
import nl.lumc.sasc.biopet.core.Logging
import nl.lumc.sasc.biopet.utils.ConfigUtils._
import argonaut._, Argonaut._
import scalaz._, Scalaz._
......@@ -36,11 +37,11 @@ class Config(var map: Map[String, Any]) extends Logging {
throw new IllegalStateException("The config JSON file is either not properly formatted or not a JSON file, file: " + configFile)
}
logger.debug(json)
val configJson = Config.jsonToMap(json.get)
val configJson = jsonToMap(json.get)
logger.debug("Contain: " + configJson)
if (map.isEmpty) map = configJson
else map = Config.mergeMaps(configJson, map)
else map = mergeMaps(configJson, map)
logger.debug("New config: " + map)
}
......@@ -112,66 +113,13 @@ class Config(var map: Map[String, Any]) extends Logging {
object Config extends Logging {
val global = new Config
def valueToMap(input: Any): Map[String, Any] = {
input match {
case m: Map[_, _] => return m.asInstanceOf[Map[String, Any]]
case _ => throw new IllegalStateException("Value '" + input + "' is not an Map")
}
}
def mergeMaps(map1: Map[String, Any], map2: Map[String, Any]): Map[String, Any] = {
var newMap: Map[String, Any] = Map()
for (key <- map1.keySet.++(map2.keySet)) {
if (!map2.contains(key)) newMap += (key -> map1(key))
else if (!map1.contains(key)) newMap += (key -> map2(key))
else {
map1(key) match {
case m1: Map[_, _] => {
map2(key) match {
case m2: Map[_, _] => newMap += (key -> mergeMaps(Config.valueToMap(m1), Config.valueToMap(m2)))
case _ => newMap += (key -> map1(key))
}
}
case _ => newMap += (key -> map1(key))
}
}
}
return newMap
}
def mergeConfigs(config1: Config, config2: Config): Config = new Config(mergeMaps(config1.map, config2.map))
private def jsonToMap(json: Json): Map[String, Any] = {
var output: Map[String, Any] = Map()
if (json.isObject) {
for (key <- json.objectFieldsOrEmpty) {
val value: Any = jsonToAny(json.field(key).get)
output += (key -> value)
}
} else return null
return output
}
private def jsonToAny(json: Json): Any = {
if (json.isObject) return jsonToMap(json)
else if (json.isArray) {
var list: List[Any] = List()
for (value <- json.array.get) list ::= jsonToAny(value)
return list
} else if (json.isBool) return json.bool.get
else if (json.isString) return json.string.get.toString
else if (json.isNumber) {
val num = json.number.get
if (num.toString.contains(".")) return num.toDouble
else return num.toLong
} else throw new IllegalStateException("Config value type not supported, value: " + json)
}
private def getMapFromPath(map: Map[String, Any], path: List[String]): Map[String, Any] = {
var returnMap: Map[String, Any] = map
for (m <- path) {
if (!returnMap.contains(m)) return Map()
else returnMap = Config.valueToMap(returnMap(m))
else returnMap = any2map(returnMap(m))
}
return returnMap
}
......@@ -204,28 +152,4 @@ object Config extends Logging {
return None
}
}
def mapToJson(map: Map[String, Any]): Json = {
map.foldLeft(jEmptyObject)((acc, kv) => (kv._1 := {
kv._2 match {
case m: Map[_, _] => mapToJson(m.map(m => m._1.toString -> anyToJson(m._2)))
case _ => anyToJson(kv._2)
}
}) ->: acc)
}
def anyToJson(any: Any): Json = {
any match {
case j: Json => j
case m: Map[_, _] => mapToJson(m.map(m => m._1.toString -> anyToJson(m._2)))
case l: List[_] => Json.array(l.map(anyToJson(_)): _*)
case n: Int => Json.jNumberOrString(n)
case n: Double => Json.jNumberOrString(n)
case n: Long => Json.jNumberOrString(n)
case n: Short => Json.jNumberOrString(n)
case n: Float => Json.jNumberOrString(n)
case n: Byte => Json.jNumberOrString(n)
case _ => jString(any.toString)
}
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import java.io.File
import nl.lumc.sasc.biopet.core.ToolCommand
import scala.io.Source
import nl.lumc.sasc.biopet.core.config.Config
import nl.lumc.sasc.biopet.utils.ConfigUtils._
object SamplesTsvToJson extends ToolCommand {
case class Args(inputFiles: List[File] = Nil) extends AbstractArgs
......@@ -37,10 +38,10 @@ object SamplesTsvToJson extends ToolCommand {
}
map
}
librariesValues.foldLeft(Map[String, Any]())((acc, kv) => Config.mergeMaps(acc, kv))
librariesValues.foldLeft(Map[String, Any]())((acc, kv) => mergeMaps(acc, kv))
}
val map = fileMaps.foldLeft(Map[String, Any]())((acc, kv) => Config.mergeMaps(acc, kv))
val json = Config.mapToJson(map)
val map = fileMaps.foldLeft(Map[String, Any]())((acc, kv) => mergeMaps(acc, kv))
val json = mapToJson(map)
println(json.spaces2)
}
}
......@@ -3,8 +3,80 @@ package nl.lumc.sasc.biopet.utils
import java.io.File
import nl.lumc.sasc.biopet.core.Logging
import nl.lumc.sasc.biopet.core.config.ConfigValue
import argonaut._, Argonaut._
import scalaz._, Scalaz._
object ConfigUtils extends Logging {
def mergeMaps(map1: Map[String, Any], map2: Map[String, Any]): Map[String, Any] = {
var newMap: Map[String, Any] = Map()
for (key <- map1.keySet.++(map2.keySet)) {
if (!map2.contains(key)) newMap += (key -> map1(key))
else if (!map1.contains(key)) newMap += (key -> map2(key))
else {
map1(key) match {
case m1: Map[_, _] => {
map2(key) match {
case m2: Map[_, _] => newMap += (key -> mergeMaps(any2map(m1), any2map(m2)))
case _ => newMap += (key -> map1(key))
}
}
case _ => newMap += (key -> map1(key))
}
}
}
return newMap
}
def jsonToMap(json: Json): Map[String, Any] = {
var output: Map[String, Any] = Map()
if (json.isObject) {
for (key <- json.objectFieldsOrEmpty) {
val value: Any = jsonToAny(json.field(key).get)
output += (key -> value)
}
} else return null
return output
}
def jsonToAny(json: Json): Any = {
if (json.isObject) return jsonToMap(json)
else if (json.isArray) {
var list: List[Any] = List()
for (value <- json.array.get) list ::= jsonToAny(value)
return list
} else if (json.isBool) return json.bool.get
else if (json.isString) return json.string.get.toString
else if (json.isNumber) {
val num = json.number.get
if (num.toString.contains(".")) return num.toDouble
else return num.toLong
} else throw new IllegalStateException("Config value type not supported, value: " + json)
}
def mapToJson(map: Map[String, Any]): Json = {
map.foldLeft(jEmptyObject)((acc, kv) => (kv._1 := {
kv._2 match {
case m: Map[_, _] => mapToJson(m.map(m => m._1.toString -> anyToJson(m._2)))
case _ => anyToJson(kv._2)
}
}) ->: acc)
}
def anyToJson(any: Any): Json = {
any match {
case j: Json => j
case m: Map[_, _] => mapToJson(m.map(m => m._1.toString -> anyToJson(m._2)))
case l: List[_] => Json.array(l.map(anyToJson(_)): _*)
case n: Int => Json.jNumberOrString(n)
case n: Double => Json.jNumberOrString(n)
case n: Long => Json.jNumberOrString(n)
case n: Short => Json.jNumberOrString(n)
case n: Float => Json.jNumberOrString(n)
case n: Byte => Json.jNumberOrString(n)
case _ => jString(any.toString)
}
}
def any2string(any: Any): String = {
if (any == null) return null
any match {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment