Skip to content

Commit

Permalink
this is better.
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed May 2, 2024
1 parent d51f7f0 commit 293e4b5
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,18 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.stream.Stream;

public record Specs<T>(T t, ConfigType type, String fileName) {
public record Session(Type type, int layer) {
public enum Type {
SAVING, LOADING;
}

public static final Session SAVING = new Session(Type.SAVING, 0);

public static final Session LOADING = new Session(Type.LOADING, 0);

public Session nested() {
return new Session(type, layer + 1);
}
public record Specs<T>(T t, ConfigType type, List<String> nestedPaths) {
public enum Session {
SAVING, LOADING;
}

@Override
public String toString() {
return layer == 0
? type().name()
: String.format("%s -> Nested by %d", type().name(), layer);
}
public Specs(T t, ConfigType type, String fileName) {
this(t, type, Collections.singletonList(fileName));
}

public int correct(Config config, Session session) {
Expand Down Expand Up @@ -65,16 +54,17 @@ public int correct(Config config, Session session) {
Object value = field.get(t);

List<String> path = getPath(field);
List<String> deeperNestedPaths = Stream.concat(nestedPaths.stream(), path.stream()).toList();
Config nestedConfig = config.get(path);

Specs<Object> nestedSpecs = new Specs<>(value, type, fileName);
Specs<Object> nestedSpecs = new Specs<>(value, type, deeperNestedPaths);
if (nestedConfig != null) {
// Correct the nested config
nestedCorrections.addAndGet(nestedSpecs.correct(nestedConfig, session.nested()));
nestedCorrections.addAndGet(nestedSpecs.correct(nestedConfig, session));
} else {
// Correct and fallback to the default config (shouldn't happen)
Config fallbackConfig = type.wrap(value);
nestedCorrections.addAndGet(nestedSpecs.correct(fallbackConfig, session.nested()));
nestedCorrections.addAndGet(nestedSpecs.correct(fallbackConfig, session));
config.set(path, fallbackConfig);

NightAutoConfig.LOGGER.warn(
Expand Down Expand Up @@ -149,7 +139,7 @@ static boolean isNested(Field field) {
}

private String loggerPrefix(Session session) {
return String.format("[%s](%s)", fileName, session);
return String.format("(%s)[%s]", session, String.join(" -> ", nestedPaths));
}

private Field[] fields() {
Expand Down

0 comments on commit 293e4b5

Please sign in to comment.