-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flatten yaml properties with list and map context.
- Loading branch information
Showing
5 changed files
with
151 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
sources/yaml/src/test/java/io/smallrye/config/source/yaml/YamlConfigSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.smallrye.config.source.yaml; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.eclipse.microprofile.config.spi.Converter; | ||
import org.junit.jupiter.api.Test; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
public class YamlConfigSourceTest { | ||
@Test | ||
void flatten() throws Exception { | ||
YamlConfigSource yaml = new YamlConfigSource("yaml", | ||
YamlConfigSourceTest.class.getResourceAsStream("/example-216.yml")); | ||
String value = yaml.getValue("admin.users"); | ||
Users users = new UserConverter().convert(value); | ||
assertEquals(2, users.getUsers().size()); | ||
assertEquals(users.users.get(0).getEmail(), "[email protected]"); | ||
assertEquals(users.users.get(0).getRoles(), Stream.of("Moderator", "Admin").collect(toList())); | ||
|
||
assertEquals("[email protected]", yaml.getValue("admin.users.[0].email")); | ||
} | ||
|
||
public static class Users { | ||
List<User> users; | ||
|
||
public List<User> getUsers() { | ||
return users; | ||
} | ||
|
||
public void setUsers(final List<User> users) { | ||
this.users = users; | ||
} | ||
} | ||
|
||
public static class User { | ||
String email; | ||
String username; | ||
String password; | ||
List<String> roles; | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(final String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(final String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(final String password) { | ||
this.password = password; | ||
} | ||
|
||
public List<String> getRoles() { | ||
return roles; | ||
} | ||
|
||
public void setRoles(final List<String> roles) { | ||
this.roles = roles; | ||
} | ||
} | ||
|
||
static class UserConverter implements Converter<Users> { | ||
@Override | ||
public Users convert(final String value) { | ||
return new Yaml().loadAs(value, Users.class); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
admin: | ||
users: | ||
- | ||
email: "[email protected]" | ||
username: "joe" | ||
password: "123456" | ||
roles: | ||
- "Moderator" | ||
- "Admin" | ||
- | ||
email: "[email protected]" | ||
username: "jack" | ||
password: "654321" | ||
roles: | ||
- "Moderator" |