Skip to content

Commit

Permalink
Revert "Remove the generation of a comma separated value name for Col…
Browse files Browse the repository at this point in the history
…lections in the YamlConfigSource (smallrye#1203)"

This reverts commit 529c72c.
  • Loading branch information
radcortez committed Nov 29, 2024
1 parent 871478a commit 49a5b14
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package io.smallrye.config.source.yaml;

import static java.util.Collections.singletonMap;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
Expand All @@ -31,6 +36,14 @@ public class YamlConfigSource extends MapBackedConfigSource {

private static final String NAME_PREFIX = "YamlConfigSource[source=";
private static final int ORDINAL = ConfigSource.DEFAULT_ORDINAL + 10;
private static final Yaml DUMPER;

static {
final DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.FOLDED);
DUMPER = new Yaml(dumperOptions);
}

public YamlConfigSource(String name, Map<String, String> source, int ordinal) {
super(name, source, ordinal, false);
Expand Down Expand Up @@ -127,7 +140,8 @@ private static void flattenYaml(String path, Map<Object, Object> source, Map<Str
} else if (value instanceof Map) {
flattenYaml(key, (Map<Object, Object>) value, target, false);
} else if (value instanceof List) {
List<Object> list = (List<Object>) value;
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);
}
Expand All @@ -139,6 +153,40 @@ private static void flattenYaml(String path, Map<Object, Object> source, Map<Str
});
}

private static void flattenList(String key, List<Object> source, Map<String, String> target) {
boolean mixed = false;
List<String> flatten = new ArrayList<>();
for (Object value : source) {
if (value instanceof String || value instanceof Boolean) {
flatten.add(value.toString());
} else if (value != null) {
mixed = true;
break;
}
}

if (!mixed) {
target.put(key, flatten.stream().map(value -> {
StringBuilder sb = new StringBuilder();
escapeCommas(sb, value, 1);
return sb.toString();
}).collect(Collectors.joining(",")));
}
}

private static void escapeCommas(StringBuilder b, String src, int escapeLevel) {
int cp;
for (int i = 0; i < src.length(); i += Character.charCount(cp)) {
cp = src.codePointAt(i);
if (cp == '\\' || cp == ',') {
for (int j = 0; j < escapeLevel; j++) {
b.append('\\');
}
}
b.appendCodePoint(cp);
}
}

/**
* Override some yaml constructors, so that the value written in the flatten result is more alike with the
* source. For instance, timestamps may be written in a completely different format which prevents converters to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void nullValue() {
+ " - ~\n"))
.build();

assertEquals("something,1,true", config.getRawValue("foo"));
assertEquals("something", config.getRawValue("foo[0]"));
assertEquals("1", config.getRawValue("foo[1]"));
assertEquals("true", config.getRawValue("foo[2]"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ void listValue() {

ConfigSource src = new YamlConfigSource("Yaml", yaml);

assertEquals("cat", src.getValue("foo.bar[0]"));
assertEquals("dog", src.getValue("foo.bar[1]"));
assertEquals("chicken", src.getValue("foo.bar[2]"));
assertEquals("cat,dog,chicken", src.getValue("foo.bar"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ void propertyNames() {

assertTrue(propertyNames.contains("quarkus.http.port"));
assertTrue(propertyNames.contains("quarkus.http.ssl-port"));
assertTrue(propertyNames.contains("quarkus.http.ssl.protocols"));
assertTrue(propertyNames.contains("quarkus.http.ssl.protocols[0]"));
assertNotNull(config.getRawValue("quarkus.http.ssl.protocols[0]"));
}
Expand Down

0 comments on commit 49a5b14

Please sign in to comment.