如何从类型安全配置对象中删除一个部分?

时间:2018-01-06 00:55:09

标签: configuration typesafe

如何从typesafe配置中删除部分?

以下是一个简化的问题 -

import com.typesafe.config.{ConfigFactory, Config}

val configStr: String = """{
  section1 {
    s1Val1 = "a"
  }
  section2 {
    s2Val1 = "b"
  }
}"""

val overrideConfigStr : String = """{
  section2 {
    s2Val2 = "bPrime"
  }
}"""

val myConfig : Config = ConfigFactory.parseString(configStr)

def removeConfigSection(config: Config, sectionHead: String): Config = ???

val overrideConfig : Config = ConfigFactory.parseString(overrideConfigStr)

val newConfigWithOverride = removeConfigSection(myConfig, 
      "section2").withFallback(overrideConfig)

与" section2"的情况一样,我想要一个配置文件并覆盖一些自定义部分的许多部分。如何实现def removeConfigSection(config:Config,sectionHead:String)?

1 个答案:

答案 0 :(得分:1)

您可以使用withoutPath()

def removeConfigSection(config: Config, sectionHead: String): Config = config.withoutPath(sectionHead)

请务必注意正确引用path