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.
Updated README.md
  • Loading branch information
fulminazzo committed Jan 28, 2024
1 parent 7503d07 commit 6b399a7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 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.1'
version = '1.5.2'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -186,6 +189,12 @@ default <O> void set(@NotNull String path, @Nullable O o) {
object = object.toString();
if (tmp.equals(String.class)) return (T) object;
Method method = tmp.getMethod("valueOf", object.getClass());
if (tmp.equals(Integer.class)) {
String str = object.toString();
final Matcher matcher = Pattern.compile(".*(\\.0+)").matcher(str);
if (matcher.matches()) str = str.substring(0, (str.length()) - matcher.group(1).length());
object = str;
}
return (T) method.invoke(tmp, object);
} catch (Exception e) {
throwException(path, object, e);
Expand Down Expand Up @@ -979,7 +988,8 @@ default String getCurrentPath() {
return path.toString();
}

default void throwException(String path, Object object, Exception e) {
default void throwException(String path, Object object, Throwable e) {
if (e instanceof RuntimeException || e instanceof InvocationTargetException) e = e.getCause();
path = path == null ? "null" : path;
String currentPath = getCurrentPath();
if (currentPath != null && !currentPath.isEmpty()) path = currentPath + "." + path;
Expand Down Expand Up @@ -1068,7 +1078,17 @@ default void print() {
if (map == null) return treeMap;
map.forEach((k, v) -> {
if (v instanceof Map) treeMap.put(k.toString(), new ConfigurationSection(parent, k.toString(), (Map<?, ?>) v));
else treeMap.put(k.toString(), v);
else {
if (v instanceof List) {
List<Object> list = (List<Object>) v;
for (int i = 0; i < list.size(); i++) {
Object v2 = list.get(i);
if (v2 instanceof Map)
list.set(i, new ConfigurationSection(parent, String.valueOf(i), (Map<?, ?>) v2));
}
}
treeMap.put(k.toString(), v);
}
});
return treeMap;
}
Expand All @@ -1085,6 +1105,15 @@ default void print() {
Map<String, Object> map = config.toMap();
map.forEach((String k, @Nullable Object v) -> {
if (v instanceof IConfiguration) v = configToGeneralMap((IConfiguration) v);
else if (v instanceof List) {
List<Object> list = new LinkedList<>((Collection<?>) v);
for (int i = 0; i < list.size(); i++) {
Object v2 = list.get(i);
if (v2 instanceof ConfigurationSection)
list.set(i, configToGeneralMap((IConfiguration) v2));
}
v = list;
}
treeMap.put(k, v);
});
return treeMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.fulminazzo.yamlparser.parsers;

import it.fulminazzo.fulmicollection.interfaces.functions.TriConsumer;
import it.fulminazzo.yamlparser.configuration.IConfiguration;
import it.fulminazzo.fulmicollection.interfaces.functions.BiFunctionException;
import org.jetbrains.annotations.NotNull;
Expand All @@ -8,6 +9,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
* List YAML parser.
Expand All @@ -33,7 +35,21 @@ public ListYAMLParser() {
protected @NotNull BiFunctionException<@NotNull IConfiguration, @NotNull String, @Nullable List<T>> getLoader() {
return (c, s) -> {
Collection<T> loaded = super.getLoader().apply(c, s);
return new ArrayList<>(loaded);
return loaded == null ? null : new ArrayList<>(loaded);
};
}

@Override
protected @NotNull TriConsumer<@NotNull IConfiguration, @NotNull String, @Nullable List<T>> getDumper() {
return (c, s, o) -> {
if (o != null && !o.isEmpty()) {
Object firstNonNull = o.stream().filter(Objects::nonNull).findFirst().orElse(null);
if (firstNonNull instanceof IConfiguration) {
c.toMap().put(s, o);
return;
}
}
super.getDumper().accept(c, s, o);
};
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package it.fulminazzo.yamlparser.configuration;

import it.fulminazzo.yamlparser.logging.LogMessage;
import it.fulminazzo.yamlparser.configuration.exceptions.CannotBeNullException;
import it.fulminazzo.yamlparser.parsers.exceptions.EmptyArrayException;
import it.fulminazzo.yamlparser.logging.LogMessage;
import it.fulminazzo.yamlparser.parsers.*;
import it.fulminazzo.yamlparser.parsers.exceptions.EmptyArrayException;
import it.fulminazzo.yamlparser.utils.FileUtils;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -241,6 +241,29 @@ void testRemoveParsersFromPackage() {
FileConfiguration.removeParsers(UUIDYAMLParser.class.getPackage().getName());
assertEquals(new LinkedList<>(), FileConfiguration.getParsers());
}

@Test
void listShouldBeParsed() {
ConfigurationSection s1;
SimpleConfiguration c = new SimpleConfiguration();

final List<ConfigurationSection> expected = new LinkedList<>();
s1 = new ConfigurationSection(c, "0");
s1.set("id", "spigotmc-repo");
s1.set("url", "https://hub.spigotmc.org/nexus/content/repositories/snapshots/");
expected.add(s1);
s1 = new ConfigurationSection(c, "1");
s1.set("id", "sonatype");
s1.set("url", "https://oss.sonatype.org/content/groups/public/");
expected.add(s1);
s1 = new ConfigurationSection(c, "2");
s1.set("id", "paper-repo");
s1.set("url", "https://repo.papermc.io/repository/maven-public/");
expected.add(s1);

final FileConfiguration actual = new FileConfiguration(new File("build/resources/test/list.yml"));
assertEquals(expected, actual.getObjectList("repositories"));
}
}

class User {
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repositories:
- id: spigotmc-repo
url: https://hub.spigotmc.org/nexus/content/repositories/snapshots/
- id: sonatype
url: https://oss.sonatype.org/content/groups/public/
- id: paper-repo
url: https://repo.papermc.io/repository/maven-public/

0 comments on commit 6b399a7

Please sign in to comment.