Skip to content

Commit

Permalink
Get ConfigHolders directly from Design (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewuathe authored and xerial committed Feb 1, 2019
1 parent 129868a commit da4d3ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
28 changes: 18 additions & 10 deletions airframe-config/src/main/scala/wvlet/airframe/config/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,24 @@ package object config {
}

def bootstrapWithConfigProcessor(configProcessor: Config => Unit): Design = {
configProcessor(getConfig)
configProcessor(currentConfig)
d
}

private def getConfig: Config = {
// Get config binded in the design.
def getConfig: Option[Config] = {
d.getDesignConfig match {
case dc: DesignOptionsWithConfig =>
dc.config
Some(dc.config)
case _ =>
Config()
None
}
}

def currentConfig: Config = {
getConfig match {
case Some(c) => c
case None => Config()
}
}

Expand All @@ -65,28 +73,28 @@ package object config {
}

def withConfigEnv(env: String, defaultEnv: String = "default"): Design = {
d.withConfig(getConfig.withEnv(env, defaultEnv))
d.withConfig(currentConfig.withEnv(env, defaultEnv))
}

def withConfigPaths(configPaths: Seq[String]): Design = {
d.withConfig(getConfig.withConfigPaths(configPaths))
d.withConfig(currentConfig.withConfigPaths(configPaths))
}

def bindConfig[A: ru.TypeTag](config: A): Design = {
val configHolder = getConfig.register[A](config)
val configHolder = currentConfig.register[A](config)
val s = Surface.of[A]
d.withConfig(configHolder)
.bind(s).toInstance(config)
}

def bindConfigFromYaml[A: ru.TypeTag](yamlFile: String): Design = {
val configHolder = getConfig.registerFromYaml[A](yamlFile)
val configHolder = currentConfig.registerFromYaml[A](yamlFile)
d.withConfig(configHolder)
.bind(Surface.of[A]).toInstance(configHolder.of[A])
}

def bindConfigFromYaml[A: ru.TypeTag: ClassTag](yamlFile: String, defaultValue: => A): Design = {
val configHolder = getConfig.registerFromYamlOrElse[A](yamlFile, defaultValue)
val configHolder = currentConfig.registerFromYamlOrElse[A](yamlFile, defaultValue)
val s = Surface.of[A]
val newConfig = configHolder.of[A]
d.withConfig(configHolder)
Expand All @@ -99,7 +107,7 @@ package object config {
def overrideConfigParams(props: Map[String, Any],
onUnusedProperties: Properties => Unit = REPORT_UNUSED_PROPERTIES): Design = {
val prevConfig = getConfig
val configHolder = getConfig.overrideWith(props, onUnusedProperties)
val configHolder = currentConfig.overrideWith(props, onUnusedProperties)
val d2 = d.withConfig(configHolder)

// Override already bounded config instances
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package wvlet.airframe.config

import wvlet.airframe.AirframeSpec
import wvlet.airframe.surface._

object AirframeBootstrapTest {
case class AppConfig(name: String)
Expand Down Expand Up @@ -67,5 +68,16 @@ class AirframeBootstrapTest extends AirframeSpec {
session.build[App2Config] shouldBe App2Config("scala")
}
}

"get config" in {
module3.noLifeCycleLogging.getConfig match {
case Some(c) =>
c.getAll.length shouldBe 1
c.getAll.head.tpe shouldBe Surface.of[App2Config]
c.getAll.head.value shouldBe App2Config("scala")
case None =>
fail()
}
}
}
}

0 comments on commit da4d3ba

Please sign in to comment.