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

Added defaults for pipelines

parent 42abd53f
......@@ -31,7 +31,7 @@ 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 = jsonToMap(json.get)
val configJson = Config.jsonToMap(json.get)
logger.debug("Contain: " + configJson)
if (map.isEmpty) map = configJson
......@@ -39,37 +39,9 @@ class Config(var map: Map[String, Any]) extends Logging {
logger.debug("New config: " + 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)
}
def getMap(): Map[String, Any] = map
var notFoundCache: List[ConfigValueIndex] = List()
var foundCache: Map[ConfigValueIndex, ConfigValue] = Map()
var defaultCache: Map[ConfigValueIndex, ConfigValue] = Map()
protected[config] var notFoundCache: List[ConfigValueIndex] = List()
protected[config] var foundCache: Map[ConfigValueIndex, ConfigValue] = Map()
protected[config] var defaultCache: Map[ConfigValueIndex, ConfigValue] = Map()
def contains(s: String): Boolean = map.contains(s)
def contains(requestedIndex: ConfigValueIndex, freeVar:Boolean): Boolean = contains(requestedIndex.module, requestedIndex.path, requestedIndex.key, freeVar)
......@@ -79,46 +51,18 @@ class Config(var map: Map[String, Any]) extends Logging {
if (notFoundCache.contains(requestedIndex)) return false
else if (foundCache.contains(requestedIndex)) return true
else {
var submodules = path.reverse
while (!submodules.isEmpty) {
var submodules2 = submodules
while (!submodules2.isEmpty) {
val p = getMapFromPath(submodules2 ::: module :: Nil)
//logger.debug("p: " + p)
if (p.contains(key)) {
foundCache += (requestedIndex -> ConfigValue.apply(requestedIndex, ConfigValueIndex(module, submodules2, key), p(key)))
return true
}
if (freeVar) {
val p2 = getMapFromPath(submodules2)
//logger.debug("p2: " + p2)
if (p2.contains(key)) {
foundCache += (requestedIndex -> ConfigValue.apply(requestedIndex, ConfigValueIndex(module, submodules2, key), p2(key)))
return true
}
}
submodules2 = submodules2.init
}
submodules = submodules.tail
}
val p = getMapFromPath(module :: Nil)
if (p.contains(key)) { // Module is not nested
foundCache += (requestedIndex -> ConfigValue.apply(requestedIndex, ConfigValueIndex(module, Nil, key), p(key)))
return true
} else if (this.contains(key) && freeVar) { // Root value of json
foundCache += (requestedIndex -> ConfigValue.apply(requestedIndex, ConfigValueIndex("", Nil, key), get(key)))
val value = Config.getValueFromMap(map, requestedIndex)
if (value.isDefined) {
foundCache += (requestedIndex -> value.get)
return true
} else { // At this point key is not found on the path
} else {
notFoundCache +:= requestedIndex
return false
}
}
}
private def get(key: String): Any = map(key)
private def get(key: String, default: Any): Any = if (contains(key)) get(key) else default
def apply(module: String, path: List[String], key: String, default: Any = null, freeVar:Boolean = true): ConfigValue = {
protected[config] def apply(module: String, path: List[String], key: String, default: Any = null, freeVar:Boolean = true): ConfigValue = {
val requestedIndex = ConfigValueIndex(module, path, key)
if (contains(requestedIndex, freeVar)) return foundCache(requestedIndex)
else if (default != null) {
......@@ -130,15 +74,6 @@ class Config(var map: Map[String, Any]) extends Logging {
}
}
private def getMapFromPath(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))
}
return returnMap
}
def getReport: String = {
var output: StringBuilder = new StringBuilder
output.append("Config report, sorted on module:\n")
......@@ -197,5 +132,69 @@ object Config {
return newMap
}
def mergeConfigs(config1: Config, config2: Config): Config = new Config(mergeMaps(config1.getMap, config2.getMap))
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))
}
return returnMap
}
def getValueFromMap(map:Map[String,Any], index:ConfigValueIndex): Option[ConfigValue] = {
var submodules = index.path.reverse
while (!submodules.isEmpty) {
var submodules2 = submodules
while (!submodules2.isEmpty) {
val p = getMapFromPath(map, submodules2 ::: index.module :: Nil)
if (p.contains(index.key)) {
return Option(ConfigValue(index, ConfigValueIndex(index.module, submodules2, index.key), p(index.key)))
}
if (index.freeVar) {
val p2 = getMapFromPath(map, submodules2)
if (p2.contains(index.key)) {
return Option(ConfigValue(index, ConfigValueIndex(index.module, submodules2, index.key), p2(index.key)))
}
}
submodules2 = submodules2.init
}
submodules = submodules.tail
}
val p = getMapFromPath(map, index.module :: Nil)
if (p.contains(index.key)) { // Module is not nested
return Option(ConfigValue(index, ConfigValueIndex(index.module, Nil, index.key), p(index.key)))
} else if (map.contains(index.key) && index.freeVar) { // Root value of json
return Option(ConfigValue(index, ConfigValueIndex("", Nil, index.key), map(index.key)))
} else { // At this point key is not found on the path
return None
}
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ trait Configurable extends Logging {
val configPath: List[String] = if (root != null) root.configFullPath else List()
protected val configName = getClass.getSimpleName.toLowerCase
protected val configFullPath = configName :: configPath
var defaults: Map[String,Any] = if (root != null) root.defaults else Map()
val config = new ConfigFuntions
......@@ -16,14 +17,18 @@ trait Configurable extends Logging {
def apply(key: String, default: Any = null, submodule: String = null, required: Boolean = false, freeVar:Boolean = true): ConfigValue = {
val m = if (submodule != null) submodule else configName
val p = if (submodule != null) configName :: configPath else configPath
if (!contains(key, submodule, freeVar) && default == null) {
val d = {
val value = Config.getValueFromMap(defaults, ConfigValueIndex(m, p, key, freeVar))
if (value.isDefined) value.get else default
}
if (!contains(key, submodule, freeVar) && d == null) {
if (required) {
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)
} else return null
}
if (default == null) return globalConfig(m, p, key, freeVar)
else return globalConfig(m, p, key, default, freeVar)
if (d == null) return globalConfig(m, p, key, freeVar)
else return globalConfig(m, p, key, d, freeVar)
}
def contains(key: String, submodule: String = null, freeVar:Boolean = true) = {
......
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