1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 10:38:19 +08:00

feat(source): Update sources' original type

This commit is contained in:
2025-02-16 02:04:14 +08:00
parent eff91b0496
commit 96ed604cd9
13 changed files with 160 additions and 39 deletions
@@ -2,7 +2,10 @@ package cc.carm.lib.configuration.source.section;
import cc.carm.lib.configuration.function.DataFunction; import cc.carm.lib.configuration.function.DataFunction;
import cc.carm.lib.configuration.source.option.StandardOptions; import cc.carm.lib.configuration.source.option.StandardOptions;
import org.jetbrains.annotations.*; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
@@ -37,11 +40,15 @@ public interface ConfigureSection {
return typeClass.isInstance(get(path)); return typeClass.isInstance(get(path));
} }
boolean isList(@NotNull String path); default boolean isList(@NotNull String path) {
return isType(path, List.class);
}
@Nullable List<?> getList(@NotNull String path); @Nullable List<?> getList(@NotNull String path);
boolean isSection(@NotNull String path); default boolean isSection(@NotNull String path) {
return isType(path, ConfigureSection.class);
}
@Nullable @Nullable
ConfigureSection getSection(@NotNull String path); ConfigureSection getSection(@NotNull String path);
@@ -96,22 +96,12 @@ public class MemorySection implements ConfigureSection {
return section == this ? data.get(path) : section.get(childPath(path)); return section == this ? data.get(path) : section.get(childPath(path));
} }
@Override
public boolean isList(@NotNull String path) {
return get(path) instanceof List<?>;
}
@Override @Override
public @Nullable List<?> getList(@NotNull String path) { public @Nullable List<?> getList(@NotNull String path) {
Object val = get(path); Object val = get(path);
return (val instanceof List<?>) ? (List<?>) val : null; return (val instanceof List<?>) ? (List<?>) val : null;
} }
@Override
public boolean isSection(@NotNull String path) {
return get(path) instanceof ConfigureSection;
}
@Override @Override
public @Nullable ConfigureSection getSection(@NotNull String path) { public @Nullable ConfigureSection getSection(@NotNull String path) {
Object val = get(path); Object val = get(path);
@@ -14,7 +14,7 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
public class JSONSource extends FileConfigSource<MemorySection, Map<?, ?>, JSONSource> { public class JSONSource extends FileConfigSource<MemorySection, Map<String, Object>, JSONSource> {
public static final @NotNull Gson DEFAULT_GSON = new GsonBuilder() public static final @NotNull Gson DEFAULT_GSON = new GsonBuilder()
.serializeNulls().disableHtmlEscaping().setPrettyPrinting() .serializeNulls().disableHtmlEscaping().setPrettyPrinting()
@@ -53,7 +53,7 @@ public class JSONSource extends FileConfigSource<MemorySection, Map<?, ?>, JSONS
} }
@Override @Override
public @NotNull Map<?, ?> original() { public @NotNull Map<String, Object> original() {
return section().data(); return section().data();
} }
+19 -1
View File
@@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId> <artifactId>easyconfiguration-parent</artifactId>
<version>3.9.1</version> <version>4.0.0</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>
@@ -25,6 +25,24 @@
<version>${project.parent.version}</version> <version>${project.parent.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-feature-commentable</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-feature-file</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyconfiguration-feature-section</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>${project.parent.groupId}</groupId> <groupId>${project.parent.groupId}</groupId>
@@ -0,0 +1,69 @@
package cc.carm.lib.configuration.source.hocon;
import cc.carm.lib.configuration.source.section.ConfigureSection;
import com.typesafe.config.Config;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import java.util.List;
import java.util.Map;
public class HOCONSection implements ConfigureSection {
protected final @NotNull HOCONSource source;
protected final @Nullable HOCONSection parent;
protected final @NotNull Config data;
public HOCONSection(@NotNull HOCONSource source, @Nullable HOCONSection parent,
@NotNull Config data) {
this.source = source;
this.parent = parent;
this.data = data;
}
public @NotNull Config data() {
return this.data;
}
@Override
public @NotNull HOCONSource source() {
return this.source;
}
@Override
public @Nullable HOCONSection parent() {
return this.parent;
}
@Override
public @NotNull @UnmodifiableView Map<String, Object> getValues(boolean deep) {
return data().root().unwrapped();
}
@Override
public void set(@NotNull String path, @Nullable Object value) {
}
@Override
public boolean contains(@NotNull String path) {
return data().hasPath(path);
}
@Override
public @Nullable List<?> getList(@NotNull String path) {
return data().getAnyRefList(path);
}
@Override
public @Nullable HOCONSection getSection(@NotNull String path) {
return data().getConfig(path) == null ? null : new HOCONSection(source, this, data().getConfig(path));
}
@Override
public @Nullable Object get(@NotNull String path) {
return data().getAnyRef(path);
}
}
@@ -0,0 +1,44 @@
package cc.carm.lib.configuration.source.hocon;
import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.source.section.ConfigureSource;
import com.typesafe.config.Config;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class HOCONSource extends ConfigureSource<HOCONSection, Config, HOCONSource> {
private HOCONSection rootSection;
protected HOCONSource(@NotNull ConfigurationHolder<? extends HOCONSource> holder, long lastUpdateMillis) {
super(holder, lastUpdateMillis);
}
@Override
protected HOCONSource self() {
return this;
}
@Override
public @NotNull Config original() {
return section().data();
}
@Override
public @NotNull HOCONSection section() {
return Objects.requireNonNull(rootSection, "RootSection is not initialized");
}
@Override
public void save() throws Exception {
}
@Override
protected void onReload() throws Exception {
}
}
@@ -1,25 +1,18 @@
package online.flowerinsnow.test.easyconfiguration; package online.flowerinsnow.test.easyconfiguration;
import cc.carm.lib.configuration.EasyConfiguration;
import cc.carm.lib.configuration.demo.tests.ConfigurationTest;
import cc.carm.lib.configuration.hocon.HOCONFileConfigProvider;
import org.junit.Test;
import java.io.File;
public class HOCONTest { public class HOCONTest {
@Test // @Test
public void onTest() { // public void onTest() {
HOCONFileConfigProvider provider = EasyConfiguration.from(new File("target/hocon.conf")); // HOCONFileConfigProvider provider = EasyConfiguration.from(new File("target/hocon.conf"));
//
ConfigurationTest.testDemo(provider); // ConfigurationTest.testDemo(provider);
ConfigurationTest.testInner(provider); // ConfigurationTest.testInner(provider);
//
try { // try {
provider.save(); // provider.save();
provider.reload(); // provider.reload();
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
} }
@@ -27,7 +27,7 @@ import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class YAMLSource extends FileConfigSource<MemorySection, Map<?, ?>, YAMLSource> implements CommentedSection { public class YAMLSource extends FileConfigSource<MemorySection, Map<String, Object>, YAMLSource> implements CommentedSection {
protected final @NotNull YamlConstructor yamlConstructor; protected final @NotNull YamlConstructor yamlConstructor;
protected final @NotNull YamlRepresenter yamlRepresenter; protected final @NotNull YamlRepresenter yamlRepresenter;
@@ -60,7 +60,7 @@ public class YAMLSource extends FileConfigSource<MemorySection, Map<?, ?>, YAMLS
} }
@Override @Override
public @NotNull Map<?, ?> original() { public @NotNull Map<String, Object> original() {
return section().data(); return section().data();
} }