From 293e4b58745090aa7bde6dffef5e4d5603463273 Mon Sep 17 00:00:00 2001 From: KrLite <68179735+KrLite@users.noreply.github.com> Date: Thu, 2 May 2024 17:55:31 +0800 Subject: [PATCH] this is better. --- .../nightautoconfig/spec/Specs.java | 36 +++++++------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java b/src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java index f768ded..01530d1 100644 --- a/src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java +++ b/src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java @@ -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, 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, ConfigType type, List 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) { @@ -65,16 +54,17 @@ public int correct(Config config, Session session) { Object value = field.get(t); List path = getPath(field); + List deeperNestedPaths = Stream.concat(nestedPaths.stream(), path.stream()).toList(); Config nestedConfig = config.get(path); - Specs nestedSpecs = new Specs<>(value, type, fileName); + Specs 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( @@ -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() {