Skip to content

Commit

Permalink
YAMLParser now supports YAML lists not parsed by the parser itself.
Browse files Browse the repository at this point in the history
This means that list of type
```yaml
list:
  - test: "Hello"
  - test: "Hi"
```
will be parsed as a list of (ConfigurationSection)[src/main/java/it/fulminazzo/yamlparser/configuration/ConfigurationSection].
Totally reworked classes separation to respect packaging conventions.
Reworked FileConfiguration#addParsers method.
Renamed `it.fulminazzo.yamlparser.configurations` package to `it.fulminazzo.yamlparser.configuration`.
Made FileConfiguration and ConfigurationSection final.
Added support for escaped dot characters: now it will be able to use `\.` in paths to allow for dotted strings to be parsed.
Updated README.md
  • Loading branch information
Fulminazzo committed Feb 1, 2024
1 parent 6b399a7 commit 3d12592
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'it.fulminazzo'
version = '1.5.2'
version = '1.5.3'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,9 +966,9 @@ default String getNameFromPath(@NotNull String path) {
*/
default @NotNull List<String> parsePath(@NotNull String path) {
List<String> list = new ArrayList<>();
for (String p : path.split("\\."))
if (p.trim().isEmpty()) return list;
else list.add(p);
Matcher matcher = Pattern.compile("((?:\\\\\\.|[^.])+)(?:\\.|$)").matcher(path);
while (matcher.find())
list.add(matcher.group(1));
return list;
}

Expand Down Expand Up @@ -1077,7 +1077,8 @@ default void print() {
LinkedHashMap<String, Object> treeMap = new LinkedHashMap<>();
if (map == null) return treeMap;
map.forEach((k, v) -> {
if (v instanceof Map) treeMap.put(k.toString(), new ConfigurationSection(parent, k.toString(), (Map<?, ?>) v));
final String key = k.toString().replace(".", "\\.");
if (v instanceof Map) treeMap.put(key, new ConfigurationSection(parent, key, (Map<?, ?>) v));
else {
if (v instanceof List) {
List<Object> list = (List<Object>) v;
Expand All @@ -1087,7 +1088,7 @@ default void print() {
list.set(i, new ConfigurationSection(parent, String.valueOf(i), (Map<?, ?>) v2));
}
}
treeMap.put(k.toString(), v);
treeMap.put(key, v);
}
});
return treeMap;
Expand All @@ -1103,7 +1104,8 @@ default void print() {
if (config == null) return null;
LinkedHashMap<String, Object> treeMap = new LinkedHashMap<>();
Map<String, Object> map = config.toMap();
map.forEach((String k, @Nullable Object v) -> {
map.forEach((k, v) -> {
k = k.replace("\\.", ".");
if (v instanceof IConfiguration) v = configToGeneralMap((IConfiguration) v);
else if (v instanceof List) {
List<Object> list = new LinkedList<>((Collection<?>) v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ void testRemoveParsersFromPackage() {
assertEquals(new LinkedList<>(), FileConfiguration.getParsers());
}

@Test
void testDottedValues() throws IOException {
final File parent = new File(filePath).getParentFile();
final File file = new File(parent, "test-dot.yml");
final String fileContents = FileUtils.readFileToString(file) + "\n";
FileConfiguration config = new FileConfiguration(file);
assertEquals(1, config.getInteger("dotted\\.value"));
config.save();
assertEquals(fileContents, FileUtils.readFileToString(file));
}

@Test
void listShouldBeParsed() {
ConfigurationSection s1;
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/test-dot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotted.value: 1

0 comments on commit 3d12592

Please sign in to comment.