Created
January 27, 2012 20:30
-
-
Save sheki/1690761 to your computer and use it in GitHub Desktop.
First Cut of a config tool in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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