Skip to content

Commit

Permalink
Updated FulmiCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
fulminazzo committed Apr 17, 2024
1 parent 1650976 commit cea544f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {
compileOnly 'org.jetbrains:annotations:24.1.0'

implementation 'org.yaml:snakeyaml:2.2'
api 'it.fulminazzo:FulmiCollection:1.4.2'
api 'it.fulminazzo:FulmiCollection:1.6'

testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.fulminazzo.fulmicollection.interfaces.functions.BiFunctionException;
import it.fulminazzo.fulmicollection.interfaces.functions.FunctionException;
import it.fulminazzo.fulmicollection.interfaces.functions.TriConsumer;
import it.fulminazzo.fulmicollection.structures.tuples.Singlet;
import it.fulminazzo.fulmicollection.utils.ReflectionUtils;
import it.fulminazzo.yamlparser.configuration.ConfigurationSection;
import it.fulminazzo.yamlparser.configuration.IConfiguration;
Expand All @@ -12,6 +13,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.stream.Stream;

/**
* Callable YAML parser.
Expand All @@ -37,15 +39,19 @@ protected BiFunctionException<IConfiguration, String, T> getLoader() {
return (c, s) -> {
ConfigurationSection section = c.getConfigurationSection(s);
if (section == null) return null;
T t = function.apply(section);
final T t = function.apply(section);
if (t == null) return null;
for (Field field : ReflectionUtils.getFields(t)) {
if (Modifier.isStatic(field.getModifiers())) continue;
if (field.isAnnotationPresent(PreventSaving.class)) continue;
Object object = section.get(FileUtils.formatStringToYaml(field.getName()), field.getType());
if (object == null) continue;
field.set(t, object);
}
objectFields(t).forEach(f -> {
if (Modifier.isStatic(f.getModifiers())) return;
if (f.isAnnotationPresent(PreventSaving.class)) return;
Object object = section.get(FileUtils.formatStringToYaml(f.getName()), f.getType());
if (object == null) return;
try {
f.set(t, object);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
return t;
};
}
Expand All @@ -56,16 +62,23 @@ protected TriConsumer<IConfiguration, String, T> getDumper() {
c.set(s, null);
if (t == null) return;
ConfigurationSection section = c.createSection(s);
ReflectionUtils.getFields(t).forEach(field -> {
if (Modifier.isStatic(field.getModifiers())) return;
if (field.isAnnotationPresent(PreventSaving.class)) return;
String fieldName = field.getName();
objectFields(t).forEach(f -> {
if (Modifier.isStatic(f.getModifiers())) return;
if (f.isAnnotationPresent(PreventSaving.class)) return;
String fieldName = f.getName();
try {
section.set(FileUtils.formatStringToYaml(fieldName), field.get(t));
section.set(FileUtils.formatStringToYaml(fieldName), f.get(t));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
};
}

private static <T> @NotNull Stream<Field> objectFields(final @NotNull T t) {
return ReflectionUtils.getFields(t).stream()
.map(ReflectionUtils::setAccessible)
.filter(f -> f.isPresent())
.map(Singlet::getValue);
}
}

0 comments on commit cea544f

Please sign in to comment.