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

Refactor getValueFromMap method

parent f33a18c0
...@@ -186,35 +186,49 @@ object Config extends Logging { ...@@ -186,35 +186,49 @@ object Config extends Logging {
/** /**
* Search for value in index position in a map * Search for value in index position in a map
* @param map Map to search in * @param map Map to search in
* @param index Config index * @param startIndex Config index
* @return Value * @return Value
*/ */
def getValueFromMap(map: Map[String, Any], index: ConfigValueIndex): Option[ConfigValue] = { def getValueFromMap(map: Map[String, Any], startIndex: ConfigValueIndex): Option[ConfigValue] = {
var submodules = index.path def getFromPath(path: List[String]): Option[ConfigValue] = {
while (!submodules.isEmpty) { val p = getValueFromPath(map, path ::: startIndex.module :: startIndex.key :: Nil)
var submodules2 = submodules if (p.isDefined) Option(ConfigValue(startIndex, ConfigValueIndex(startIndex.module, path, startIndex.key, freeVar = false), p.get))
while (!submodules2.isEmpty) { else if (startIndex.freeVar) {
val p = getMapFromPath(map, submodules2 ::: index.module :: Nil) getOrElse Map() val p = getValueFromPath(map, path ::: startIndex.key :: Nil)
if (p.contains(index.key)) { if (p.isDefined) Option(ConfigValue(startIndex, ConfigValueIndex(startIndex.module, path, startIndex.key, freeVar = true), p.get))
return Option(ConfigValue(index, ConfigValueIndex(index.module, submodules2, index.key, freeVar = false), p(index.key))) else None
} } else None
if (index.freeVar) { }
val p2 = getMapFromPath(map, submodules2) getOrElse Map()
if (p2.contains(index.key)) { def tailSearch(path: List[String]): Option[ConfigValue] = {
return Option(ConfigValue(index, ConfigValueIndex(index.module, submodules2, index.key, freeVar = true), p2(index.key))) val p = getFromPath(path)
} if (p != None) p
} else if (path == Nil) None
submodules2 = submodules2.init else {
val p = initSearch(path)
if (p.isDefined) p
else tailSearch(path.tail)
} }
submodules = submodules.tail
} }
val p = getMapFromPath(map, index.module :: Nil) getOrElse Map()
if (p.contains(index.key)) { // Module is not nested def initSearch(path: List[String], tail: List[String] = Nil): Option[ConfigValue] = {
return Option(ConfigValue(index, ConfigValueIndex(index.module, Nil, index.key, freeVar = false), p(index.key))) val p = getFromPath(path)
} else if (map.contains(index.key) && index.freeVar) { // Root value of json if (p.isDefined) p
return Option(ConfigValue(index, ConfigValueIndex("", Nil, index.key, freeVar = true), map(index.key))) else if (path == Nil) None
} else { // At this point key is not found on the path else {
return None val p = skipNested(path, tail)
if (p.isDefined) p
else initSearch(path.init, path.last :: tail)
}
} }
def skipNested(path: List[String], tail: List[String] = Nil): Option[ConfigValue] = {
val p = getFromPath(path ::: tail)
if (p.isDefined) p
else if (tail == Nil) None
else skipNested(path, tail.tail)
}
return tailSearch(startIndex.path)
} }
} }
\ No newline at end of file
package nl.lumc.sasc.biopet.core.config package nl.lumc.sasc.biopet.core.config
import nl.lumc.sasc.biopet.utils.ConfigUtils._
import nl.lumc.sasc.biopet.utils.{ ConfigUtilsTest, ConfigUtils } import nl.lumc.sasc.biopet.utils.{ ConfigUtilsTest, ConfigUtils }
import org.scalatest.Matchers import org.scalatest.Matchers
import org.scalatest.mock.MockitoSugar import org.scalatest.mock.MockitoSugar
...@@ -45,6 +46,13 @@ class ConfigTest extends TestNGSuite with MockitoSugar with Matchers with Config ...@@ -45,6 +46,13 @@ class ConfigTest extends TestNGSuite with MockitoSugar with Matchers with Config
new Config(map1).toString() shouldBe map1.toString() new Config(map1).toString() shouldBe map1.toString()
} }
@Test def testSkipNested: Unit = {
val map = Map("1" -> Map("2" -> Map("4" -> Map("5" -> Map("k1" -> "v1")))))
Config.getValueFromMap(map, new ConfigValueIndex("5", List("1", "2", "4", "5"), "k1")).get.asString shouldBe "v1"
Config.getValueFromMap(map, new ConfigValueIndex("5", List("1", "2", "3", "4", "5"), "k1")).get.asString shouldBe "v1"
Config.getValueFromMap(map, new ConfigValueIndex("5", List("1", "2", "3", "dummy", "dummy", "4", "5"), "k1")).get.asString shouldBe "v1"
}
@Test def testGetValueFromMap: Unit = { @Test def testGetValueFromMap: Unit = {
val map = ConfigTest.map val map = ConfigTest.map
Config.getValueFromMap(map, new ConfigValueIndex("m1", Nil, "k1")).get.asString shouldBe "v2" Config.getValueFromMap(map, new ConfigValueIndex("m1", Nil, "k1")).get.asString shouldBe "v2"
......
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