Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 12 additions & 12 deletions configlib-core/src/main/java/de/exlll/configlib/RootSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ private void preprocessEnvVarMap(String prefix, Map<Object, Object> map) {
final Object key = entry.getKey();
if (key == null) continue;
final Object val = entry.getValue();
String envVar = prefix.isEmpty()
? key.toString()
: prefix + '_' + key;
envVar = envVarConfig.caseSensitive()
? envVar
: envVar.toUpperCase(Locale.ROOT);
String envVar = createEnvironmentVariable(prefix, key.toString());
final String envVarVal = environment.getValue(envVar);
preprocessRecursively(val, envVar, envVarVal, entry::setValue);
}
Expand All @@ -96,18 +91,23 @@ private void preprocessEnvVarMap(String prefix, Map<Object, Object> map) {
private void preprocessEnvVarList(String prefix, List<Object> list) {
for (int i = 0; i < list.size(); i++) {
final Object val = list.get(i);
String envVar = prefix.isEmpty()
? String.valueOf(i)
: prefix + '_' + i;
envVar = envVarConfig.caseSensitive()
? envVar
: envVar.toUpperCase(Locale.ROOT);
String envVar = createEnvironmentVariable(prefix, String.valueOf(i));
final String envVarVal = environment.getValue(envVar);
final int index = i;
preprocessRecursively(val, envVar, envVarVal, o -> list.set(index, o));
}
}

private String createEnvironmentVariable(String prefix, String pathSegment) {
String envVar = prefix.isEmpty()
? pathSegment
: prefix + '_' + pathSegment;
envVar = envVar.replace('-', '_');
return envVarConfig.caseSensitive()
? envVar
: envVar.toUpperCase(Locale.ROOT);
}
Comment on lines +101 to +109

private void preprocessRecursively(
Object value,
String envVar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ record Config2(
List<List<String>> listListString
) {}

private record KebabConfig(String encryptionKey, KebabNestedConfig nestedConfig) {}

private record KebabNestedConfig(int portNumber) {}


@Test
void preprocessEnvVarsFailsIfEnvVarTriesToReplaceCollection() {
Expand Down Expand Up @@ -177,6 +181,31 @@ private static Config1 deserializeExampleConfig1(RootSerializer<Config1> seriali
)));
}

@Test
void preprocessLowerKebabEnvVarsWithSnakeCaseEnvironmentNames() {
final var envConfig = EnvVarResolutionConfiguration.resolveEnvVarsWithPrefix("MY_PREFIX", false);
final var props = ConfigurationProperties.newBuilder()
.setNameFormatter(NameFormatters.LOWER_KEBAB_CASE)
.setEnvVarResolutionConfiguration(envConfig)
.build();
final var serializer = new RootSerializer<>(
KebabConfig.class,
props,
new MapEnvironment(entriesAsMap(
entry("MY_PREFIX_ENCRYPTION_KEY", "env-key"),
entry("MY_PREFIX_NESTED_CONFIG_PORT_NUMBER", "5432")
))
);

KebabConfig config = serializer.deserialize(asMap(
"encryption-key", "yaml-key",
"nested-config", asMap("port-number", 3306L)
));

assertThat(config.encryptionKey, is("env-key"));
assertThat(config.nestedConfig.portNumber, is(5432));
}

@Test
void preprocessEnvVarsForTopLevelVariablesWithoutPrefixCaseInsensitive() {
RootSerializer<ExampleConfigurationA2> serializer = newSerializer(
Expand Down Expand Up @@ -419,4 +448,4 @@ private static <T> RootSerializer<T> newSerializer(
);
}
}
}
}
Loading