From 8e7ac263e7c18ef44faa7f33bf51bd7f4d67ae8c Mon Sep 17 00:00:00 2001 From: carm Date: Thu, 20 Feb 2025 03:01:45 +0800 Subject: [PATCH] feat: Split MapSection and MemorySection --- core/pom.xml | 2 +- demo/pom.xml | 2 +- features/commentable/pom.xml | 2 +- features/file/pom.xml | 2 +- features/section/pom.xml | 2 +- .../configuration/source/section/MapSection.java | 5 ++++- .../source/section/MemorySection.java | 14 +++++++------- features/text/pom.xml | 2 +- features/versioned/pom.xml | 2 +- pom.xml | 2 +- providers/gson/pom.xml | 2 +- .../lib/configuration/source/json/JSONSource.java | 14 +++++++------- providers/yaml/pom.xml | 2 +- 13 files changed, 28 insertions(+), 25 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 611f04b..b836d23 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.3 + 4.0.4 4.0.0 diff --git a/demo/pom.xml b/demo/pom.xml index d6dfc7f..3540607 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.3 + 4.0.4 4.0.0 diff --git a/features/commentable/pom.xml b/features/commentable/pom.xml index 102b7d1..b9d3026 100644 --- a/features/commentable/pom.xml +++ b/features/commentable/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.3 + 4.0.4 ../../pom.xml diff --git a/features/file/pom.xml b/features/file/pom.xml index 4186f36..b5d1d3c 100644 --- a/features/file/pom.xml +++ b/features/file/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.3 + 4.0.4 ../../pom.xml diff --git a/features/section/pom.xml b/features/section/pom.xml index dc878fc..7e7d672 100644 --- a/features/section/pom.xml +++ b/features/section/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.3 + 4.0.4 ../../pom.xml diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java index 8774d7d..5730c79 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/MapSection.java @@ -10,9 +10,12 @@ public abstract class MapSection> implements ConfigureSe protected final @NotNull Map data; protected final @Nullable R parent; - protected MapSection(@NotNull Map data, @Nullable R parent) { + protected MapSection(@Nullable R parent) { this.parent = parent; this.data = new LinkedHashMap<>(); + } + + public void migrate(Map data) { for (Map.Entry entry : data.entrySet()) { String key = (entry.getKey() == null) ? "null" : entry.getKey().toString(); if (entry.getValue() instanceof Map) { diff --git a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MemorySection.java b/features/section/src/main/java/cc/carm/lib/configuration/source/section/MemorySection.java index 55c33f7..d184057 100644 --- a/features/section/src/main/java/cc/carm/lib/configuration/source/section/MemorySection.java +++ b/features/section/src/main/java/cc/carm/lib/configuration/source/section/MemorySection.java @@ -8,22 +8,22 @@ import java.util.Map; public class MemorySection extends MapSection { - public static @NotNull MemorySection root(@NotNull ConfigureSource source) { return new MemorySection(source, new LinkedHashMap<>(), null); } public static @NotNull MemorySection root(@NotNull ConfigureSource source, - @Nullable Map data) { - return new MemorySection(source, data == null ? new LinkedHashMap<>() : data, null); + @Nullable Map raw) { + return new MemorySection(source, raw == null ? new LinkedHashMap<>() : raw, null); } protected final @NotNull ConfigureSource source; protected MemorySection(@NotNull ConfigureSource source, - @NotNull Map data, @Nullable MemorySection parent) { - super(data, parent); + @NotNull Map raw, @Nullable MemorySection parent) { + super(parent); this.source = source; + migrate(raw); } public @NotNull ConfigureSource source() { @@ -32,7 +32,7 @@ public class MemorySection extends MapSection { @Override public char pathSeparator() { - return source.pathSeparator(); + return source().pathSeparator(); } @Override @@ -42,7 +42,7 @@ public class MemorySection extends MapSection { @Override protected @NotNull MemorySection createChild(@NotNull Map data) { - return new MemorySection(source, data, this); + return new MemorySection(source(), data, this); } } diff --git a/features/text/pom.xml b/features/text/pom.xml index 9a310ea..b13b35a 100644 --- a/features/text/pom.xml +++ b/features/text/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.3 + 4.0.4 ../../pom.xml diff --git a/features/versioned/pom.xml b/features/versioned/pom.xml index 531c607..3e47eaa 100644 --- a/features/versioned/pom.xml +++ b/features/versioned/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyconfiguration-parent - 4.0.3 + 4.0.4 ../../pom.xml diff --git a/pom.xml b/pom.xml index 90888c5..b686c37 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ cc.carm.lib easyconfiguration-parent pom - 4.0.3 + 4.0.4 core features/section diff --git a/providers/gson/pom.xml b/providers/gson/pom.xml index 24f17e1..eff5e06 100644 --- a/providers/gson/pom.xml +++ b/providers/gson/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.3 + 4.0.4 ../../pom.xml 4.0.0 diff --git a/providers/gson/src/main/java/cc/carm/lib/configuration/source/json/JSONSource.java b/providers/gson/src/main/java/cc/carm/lib/configuration/source/json/JSONSource.java index 0a0448d..22e8f50 100644 --- a/providers/gson/src/main/java/cc/carm/lib/configuration/source/json/JSONSource.java +++ b/providers/gson/src/main/java/cc/carm/lib/configuration/source/json/JSONSource.java @@ -2,7 +2,7 @@ package cc.carm.lib.configuration.source.json; import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.source.file.FileConfigSource; -import cc.carm.lib.configuration.source.section.MapSection; +import cc.carm.lib.configuration.source.section.MemorySection; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSerializer; @@ -14,17 +14,17 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; -public class JSONSource extends FileConfigSource, JSONSource> { +public class JSONSource extends FileConfigSource, JSONSource> { public static final @NotNull Gson DEFAULT_GSON = new GsonBuilder() .serializeNulls().disableHtmlEscaping().setPrettyPrinting() .registerTypeAdapter( - MapSection.class, - (JsonSerializer) (src, t, c) -> c.serialize(src.data()) + MemorySection.class, + (JsonSerializer) (src, t, c) -> c.serialize(src.data()) ).create(); protected final @NotNull Gson gson; - protected @Nullable MapSection rootSection; + protected @Nullable MemorySection rootSection; protected JSONSource(@NotNull ConfigurationHolder holder, @NotNull File file, @Nullable String resourcePath) { @@ -58,7 +58,7 @@ public class JSONSource extends FileConfigSource } @Override - public @NotNull MapSection section() { + public @NotNull MemorySection section() { return Objects.requireNonNull(this.rootSection, "Root section is not initialized"); } @@ -70,7 +70,7 @@ public class JSONSource extends FileConfigSource @Override protected void onReload() throws Exception { Map data = fileReader(reader -> gson.fromJson(reader, LinkedHashMap.class)); - this.rootSection = MapSection.root(this, data); + this.rootSection = MemorySection.root(this, data); this.lastUpdateMillis = System.currentTimeMillis(); // 更新时间 } diff --git a/providers/yaml/pom.xml b/providers/yaml/pom.xml index a6dd4ad..2e0ac0d 100644 --- a/providers/yaml/pom.xml +++ b/providers/yaml/pom.xml @@ -6,7 +6,7 @@ easyconfiguration-parent cc.carm.lib - 4.0.3 + 4.0.4 ../../pom.xml