Commit d4b8ed2c authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Added yaml support

parent 04f35d97
......@@ -110,5 +110,10 @@
<artifactId>scopt_2.10</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.15</version>
</dependency>
</dependencies>
</project>
......@@ -20,7 +20,10 @@ import nl.lumc.sasc.biopet.core.BiopetQScript
import nl.lumc.sasc.biopet.core.Logging
import nl.lumc.sasc.biopet.core.config.ConfigValue
import argonaut._, Argonaut._
import org.yaml.snakeyaml.Yaml
import scala.collection.mutable
import scalaz._, Scalaz._
import scala.collection.JavaConversions._
/**
* This object contains general function for the config
......@@ -78,8 +81,9 @@ object ConfigUtils extends Logging {
val value = map.get(path.head)
if (path.tail == Nil || value == None) value
else value.get match {
case map: Map[_, _] => getValueFromPath(map.asInstanceOf[Map[String, Any]], path.tail)
case _ => None
case map: Map[_, _] => getValueFromPath(map.asInstanceOf[Map[String, Any]], path.tail)
case map: java.util.LinkedHashMap[_, _] => getValueFromPath(map.toMap.asInstanceOf[Map[String, Any]], path.tail)
case _ => None
}
}
......@@ -105,9 +109,24 @@ object ConfigUtils extends Logging {
* @return Config map
*/
def fileToConfigMap(configFile: File): Map[String, Any] = {
val configJson = jsonToMap(fileToJson(configFile))
logger.debug("Contain: " + configJson)
return configJson
val configMap = {
if (configFile.getName.endsWith(".yaml")) yamlToMap(configFile)
else jsonToMap(fileToJson(configFile))
}
logger.debug("Contain: " + configMap)
return configMap
}
/**
* Convert a yaml file to map[String, Any]
* @param file Input file
* @return config map
*/
def yamlToMap(file: File): Map[String, Any] = {
val yaml = new Yaml()
val a = yaml.load(scala.io.Source.fromFile(file).reader())
ConfigUtils.any2map(a)
}
/**
......@@ -331,11 +350,22 @@ object ConfigUtils extends Logging {
def any2map(any: Any): Map[String, Any] = {
if (any == null) return null
any match {
case m: Map[_, _] => m.map(x => x._1.toString -> x._2)
case _ => throw new IllegalStateException("Value '" + any + "' is not an Map")
case m: Map[_, _] => m.map(x => x._1.toString -> x._2)
case m: java.util.LinkedHashMap[_, _] => nestedJavaHashMaptoScalaMap(m)
case _ => throw new IllegalStateException("Value '" + any + "' is not an Map")
}
}
/** Convert nested java hash map to scala hash map */
def nestedJavaHashMaptoScalaMap(input: java.util.LinkedHashMap[_, _]): Map[String, Any] = {
input.map(value => {
value._2 match {
case m: java.util.LinkedHashMap[_, _] => value._1.toString -> nestedJavaHashMaptoScalaMap(m)
case _ => value._1.toString -> value._2
}
}).toMap
}
/**
* Trait for implicit conversions for ConfigValue to native scala values
*/
......
......@@ -141,7 +141,7 @@ object ConfigTest {
)
)
val file = ConfigUtilsTest.writeTemp(ConfigUtils.mapToJson(map).spaces2)
val file = ConfigUtilsTest.writeTemp(ConfigUtils.mapToJson(map).spaces2, "json")
val config = new Config
config.loadConfigFile(file)
......
......@@ -205,8 +205,8 @@ class ConfigUtilsTest extends TestNGSuite with Matchers {
}
}
object ConfigUtilsTest {
def writeTemp(text: String): File = {
val file = File.createTempFile("TestConfigUtils.", ".json")
def writeTemp(text: String, extension: String): File = {
val file = File.createTempFile("TestConfigUtils.", extension)
val w = new PrintWriter(file)
w.write(text)
w.close()
......@@ -229,7 +229,7 @@ object ConfigUtilsTest {
|}
""".stripMargin
val file1 = writeTemp(jsonText1)
val file1 = writeTemp(jsonText1, ".json")
val json1 = {
("int" := 1337) ->:
......@@ -262,7 +262,7 @@ object ConfigUtilsTest {
|}
""".stripMargin
val file2 = writeTemp(jsonText2)
val file2 = writeTemp(jsonText2, ".yaml")
val json2 = {
("int" := 7331) ->:
......@@ -283,5 +283,5 @@ object ConfigUtilsTest {
|}
""".stripMargin
val corruptFile = writeTemp(corruptJson)
val corruptFile = writeTemp(corruptJson, ".json")
}
\ No newline at end of file
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