Skip to content

Commit

Permalink
Merge pull request #30172 from gsmet/codestart-flattening-fix
Browse files Browse the repository at this point in the history
Codestarts - Fix flattening of log levels
  • Loading branch information
gsmet authored Jan 5, 2023
2 parents 5837733 + affb2c5 commit 87b1bc7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,22 @@ private void writePropertiesConfig(Path targetPath, Map<String, Object> config)
static void flatten(String prefix, Map<String, String> target, Map<String, ?> map) {
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
flatten(prefix + entry.getKey() + ".", target, (Map) entry.getValue());
flatten(prefix + quote(entry.getKey().toString()) + ".", target, (Map) entry.getValue());
} else {
// TODO: handle different types of values
target.put(prefix + entry.getKey(), entry.getValue().toString());
target.put(prefix + quote(entry.getKey().toString()), entry.getValue().toString());
}
}
}

private static String quote(String key) {
if (!key.contains(".")) {
return key;
}

return "\"" + key.replaceAll("\"", "\\\"") + "\"";
}

private static String getConfigType(Map<String, Object> data) {
final Optional<String> config = CodestartData.getInputCodestartForType(data, CodestartType.CONFIG);
return config.orElseThrow(() -> new CodestartException("Config type is required"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,27 @@ void testFlatten() {
assertThat(flat).containsEntry("c.c-c.c-c-a", "1");
assertThat(flat).containsEntry("c.c-c.c-c-b", "2");
}

@Test
void testLogLevel() {
final HashMap<String, Object> level = new HashMap<>();
level.put("level", "DEBUG");

final HashMap<String, Object> categoryName = new HashMap<>();
categoryName.put("org.hibernate", level);

final HashMap<String, Object> category = new HashMap<>();
category.put("category", categoryName);

final HashMap<String, Object> log = new HashMap<>();
log.put("log", category);

final HashMap<String, Object> quarkus = new HashMap<>();
quarkus.put("quarkus", log);

final HashMap<String, String> flat = new HashMap<>();
SmartConfigMergeCodestartFileStrategyHandler.flatten("", flat, quarkus);

assertThat(flat).containsEntry("quarkus.log.category.\"org.hibernate\".level", "DEBUG");
}
}

0 comments on commit 87b1bc7

Please sign in to comment.