Skip to content

Commit

Permalink
hard to correct inner ones...
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed May 2, 2024
1 parent d45dff1 commit d50cdb2
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package band.kessokuteatime.nightautoconfig.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marks a type to become a nestable configuration class inside a {@link me.shedaniel.autoconfig.ConfigData} type.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Nested {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package band.kessokuteatime.nightautoconfig.example.config;

import band.kessokuteatime.nightautoconfig.annotation.Nested;
import band.kessokuteatime.nightautoconfig.annotation.SpecInRangeDouble;
import band.kessokuteatime.nightautoconfig.converter.FloatToDoubleConverter;
import com.electronwill.nightconfig.core.conversion.Conversion;
import com.electronwill.nightconfig.core.conversion.Path;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry;
Expand All @@ -21,19 +23,17 @@ public class ExampleConfig implements ConfigData {

public String exampleString = "Hello, World!";

//@Path("stringWithCustomKey")
@Path("stringWithCustomKey")
public String exampleString2 = "Another String.";

@ConfigEntry.Category("category")
//@ConfigEntry.Category("category")
public String categorizedString = "Categorized!";

/*
@ConfigEntry.Gui.TransitiveObject
@ConfigEntry.Category("inner")
//@ConfigEntry.Category("inner")
public InnerConfig innerConfig = new InnerConfig();

*/

@Nested
public static class InnerConfig {
public int innerInt = 42;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package band.kessokuteatime.nightautoconfig.serializer;

import com.electronwill.nightconfig.core.conversion.ObjectConverter;
import com.electronwill.nightconfig.hocon.HoconFormat;
import com.electronwill.nightconfig.json.JsonFormat;
import com.electronwill.nightconfig.toml.TomlFormat;
import com.electronwill.nightconfig.yaml.YamlFormat;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.util.Utils;
Expand Down Expand Up @@ -29,4 +34,15 @@ public Path getConfigPath(Config definition) {
case YAML, TOML, HOCON -> new CommentedNightConfigSerializer<>(definition, configClass, this);
};
}

public com.electronwill.nightconfig.core.Config wrap(Object object) {
com.electronwill.nightconfig.core.Config config = switch (this) {
case JSON -> JsonFormat.newConfig();
case YAML -> YamlFormat.newConfig();
case TOML -> TomlFormat.newConfig();
case HOCON -> HoconFormat.newConfig();
};
new ObjectConverter().toObject(config, object);
return config;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package band.kessokuteatime.nightautoconfig.serializer;

import band.kessokuteatime.nightautoconfig.spec.SpecBuilder;
import band.kessokuteatime.nightautoconfig.spec.Specs;
import com.electronwill.nightconfig.core.ConfigSpec;
import com.electronwill.nightconfig.core.conversion.ObjectConverter;
import com.electronwill.nightconfig.core.file.FileConfig;
Expand All @@ -24,20 +24,23 @@ public abstract class NightConfigSerializer<
protected final ConfigType type;
protected final B builder;

protected final Specs specs;

public NightConfigSerializer(Config definition, Class<T> configClass, ConfigType type, B builder) {
this.definition = definition;
this.configClass = configClass;
this.type = type;
this.builder = builder;

this.specs = new Specs<>(createDefault(), type);
}

@Override
public void serialize(T t) throws SerializationException {
if (Files.exists(type.getConfigPath(definition))) {
FileConf config = new ObjectConverter().toConfig(t, builder::build);
ConfigSpec spec = new SpecBuilder<>(t).build();

spec.correct(config);
specs.correct(config, Specs.Session.SAVING);
config.save();
config.close();
} else {
Expand All @@ -54,10 +57,9 @@ public void serialize(T t) throws SerializationException {
public T deserialize() throws SerializationException {
if (Files.exists(type.getConfigPath(definition))) {
FileConf config = builder.build();
ConfigSpec spec = new SpecBuilder<>(createDefault()).build();

config.load();
spec.correct(config);
specs.correct(config, Specs.Session.LOADING);
return new ObjectConverter().toObject(config, this::createDefault);
} else {
T t = createDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
package band.kessokuteatime.nightautoconfig.spec;

import band.kessokuteatime.nightautoconfig.NightAutoConfig;
import band.kessokuteatime.nightautoconfig.annotation.*;
import band.kessokuteatime.nightautoconfig.serializer.ConfigType;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.ConfigSpec;
import com.electronwill.nightconfig.core.conversion.AdvancedPath;
import com.electronwill.nightconfig.core.conversion.Path;
import com.electronwill.nightconfig.core.utils.StringUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

public record SpecBuilder<T>(T t) {
public ConfigSpec build() {
public record Specs<T>(T t, ConfigType type) {
public enum Session {
SAVING("Saving"),
NESTED_SAVING("Saving/Nested"),
LOADING("Loading"),
NESTED_LOADING("Loading/Nested");

private final String name;

Session(String name) {
this.name = name;
}

public String semanticName() {
return name;
}
}

public void correct(Config config, Session session) {
ConfigSpec spec = new ConfigSpec();

appendBasicSpecs(spec);
appendInRangeSpecs(spec);

return spec;
ConfigSpec.CorrectionListener listener = (action, path, incorrectValue, correctedValue) -> {
String pathString = String.join(",", path);
NightAutoConfig.LOGGER.info(
"({}}) Corrected {}: was {}, is now {}",
session.semanticName(), pathString, incorrectValue, correctedValue
);
};

int count = correct(config, listener);
if (count > 0) {
NightAutoConfig.LOGGER.info(
"({}) Corrected {} {}",
session.semanticName(), count, (count == 1)? "item" : "items"
);
}
}

private int correct(Config config, ConfigSpec.CorrectionListener listener) {

}

/**
Expand Down Expand Up @@ -72,6 +108,13 @@ private void appendBasicSpecs(ConfigSpec spec) {
field.setAccessible(true);
Object value = field.get(t);

boolean isNested = field.getType().isAnnotationPresent(Nested.class);

if (isNested) {
System.out.println(new Specs<>(value, type).build());
return;
}

boolean isFloat = List.of(float.class, Float.class).contains(field.getType());

if (field.isAnnotationPresent(SpecElementValidator.class)) {
Expand Down

0 comments on commit d50cdb2

Please sign in to comment.