Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
* @since 1.0
*/
public class EnvironmentPropertySource extends MapPropertySource {
/**
* Converts list elemtents defined as _0 or _0_ into names that the resolver can understand; i.e., [0].
*/
private static final String LIST_CONVERTER_REGEX = "_([0-9]{1,2})(?:_|$)";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre compile this regex

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


/**
* The position of the loader.
Expand Down Expand Up @@ -77,20 +81,20 @@ static Map getEnv(@Nullable List<String> includes, @Nullable List<String> exclud
}

static Map getEnv(Map<String, String> env, @Nullable List<String> includes, @Nullable List<String> excludes) {
if (includes != null || excludes != null) {
Map<String, String> result = new HashMap<>();
for (Map.Entry<String, String> entry : env.entrySet()) {
String envVar = entry.getKey();
if (excludes != null && excludes.contains(envVar)) {
continue;
}
if (includes != null && !includes.contains(envVar)) {
continue;
}
result.put(envVar, entry.getValue());
Map<String, String> result = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a LinkedHashMap to retain order

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existing code but updated as per comment.

for (Map.Entry<String, String> entry : env.entrySet()) {
String envVar = entry.getKey();
if (excludes != null && excludes.contains(envVar)) {
continue;
}
if (includes != null && !includes.contains(envVar)) {
continue;
}
return result;

String convertedEnvVar = envVar.replaceAll(LIST_CONVERTER_REGEX, "[$1]");

result.put(convertedEnvVar, entry.getValue());
}
return env;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,25 @@ class EnvironmentPropertySourceSpec extends Specification {
cleanup:
context.close()
}


void "test environment variable list conversion scenarios"() {
def envs = SystemLambda.withEnvironmentVariable("A_0", "valueA")
.and("A_0__B", "nestedValueB")
.and("A_0__C_0", "multipleArraysC")

ApplicationContext context = envs.execute(() -> ApplicationContext.builder().start())

expect:
context.getProperty("a[0]", String).isPresent()
context.getRequiredProperty("a[0]", String) == "valueA"

context.getProperty("a[0].b", String).isPresent()
context.getRequiredProperty("a[0].b", String) == "nestedValueB"

context.getProperty("a[0].c[0]", String).isPresent()
context.getRequiredProperty("a[0].c[0]", String) == "multipleArraysC"

cleanup:
context.close()
}
}