Skip to content
Permalink
Browse files

Fixing generic configuration handler to allow empty configs

  • Loading branch information
atooni committed Mar 17, 2020
1 parent 386dcdc commit 21bdc524ab9a065261804e35f4120c7b0ee930df
@@ -43,7 +43,7 @@ object ConfigLocator {
List(prefix + s)
} else {
try {
fullKeyset(prefix + s + ".", cfg.getConfig(s))
List(prefix + s) ++ fullKeyset(prefix + s + ".", cfg.getConfig(s))
} catch {
case NonFatal(_) => List(prefix + s)
}
@@ -10,6 +10,7 @@ import blended.util.RichTry._

import scala.beans.BeanProperty
import scala.collection.JavaConverters._
import scala.util.{Success, Try}

class ContainerContextImpl extends AbstractContainerContextImpl {

@@ -148,9 +149,18 @@ class ContainerContextImpl extends AbstractContainerContextImpl {
ConfigParseOptions.defaults().setAllowMissing(false)
).withFallback(sysProps).withFallback(envProps).resolve()

val nullKeys =
ConfigLocator.fullKeyset("", appCfg).filter(s => appCfg.getIsNull(s))
.map(s => (s -> null)).toMap.asJava
// we need to make sure that all keys are available in the resulting config,
// even if they point to null values or empty configs
val allKeys : List[String] = ConfigLocator.fullKeyset("", appCfg)

val nullKeys = allKeys.filter(s => appCfg.getIsNull(s)).map(s => (s -> null)).toMap.asJava

val emptyKeys = allKeys.filter { s =>
Try { appCfg.getConfig(s) } match {
case Success(c) => c.isEmpty()
case _ => false
}
}.map(s => s -> ConfigFactory.empty().root()).toMap.asJava

val evaluated = ConfigLocator.evaluatedConfig(appCfg, this).unwrap

@@ -160,6 +170,7 @@ class ContainerContextImpl extends AbstractContainerContextImpl {
.withFallback(sysProps)
.withFallback(envProps)
.withFallback(ConfigFactory.parseMap(nullKeys))
.withFallback(ConfigFactory.parseMap(emptyKeys))
.resolve()

log.debug(s"Resolved container config : $resolvedCfg")
@@ -9,7 +9,7 @@ import org.scalatest.Matchers
import blended.util.RichTry._
import com.typesafe.config.Config

import scala.collection.JavaConverters._
import scala.util.Try

class ContainerContextImplSpec extends LoggingFreeSpec
with Matchers {
@@ -29,9 +29,12 @@ class ContainerContextImplSpec extends LoggingFreeSpec

val cfg : Config = ctContext.getConfig("ssl-config.keyManager")
cfg.getConfig("prototype") should not be (empty)
println(cfg.root().render())
log.info(s"Container Context : [$ctContext]")

val clientCfg : Config = ctContext.getConfig("akka.http.host-connection-pool")
clientCfg should not be (empty)
assert( Try { clientCfg.getConfig("client")}.isSuccess )

ctContext.properties should have size(9)
ctContext.properties.get("foo") should be (Some("bar"))
ctContext.properties.get("bar") should be (Some("test"))
@@ -65,10 +65,10 @@ class Cmdline {
var _baseDir: String = _
def baseDir: Option[String] = Option(_baseDir)

// Must use the hardcoded exit code here, otherwise doc generation fails
@CmdOption(
names = Array("--expect-refresh"),
description = "Expected a refreshed certificate. If no certificate was refreshed the application will exit with exit code " +
ExitCode.StringCode_NoCertsRefreshed,
description = "Expected a refreshed certificate. If no certificate was refreshed the application will exit with exit code 5",

This comment has been minimized.

Copy link
@lefou

lefou Mar 17, 2020

Member

There must be some work-around. I already used a final string value, so that the annotation works in scalac. It's probably a bug in scaladoc?

This comment has been minimized.

Copy link
@lefou

lefou Mar 17, 2020

Member

Seems to be more or less a scaladoc bug/feature, probably solved in Scala 2.13. I just added a unit test for that.

requires = Array("--refresh-certs")
)
var expectRefresh: Boolean = false

0 comments on commit 21bdc52

Please sign in to comment.
You can’t perform that action at this time.