Last active
May 2, 2019 21:28
-
-
Save erikerlandson/e8df8dee52f45e68b4f18b37bbd10ae2 to your computer and use it in GitHub Desktop.
Integrate typesafe/lightbend Config with coulomb QuantityParser
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
object ConfigIntegration { | |
import scala.language.implicitConversions | |
import scala.util.Try | |
import scala.reflect.runtime.universe.TypeTag | |
import com.typesafe.config.ConfigFactory | |
import com.typesafe.config.Config | |
import coulomb.parser.unitops.UnitTypeString | |
import coulomb.parser.QuantityParser | |
case class ConfigWithQP(conf: Config, qp: QuantityParser) { | |
def getUnitQuantity[N :TypeTag, U :UnitTypeString](key: String) = { | |
for { | |
raw <- Try { conf.getString(key) } | |
qv <- qp[N, U](raw) | |
} yield { qv } | |
} | |
} | |
object ConfigWithQP { | |
implicit def exposeConfig(cwqp: ConfigWithQP): Config = cwqp.conf | |
} | |
} |
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
scala> conf.entrySet().asScala.map { e => s"${e.getKey()} = ${e.getValue()}" }.mkString("\n") | |
res6: String = | |
bandwidth = Quoted("10 megabyte / second") | |
memory = Quoted("100 gigabyte") | |
duration = Quoted("60 second") | |
scala> val qp = QuantityParser[Byte :: Second :: Giga :: Mega :: HNil] | |
qp: coulomb.parser.QuantityParser = coulomb.parser.QuantityParser@552b9b22 | |
scala> val confWithUnits = ConfigWithQP(conf, qp) | |
confWithUnits: coulomb.parser.ConfigIntegration.ConfigWithQP = ConfigWithQP(Config(SimpleConfigObject({"bandwidth":"10 megabyte / second","duration":"60 second","memory":"100 gigabyte"})),coulomb.parser.QuantityParser@552b9b22) | |
scala> confWithUnits.getUnitQuantity[Double, Tera %* Bit]("memory").get.show | |
res7: String = 0.8 Tb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment