Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add as config properties other object types. #451

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.smallrye.config.source.yaml;

import static java.util.Collections.singletonMap;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -29,6 +30,15 @@ public class YamlConfigSource extends MapBackedConfigSource {
private static final long serialVersionUID = -418186029484956531L;

private static final String NAME_PREFIX = "YamlConfigSource[source=";
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);
}

static final int ORDINAL = ConfigSource.DEFAULT_ORDINAL + 10;

private final Set<String> propertyNames;
Expand Down Expand Up @@ -127,12 +137,13 @@ private static void flattenYaml(String path, Map<Object, Object> source, Map<Str
if (value instanceof String) {
target.put(key, (String) value);
} else if (value instanceof Map) {
flattenMap(key, (Map<Object, Object>) value, target);
flattenYaml(key, (Map<Object, Object>) value, target);
} 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);
flattenYaml(key, singletonMap("[" + i + "]", list.get(i)), target);
}
} else {
target.put(key, (value != null ? value.toString() : ""));
Expand All @@ -148,14 +159,16 @@ private static void flattenList(String key, List<Object> source, Map<String, Str
return sb.toString();
}).collect(Collectors.joining(",")));
} else {
final DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.FOLDED);
target.put(key,
new Yaml(dumperOptions).dump(Collections.singletonMap(key.substring(key.lastIndexOf(".") + 1), source)));
target.put(key, DUMPER.dump(singletonMap(key.substring(key.lastIndexOf(".") + 1), source)));
}
}

private static void flattenMap(String key, Map<Object, Object> source, Map<String, String> target) {
String lastKey = key.substring(key.lastIndexOf(".") + 1);
target.put(key, lastKey.contains("[") && lastKey.contains("]") ? DUMPER.dump(source)
: DUMPER.dump(singletonMap(lastKey, source)));
}

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 @@ -144,6 +144,7 @@ void configMapping() throws Exception {
.withSources(new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example-216.yml")))
.withConverter(Users.class, 100, new UserConverter())
.withMapping(UsersMapping.class, "admin")
.withMappingIgnore("admin")
.build();

UsersMapping usersMapping = config.getConfigMapping(UsersMapping.class);
Expand All @@ -152,6 +153,21 @@ void configMapping() throws Exception {
assertEquals(usersMapping.users().users.get(0).getRoles(), Stream.of("Moderator", "Admin").collect(toList()));
}

@Test
void mapParentKey() {
YamlConfigSource parentLists = new YamlConfigSource("yaml", "users:\n" +
" - name: bob\n" +
" - name: joe\n" +
" age: 45");
assertNotNull(parentLists.getValue("users"));

YamlConfigSource parentObjects = new YamlConfigSource("yaml", "users:\n" +
" bob:\n" +
" joe:\n" +
" age: 45");
assertNotNull(parentObjects.getValue("users"));
}

public static class Users {
List<User> users;

Expand Down