Skip to content

Commit

Permalink
Support dynamic keys in a map's key (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejba committed Sep 3, 2023
1 parent 35e2442 commit 3741d85
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,15 @@ private static void flattenYaml(String path, Map<Object, Object> source, Map<Str
key = originalKey.toString();
}

if (key.contains(".")) {
key = "\"" + key + "\"";
}

if (!key.isEmpty() && path != null && !path.isEmpty()) {
key = indexed ? path + key : path + "." + key;
} else if (path != null && !path.isEmpty()) {
key = path;
}

if (value instanceof String) {
target.put(key, (String) value);
} else if (value instanceof Map) {
flattenYaml(key, (Map<Object, Object>) value, target, false);
} else if (value instanceof List) {
final List<Object> list = (List<Object>) value;
flattenList(key, list, target);
for (int i = 0; i < list.size(); i++) {
flattenYaml(key, Collections.singletonMap("[" + i + "]", list.get(i)), target, true);
}
if (!key.startsWith("%")) {
processEntry(key, value, path, target, indexed);
} else {
if (value != null) {
target.put(key, value.toString());
for (String e : key.split(",")) {
if (!e.contains("%")) {
processEntry("%" + e, value, path, target, indexed);
} else {
processEntry(e, value, path, target, indexed);
}
}
}
});
Expand Down Expand Up @@ -190,6 +176,35 @@ private static void flattenList(String key, List<Object> source, Map<String, Str
}
}

private static void processEntry(String key, Object value, String path,
Map<String, String> target, boolean indexed) {
if (key.contains(".")) {
key = "\"" + key + "\"";
}

if (!key.isEmpty() && path != null && !path.isEmpty()) {
key = indexed ? path + key : path + "." + key;
} else if (path != null && !path.isEmpty()) {
key = path;
}

if (value instanceof String) {
target.put(key, (String) value);
} else if (value instanceof Map) {
flattenYaml(key, (Map<Object, Object>) value, target, false);
} else if (value instanceof List) {
final List<Object> list = (List<Object>) value;
flattenList(key, list, target);
for (int i = 0; i < list.size(); i++) {
flattenYaml(key, Collections.singletonMap("[" + i + "]", list.get(i)), target, true);
}
} else {
if (value != null) {
target.put(key, value.toString());
}
}
}

private static void escapeCommas(StringBuilder b, String src, int escapeLevel) {
int cp;
for (int i = 0; i < src.length(); i += Character.charCount(cp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ void list() {
assertEquals("TLSv1.3", list.get(1));
}

@Test
void listOfKeys() {
String yaml = "\"%dev,test\":\n" +
" http:\n" +
" ssl:\n" +
" protocols:\n" +
" - TLSv1.2\n" +
" - TLSv1.3";

SmallRyeConfig config = new SmallRyeConfigBuilder().withSources(new YamlConfigSource("Yaml", yaml)).build();
String[] values = config.getValue("%dev.http.ssl.protocols", String[].class);
assertEquals(2, values.length);
assertEquals("TLSv1.2", values[0]);
assertEquals("TLSv1.3", values[1]);

List<String> list = config.getValues("%test.http.ssl.protocols", String.class,
ArrayList::new);
assertEquals(2, list.size());
assertEquals("TLSv1.2", list.get(0));
assertEquals("TLSv1.3", list.get(1));
}

@Test
void indentSpaces() {
String yaml;
Expand Down

0 comments on commit 3741d85

Please sign in to comment.