mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 14:50:23 +08:00
Allow environment vars to be resolved to list
This commit is contained in:
parent
38dce6253c
commit
7473d8ca1b
@ -18,6 +18,9 @@ fork in Test := true
|
||||
fork in run := true
|
||||
fork in run in Test := true
|
||||
|
||||
//env vars for tests
|
||||
envVars in Test ++= Map("testList.0" -> "0", "testList.1" -> "1")
|
||||
|
||||
autoScalaLibrary := false
|
||||
crossPaths := false
|
||||
|
||||
|
@ -335,16 +335,7 @@ public class ConfigImpl {
|
||||
}
|
||||
|
||||
private static AbstractConfigObject loadEnvVariables() {
|
||||
Map<String, String> env = System.getenv();
|
||||
Map<String, AbstractConfigValue> m = new HashMap<String, AbstractConfigValue>();
|
||||
for (Map.Entry<String, String> entry : env.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
m.put(key,
|
||||
new ConfigString.Quoted(SimpleConfigOrigin.newSimple("env var " + key), entry
|
||||
.getValue()));
|
||||
}
|
||||
return new SimpleConfigObject(SimpleConfigOrigin.newSimple("env variables"),
|
||||
m, ResolveStatus.RESOLVED, false /* ignoresFallbacks */);
|
||||
return PropertiesParser.fromStringMap(newSimpleOrigin("env variables"), System.getenv());
|
||||
}
|
||||
|
||||
private static class EnvVariablesHolder {
|
||||
|
@ -56,15 +56,28 @@ final class PropertiesParser {
|
||||
|
||||
static AbstractConfigObject fromProperties(ConfigOrigin origin,
|
||||
Properties props) {
|
||||
return fromEntrySet(origin, props.entrySet());
|
||||
}
|
||||
|
||||
private static <K, V> AbstractConfigObject fromEntrySet(ConfigOrigin origin, Set<Map.Entry<K, V>> entries) {
|
||||
final Map<Path, Object> pathMap = getPathMap(entries);
|
||||
return fromPathMap(origin, pathMap, true /* from properties */);
|
||||
}
|
||||
|
||||
private static <K, V> Map<Path, Object> getPathMap(Set<Map.Entry<K, V>> entries) {
|
||||
Map<Path, Object> pathMap = new HashMap<Path, Object>();
|
||||
for (Map.Entry<Object, Object> entry : props.entrySet()) {
|
||||
for (Map.Entry<K, V> entry : entries) {
|
||||
Object key = entry.getKey();
|
||||
if (key instanceof String) {
|
||||
Path path = pathFromPropertyKey((String) key);
|
||||
pathMap.put(path, entry.getValue());
|
||||
}
|
||||
}
|
||||
return fromPathMap(origin, pathMap, true /* from properties */);
|
||||
return pathMap;
|
||||
}
|
||||
|
||||
static AbstractConfigObject fromStringMap(ConfigOrigin origin, Map<String, String> stringMap) {
|
||||
return fromEntrySet(origin, stringMap.entrySet());
|
||||
}
|
||||
|
||||
static AbstractConfigObject fromPathMap(ConfigOrigin origin,
|
||||
|
@ -10,6 +10,7 @@ import com.typesafe.config.ConfigException
|
||||
import com.typesafe.config.ConfigResolveOptions
|
||||
import com.typesafe.config.Config
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
class ConfigSubstitutionTest extends TestUtils {
|
||||
|
||||
@ -723,6 +724,35 @@ class ConfigSubstitutionTest extends TestUtils {
|
||||
checkNotSerializable(substComplexObject)
|
||||
}
|
||||
|
||||
@Test
|
||||
def resolveListFromSystemProps() {
|
||||
val props = parseObject(
|
||||
"""
|
||||
|"a": ${testList}
|
||||
""".stripMargin)
|
||||
|
||||
System.setProperty("testList.0", "0")
|
||||
System.setProperty("testList.1", "1")
|
||||
ConfigImpl.reloadSystemPropertiesConfig()
|
||||
|
||||
val resolved = resolve(ConfigFactory.systemProperties().withFallback(props).root.asInstanceOf[AbstractConfigObject])
|
||||
|
||||
assertEquals(List("0", "1"), resolved.getList("a").unwrapped().asScala)
|
||||
}
|
||||
|
||||
@Test
|
||||
def resolveListFromEnvVars() {
|
||||
val props = parseObject(
|
||||
"""
|
||||
|"a": ${testList}
|
||||
""".stripMargin)
|
||||
|
||||
//"testList.0" and "testList.1" are defined as envVars in build.sbt
|
||||
val resolved = resolve(props)
|
||||
|
||||
assertEquals(List("0", "1"), resolved.getList("a").unwrapped().asScala)
|
||||
}
|
||||
|
||||
// this is a weird test, it used to test fallback to system props which made more sense.
|
||||
// Now it just tests that if you override with system props, you can use system props
|
||||
// in substitutions.
|
||||
|
Loading…
Reference in New Issue
Block a user