Created
August 27, 2014 17:41
-
-
Save Mortimerp9/97fd022d59e9ae7f0c5b to your computer and use it in GitHub Desktop.
Smarter rich ConfigString to wrap Typesafe Config in Scala
This file contains 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
import RichConfigString._ | |
import com.typesafe.config.Config | |
import scala.concurrent.duration._ | |
implicit val config = Config.load() | |
val duration = "your.config.key" configOrElse(10 seconds) | |
val longValue = "your.config.key2" configOrElse(10L) | |
val intSeqValue = "your.config.key2" configOrElse(Seq(10,20,30))) |
This file contains 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
import com.typesafe.config.Config | |
import scala.util.Try | |
import scala.concurrent.duration.FiniteDuration | |
/** | |
* Some scala wrapper around the typesafe Config object to write configuration in the format: | |
* "host.defaults" configOrElse List("173.208.36.96:8800", "69.162.159.99:8800", "173.208.36.55:8800") | |
* | |
* The key is given as a string and `configOrElse` tries to get it from the configuration and guess the right type | |
* based on the default fallback type. | |
* | |
* User: pierre | |
* Date: 7/8/13 | |
* Time: 9:48 AM | |
*/ | |
object ConfigTools { | |
import java.util.{List => JavaList} | |
trait CanConfigTo[T] { | |
def get(key: String): T | |
} | |
trait CanConfigToList[T] extends CanConfigTo[List[T]] { | |
def get(key: String): List[T] = { | |
import collection.JavaConversions._ | |
getJavaList(key).toList | |
} | |
def getJavaList(key: String): JavaList[T] | |
} | |
object CanConfigTo { | |
import collection.JavaConversions._ | |
implicit def canConfigToString(implicit conf: Config) = new CanConfigTo[String] { | |
def get(key: String): String = conf.getString(key) | |
} | |
implicit def canConfigToStringList(implicit conf: Config) = new CanConfigToList[String] { | |
def getJavaList(key: String): JavaList[String] = conf.getStringList(key) | |
} | |
implicit def canConfigToInt(implicit conf: Config) = new CanConfigTo[Int] { | |
def get(key: String): Int = conf.getInt(key) | |
} | |
implicit def canConfigToIntList(implicit conf: Config) = new CanConfigToList[Int] { | |
def getJavaList(key: String): JavaList[Int] = conf.getIntList(key).map(_.toInt) | |
} | |
implicit def canConfigToDuration(implicit conf: Config) = new CanConfigTo[FiniteDuration] { | |
import scala.concurrent.duration._ | |
def get(key: String): FiniteDuration = conf.getNanoseconds(key).toLong.nanoseconds | |
} | |
implicit def canConfigToDurationList(implicit conf: Config) = new CanConfigToList[FiniteDuration] { | |
import scala.concurrent.duration._ | |
def getJavaList(key: String): JavaList[FiniteDuration] = conf.getNanosecondsList(key).map(_.toLong.nanoseconds) | |
} | |
implicit def canConfigToBoolean(implicit conf: Config) = new CanConfigTo[Boolean] { | |
def get(key: String): Boolean = conf.getBoolean(key) | |
} | |
implicit def canConfigToBooleanList(implicit conf: Config) = new CanConfigToList[Boolean] { | |
def getJavaList(key: String): JavaList[Boolean] = conf.getBooleanList(key).map(_.booleanValue) | |
} | |
implicit def canConfigToMap[T](implicit conf: Config) = new CanConfigTo[Map[String, T]] { | |
import scala.collection.JavaConverters._ | |
def get(key: String): Map[String, T] = conf.getObject(key).unwrapped.asScala.toMap.asInstanceOf[Map[String, T]] | |
} | |
} | |
class RichConfigString(key: String)(implicit conf: Config) { | |
def config[T](implicit can: CanConfigTo[T]): Option[T] = Try { | |
can.get(key) | |
} toOption | |
def configOrElse[T](el: => T)(implicit can: CanConfigTo[T]): T = Try { | |
can.get(key) | |
} getOrElse (el) | |
} | |
/** | |
* implicit conversion to get the RichConfigString and add config operations to the String | |
* @param key | |
* @param conf | |
* @return | |
*/ | |
implicit def toConfigString(key: String)(implicit conf: Config): RichConfigString = new RichConfigString(key) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment