mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
feat(section): Implement more sections
This commit is contained in:
@@ -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.MemorySection;
|
||||
import cc.carm.lib.configuration.source.section.SourcedSection;
|
||||
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<MemorySection, Map<String, Object>, JSONSource> {
|
||||
public class JSONSource extends FileConfigSource<SourcedSection, Map<String, Object>, JSONSource> {
|
||||
|
||||
public static final @NotNull Gson DEFAULT_GSON = new GsonBuilder()
|
||||
.serializeNulls().disableHtmlEscaping().setPrettyPrinting()
|
||||
.registerTypeAdapter(
|
||||
MemorySection.class,
|
||||
(JsonSerializer<MemorySection>) (src, t, c) -> c.serialize(src.data())
|
||||
SourcedSection.class,
|
||||
(JsonSerializer<SourcedSection>) (src, t, c) -> c.serialize(src.data())
|
||||
).create();
|
||||
|
||||
protected final @NotNull Gson gson;
|
||||
protected @Nullable MemorySection rootSection;
|
||||
protected @Nullable SourcedSection rootSection;
|
||||
|
||||
protected JSONSource(@NotNull ConfigurationHolder<? extends JSONSource> holder,
|
||||
@NotNull File file, @Nullable String resourcePath) {
|
||||
@@ -58,7 +58,7 @@ public class JSONSource extends FileConfigSource<MemorySection, Map<String, Obje
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull MemorySection section() {
|
||||
public @NotNull SourcedSection section() {
|
||||
return Objects.requireNonNull(this.rootSection, "Root section is not initialized");
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public class JSONSource extends FileConfigSource<MemorySection, Map<String, Obje
|
||||
@Override
|
||||
protected void onReload() throws Exception {
|
||||
Map<?, ?> data = fileReader(reader -> gson.fromJson(reader, LinkedHashMap.class));
|
||||
this.rootSection = MemorySection.root(this, data);
|
||||
this.rootSection = SourcedSection.root(this, data);
|
||||
this.lastUpdateMillis = System.currentTimeMillis(); // 更新时间
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import cc.carm.lib.configuration.commentable.CommentableOptions;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.file.FileConfigSource;
|
||||
import cc.carm.lib.configuration.source.section.ConfigureSection;
|
||||
import cc.carm.lib.configuration.source.section.MemorySection;
|
||||
import cc.carm.lib.configuration.source.section.SourcedSection;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedSection;
|
||||
import cc.carm.lib.yamlcommentupdater.CommentedYAMLWriter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -26,14 +26,14 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
public class YAMLSource
|
||||
extends FileConfigSource<MemorySection, Map<String, Object>, YAMLSource>
|
||||
extends FileConfigSource<SourcedSection, Map<String, Object>, YAMLSource>
|
||||
implements CommentedSection {
|
||||
|
||||
protected final @NotNull YamlConstructor yamlConstructor;
|
||||
protected final @NotNull YamlRepresenter yamlRepresenter;
|
||||
protected final @NotNull Yaml yaml;
|
||||
|
||||
protected @Nullable MemorySection rootSection;
|
||||
protected @Nullable SourcedSection rootSection;
|
||||
|
||||
protected YAMLSource(@NotNull ConfigurationHolder<? extends YAMLSource> holder,
|
||||
@NotNull File file, @Nullable String resourcePath) {
|
||||
@@ -65,7 +65,7 @@ public class YAMLSource
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull MemorySection section() {
|
||||
public @NotNull SourcedSection section() {
|
||||
return Objects.requireNonNull(this.rootSection, "Root section is not initialized.");
|
||||
}
|
||||
|
||||
@@ -130,18 +130,18 @@ public class YAMLSource
|
||||
return this.saveToString(section());
|
||||
}
|
||||
|
||||
public @NotNull MemorySection loadFromString(@NotNull String data) throws Exception {
|
||||
public @NotNull SourcedSection loadFromString(@NotNull String data) throws Exception {
|
||||
MappingNode mappingNode;
|
||||
try (Reader reader = new UnicodeReader(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)))) {
|
||||
Node rawNode = this.yaml.compose(reader);
|
||||
mappingNode = (MappingNode) rawNode;
|
||||
}
|
||||
if (mappingNode == null) return MemorySection.root(this);
|
||||
if (mappingNode == null) return SourcedSection.root(this);
|
||||
|
||||
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
this.constructMap(mappingNode, map);
|
||||
return MemorySection.root(this, map);
|
||||
return SourcedSection.root(this, map);
|
||||
}
|
||||
|
||||
private void constructMap(@NotNull MappingNode mappingNode, @NotNull Map<String, Object> section) {
|
||||
@@ -170,7 +170,7 @@ public class YAMLSource
|
||||
public String serializeValue(@NotNull String key, @NotNull Object value) {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put(key, value);
|
||||
return saveToString(MemorySection.root(this, map));
|
||||
return saveToString(SourcedSection.root(this, map));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user