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

Added sample and library option to config

parent 852d8e5d
......@@ -29,15 +29,27 @@ trait Configurable extends ImplicitConversions {
val config = new ConfigFunctions
def path(sample: String = null, library: String = null, submodule: String = null) = {
(if (sample != null) "samples" :: sample :: Nil else Nil) :::
(if (library != null) "libraries" :: library :: Nil else Nil) :::
(if (submodule != null) configName :: configPath else configPath)
}
protected class ConfigFunctions {
def apply(key: String, default: Any = null, submodule: String = null, required: Boolean = false, freeVar: Boolean = true): ConfigValue = {
def apply(key: String,
default: Any = null,
submodule: String = null,
required: Boolean = false,
freeVar: Boolean = true,
sample: String = null,
library: String = null): ConfigValue = {
val m = if (submodule != null) submodule else configName
val p = (if (submodule != null) configName :: configPath else configPath)
val p = path(sample, library, submodule)
val d = {
val value = Config.getValueFromMap(defaults.toMap, ConfigValueIndex(m, p, key, freeVar))
if (value.isDefined) value.get.value else default
}
if (!contains(key, submodule, freeVar) && d == null) {
if (!contains(key, submodule, freeVar, sample = sample, library = library) && d == null) {
if (required) {
Logging.logger.error("Value in config could not be found but it is required, key: " + key + " module: " + m + " path: " + p)
throw new IllegalStateException("Value in config could not be found but it is required, key: " + key + " module: " + m + " path: " + p)
......@@ -47,9 +59,13 @@ trait Configurable extends ImplicitConversions {
else return Config.global(m, p, key, d, freeVar)
}
def contains(key: String, submodule: String = null, freeVar: Boolean = true) = {
def contains(key: String,
submodule: String = null,
freeVar: Boolean = true,
sample: String = null,
library: String = null) = {
val m = if (submodule != null) submodule else configName
val p = (if (submodule != null) configName :: configPath else configPath)
val p = path(sample, library, submodule)
Config.global.contains(m, p, key, freeVar) || !(Config.getValueFromMap(defaults.toMap, ConfigValueIndex(m, p, key, freeVar)) == None)
}
......
......@@ -36,12 +36,22 @@ class ConfigurableTest extends TestNGSuite with MockitoSugar with Matchers {
classC.get("k1", freeVar = false).asString shouldBe "c1"
classC.classB.get("k1", freeVar = false).asString shouldBe "b1"
classC.classB.classA.get("k1", freeVar = false).asString shouldBe "a1"
classC.get("bla", sample = "sample1", library = "library1").asString shouldBe "bla"
classC.get("test", sample = "sample1", library = "library1").asString shouldBe "test"
classC.get("test", sample = "sample1").asString shouldBe "test"
}
}
abstract class Cfg extends Configurable {
def get(key: String, default: String = null, submodule: String = null, required: Boolean = false, freeVar: Boolean = true) = {
config(key, default, submodule, required, freeVar = freeVar)
def get(key: String,
default: String = null,
submodule: String = null,
required: Boolean = false,
freeVar: Boolean = true,
sample: String = null,
library: String = null) = {
config(key, default, submodule, required, freeVar = freeVar, sample = sample, library = library)
}
}
......@@ -64,6 +74,15 @@ object ConfigurableTest {
"k1" -> "b1"
), "classc" -> Map(
"k1" -> "c1"
), "samples" -> Map(
"sample1" -> Map(
"test" -> "test",
"libraries" -> Map(
"library1" -> Map(
"bla" -> "bla"
)
)
)
)
)
......
Supports Markdown
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