Skip to content

Instantly share code, notes, and snippets.

@sheki
Created January 27, 2012 20:30
Show Gist options
  • Select an option

  • Save sheki/1690761 to your computer and use it in GitHub Desktop.

Select an option

Save sheki/1690761 to your computer and use it in GitHub Desktop.
First Cut of a config tool in Scala
class Config(private val envConf: Map[String, _],private val defaultConf: Map[String, _], environment: String) {
def findInteger(key : String ) : Option[Int] = find[Int](key)
def findString(key : String ) : Option[String] = find[String](key)
private def find[T](key : String ) : Option[T] = {
val keyList = key.split("\\.").toList
lookInMap[T](keyList,envConf) match {
case None => lookInMap(keyList,defaultConf)
case Some(x : T) => Some(x)
case _ => None
}
}
private def lookInMap[T](keys : List[String] , dict : Map[String, _ ] ) : Option[T] = {
if(keys.tail == Nil) {
dict.get(keys.head) match {
case Some(x : T) => Some(x)
case _ => None
}
}
else {
dict.get(keys.head) match {
case Some(child : java.util.Map[String, _]) => lookInMap[T](keys.tail,child.asScala)
case _ => None
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment