Skip to content

Commit

Permalink
Remove the generation of a comma separated value name for Collections…
Browse files Browse the repository at this point in the history
… in the YamlConfigSource (#1203)
  • Loading branch information
radcortez authored Jul 30, 2024
1 parent 7c456fe commit 529c72c
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
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 @@ -36,14 +31,6 @@ 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 @@ -140,8 +127,7 @@ 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) {
final List<Object> list = (List<Object>) value;
flattenList(key, list, target);
List<Object> list = (List<Object>) value;
for (int i = 0; i < list.size(); i++) {
flattenYaml(key, Collections.singletonMap("[" + i + "]", list.get(i)), target, true);
}
Expand All @@ -153,40 +139,6 @@ 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,7 +43,6 @@ 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,7 +41,9 @@ void listValue() {

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

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ 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 529c72c

Please sign in to comment.