Skip to content

Commit 3ae0c10

Browse files
Adding support for list access using env variables
1 parent 8693d3f commit 3ae0c10

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

inject/src/main/java/io/micronaut/context/env/EnvironmentPropertySource.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
* @since 1.0
2929
*/
3030
public class EnvironmentPropertySource extends MapPropertySource {
31+
/**
32+
* Converts list elemtents defined as _0 or _0_ into names that the resolver can understand; i.e., [0].
33+
*/
34+
private static final String LIST_CONVERTER_REGEX = "_([0-9]{1,2})(?:_|$)";
3135

3236
/**
3337
* The position of the loader.
@@ -77,20 +81,20 @@ static Map getEnv(@Nullable List<String> includes, @Nullable List<String> exclud
7781
}
7882

7983
static Map getEnv(Map<String, String> env, @Nullable List<String> includes, @Nullable List<String> excludes) {
80-
if (includes != null || excludes != null) {
81-
Map<String, String> result = new HashMap<>();
82-
for (Map.Entry<String, String> entry : env.entrySet()) {
83-
String envVar = entry.getKey();
84-
if (excludes != null && excludes.contains(envVar)) {
85-
continue;
86-
}
87-
if (includes != null && !includes.contains(envVar)) {
88-
continue;
89-
}
90-
result.put(envVar, entry.getValue());
84+
Map<String, String> result = new HashMap<>();
85+
for (Map.Entry<String, String> entry : env.entrySet()) {
86+
String envVar = entry.getKey();
87+
if (excludes != null && excludes.contains(envVar)) {
88+
continue;
89+
}
90+
if (includes != null && !includes.contains(envVar)) {
91+
continue;
9192
}
92-
return result;
93+
94+
String convertedEnvVar = envVar.replaceAll(LIST_CONVERTER_REGEX, "[$1]");
95+
96+
result.put(convertedEnvVar, entry.getValue());
9397
}
94-
return env;
98+
return result;
9599
}
96100
}

0 commit comments

Comments
 (0)