diff --git a/build.gradle b/build.gradle index cae542b..9ca3a0e 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = 'it.fulminazzo' -version = '1.5.1' +version = '1.5.2' repositories { mavenCentral() diff --git a/src/main/java/it/fulminazzo/yamlparser/configuration/IConfiguration.java b/src/main/java/it/fulminazzo/yamlparser/configuration/IConfiguration.java index df76d77..932565f 100644 --- a/src/main/java/it/fulminazzo/yamlparser/configuration/IConfiguration.java +++ b/src/main/java/it/fulminazzo/yamlparser/configuration/IConfiguration.java @@ -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; /** @@ -186,6 +189,12 @@ default 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); @@ -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; @@ -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 list = (List) 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; } @@ -1085,6 +1105,15 @@ default void print() { Map map = config.toMap(); map.forEach((String k, @Nullable Object v) -> { if (v instanceof IConfiguration) v = configToGeneralMap((IConfiguration) v); + else if (v instanceof List) { + List 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; diff --git a/src/main/java/it/fulminazzo/yamlparser/parsers/ListYAMLParser.java b/src/main/java/it/fulminazzo/yamlparser/parsers/ListYAMLParser.java index 4313248..04f5620 100644 --- a/src/main/java/it/fulminazzo/yamlparser/parsers/ListYAMLParser.java +++ b/src/main/java/it/fulminazzo/yamlparser/parsers/ListYAMLParser.java @@ -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; @@ -8,6 +9,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Objects; /** * List YAML parser. @@ -33,7 +35,21 @@ public ListYAMLParser() { protected @NotNull BiFunctionException<@NotNull IConfiguration, @NotNull String, @Nullable List> getLoader() { return (c, s) -> { Collection 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> 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); }; } } diff --git a/src/test/java/it/fulminazzo/yamlparser/configuration/FileConfigurationTest.java b/src/test/java/it/fulminazzo/yamlparser/configuration/FileConfigurationTest.java index c0f1a8a..d7cd252 100644 --- a/src/test/java/it/fulminazzo/yamlparser/configuration/FileConfigurationTest.java +++ b/src/test/java/it/fulminazzo/yamlparser/configuration/FileConfigurationTest.java @@ -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; @@ -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 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 { diff --git a/src/test/resources/list.yml b/src/test/resources/list.yml new file mode 100644 index 0000000..8c44c39 --- /dev/null +++ b/src/test/resources/list.yml @@ -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/ \ No newline at end of file