mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-05 02:58:20 +08:00
[1.1.0] 实现 ConfigurationSerializable 相关数据的加载、获取与写入。
This commit is contained in:
@@ -2,9 +2,11 @@ package config;
|
||||
|
||||
import cc.carm.lib.configuration.EasyConfiguration;
|
||||
import cc.carm.lib.configuration.yaml.YamlConfigProvider;
|
||||
import config.misc.TestUser;
|
||||
import config.source.DemoConfiguration;
|
||||
import config.model.TestModel;
|
||||
import config.source.ComplexConfiguration;
|
||||
import config.source.DemoConfiguration;
|
||||
import config.source.ImplConfiguration;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -15,32 +17,16 @@ import java.util.stream.IntStream;
|
||||
|
||||
public class ConfigTester {
|
||||
|
||||
|
||||
@Test
|
||||
public void onTest() {
|
||||
ConfigurationSerialization.registerClass(TestModel.class);
|
||||
|
||||
YamlConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
||||
provider.initialize(DemoConfiguration.class);
|
||||
provider.initialize(ComplexConfiguration.class);
|
||||
|
||||
System.out.println("before: " + ComplexConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
ComplexConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||
System.out.println("after: " + ComplexConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
|
||||
|
||||
ComplexConfiguration.Sub.That.Operators.getNotNull().forEach(System.out::println);
|
||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||
ComplexConfiguration.Sub.That.Operators.set(operators);
|
||||
|
||||
System.out.println(ComplexConfiguration.USER.get());
|
||||
TestUser b = new TestUser(UUID.randomUUID().toString().substring(0, 3), UUID.randomUUID());
|
||||
ComplexConfiguration.USER.set(b);
|
||||
|
||||
ComplexConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
data.put((int) (1000 * Math.random()), UUID.randomUUID());
|
||||
}
|
||||
ComplexConfiguration.USERS.set(data);
|
||||
testDemo(provider);
|
||||
testComplex(provider);
|
||||
testSerialization(provider);
|
||||
|
||||
try {
|
||||
provider.save();
|
||||
@@ -50,5 +36,45 @@ public class ConfigTester {
|
||||
|
||||
}
|
||||
|
||||
public static void testSerialization(YamlConfigProvider provider) {
|
||||
System.out.println("----------------------------------------------------");
|
||||
provider.initialize(ImplConfiguration.class);
|
||||
System.out.println(ImplConfiguration.TEST.get());
|
||||
ImplConfiguration.TEST.set(TestModel.random());
|
||||
System.out.println("----------------------------------------------------");
|
||||
}
|
||||
|
||||
public static void testDemo(YamlConfigProvider provider) {
|
||||
provider.initialize(DemoConfiguration.class);
|
||||
}
|
||||
|
||||
public static void testComplex(YamlConfigProvider provider) {
|
||||
System.out.println("----------------------------------------------------");
|
||||
provider.initialize(ComplexConfiguration.class);
|
||||
|
||||
System.out.println("> Test Value:");
|
||||
System.out.println("before: " + ComplexConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
ComplexConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||
System.out.println("after: " + ComplexConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
|
||||
System.out.println("> Test List:");
|
||||
ComplexConfiguration.Sub.That.Operators.getNotNull().forEach(System.out::println);
|
||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||
ComplexConfiguration.Sub.That.Operators.set(operators);
|
||||
|
||||
System.out.println("> Test Section:");
|
||||
System.out.println(ComplexConfiguration.USER.get());
|
||||
ComplexConfiguration.USER.set(TestModel.random());
|
||||
|
||||
System.out.println("> Test Maps:");
|
||||
ComplexConfiguration.USERS.getNotNull().forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
LinkedHashMap<Integer, UUID> data = new LinkedHashMap<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
data.put((int) (1000 * Math.random()), UUID.randomUUID());
|
||||
}
|
||||
ComplexConfiguration.USERS.set(data);
|
||||
System.out.println("----------------------------------------------------");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+26
-5
@@ -1,18 +1,22 @@
|
||||
package config.misc;
|
||||
package config.model;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable;
|
||||
import org.bspfsystems.yamlconfiguration.serialization.SerializableAs;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TestUser {
|
||||
@SerializableAs("TestModel")
|
||||
public class TestModel implements ConfigurationSerializable {
|
||||
|
||||
public String name;
|
||||
public UUID uuid;
|
||||
|
||||
public TestUser(String name, UUID uuid) {
|
||||
public TestModel(String name, UUID uuid) {
|
||||
this.name = name;
|
||||
this.uuid = uuid;
|
||||
}
|
||||
@@ -33,6 +37,7 @@ public class TestUser {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", name);
|
||||
@@ -42,14 +47,30 @@ public class TestUser {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static TestUser deserialize(ConfigurationWrapper section) throws Exception {
|
||||
public static TestModel deserialize(ConfigurationWrapper section) {
|
||||
String name = section.getString("name");
|
||||
if (name == null) throw new NullPointerException("name is null");
|
||||
String uuidString = section.getString("info.uuid");
|
||||
if (uuidString == null) throw new NullPointerException("uuid is null");
|
||||
return new TestUser(name, UUID.fromString(uuidString));
|
||||
return new TestModel(name, UUID.fromString(uuidString));
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
@SuppressWarnings("unchecked")
|
||||
public static TestModel deserialize(Map<String, ?> args) {
|
||||
String name = (String) args.get("name");
|
||||
if (name == null) throw new NullPointerException("name is null");
|
||||
Map<String, ?> map = (Map<String, ?>) args.get("info");
|
||||
String uuidString = (String) map.get("uuid");
|
||||
if (uuidString == null) throw new NullPointerException("uuid is null");
|
||||
return new TestModel(name, UUID.fromString(uuidString));
|
||||
}
|
||||
|
||||
public static TestModel random() {
|
||||
return new TestModel(UUID.randomUUID().toString().substring(0, 5), UUID.randomUUID());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TestUser{" +
|
||||
@@ -8,7 +8,7 @@ import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
import config.misc.TestUser;
|
||||
import config.model.TestModel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -18,11 +18,11 @@ import java.util.UUID;
|
||||
public class ComplexConfiguration extends ConfigurationRoot {
|
||||
|
||||
@ConfigComment({"User测试"})
|
||||
public static final ConfigValue<TestUser> USER = ConfiguredSection
|
||||
.builder(TestUser.class)
|
||||
.defaults(new TestUser("Carm", UUID.randomUUID()))
|
||||
.parseValue((section, defaultValue) -> TestUser.deserialize(section))
|
||||
.serializeValue(TestUser::serialize).build();
|
||||
public static final ConfigValue<TestModel> USER = ConfiguredSection
|
||||
.builder(TestModel.class)
|
||||
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||
.parseValue((section, defaultValue) -> TestModel.deserialize(section))
|
||||
.serializeValue(TestModel::serialize).build();
|
||||
|
||||
@ConfigComment({"[ID-UUID] 对照表", "", "用于测试Map类型的解析与序列化保存"})
|
||||
public static final ConfigValue<Map<Integer, UUID>> USERS = ConfiguredMap
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package config.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigurationRoot;
|
||||
import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
||||
import config.model.TestModel;
|
||||
|
||||
@ConfigPath("ImplConfiguration")
|
||||
public class ImplConfiguration extends ConfigurationRoot {
|
||||
|
||||
public static final ConfigValue<TestModel> TEST = ConfiguredSerializable.of(TestModel.class);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user