mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat(json): Finished json version's provider
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package cc.carm.lib.configuration.demo.tests;
|
||||
|
||||
import cc.carm.lib.configuration.demo.tests.conf.DemoConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.conf.TestConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.model.TestModel;
|
||||
import cc.carm.lib.configuration.demo.tests.conf.RegistryConfig;
|
||||
import cc.carm.lib.configuration.demo.tests.model.UserRecord;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
@@ -16,7 +16,7 @@ public class ConfigurationTest {
|
||||
@TestOnly
|
||||
public static void testDemo(ConfigurationHolder<?> holder) {
|
||||
try {
|
||||
holder.initializer().initialize(holder, DemoConfiguration.class);
|
||||
holder.initialize(DemoConfiguration.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -30,27 +30,27 @@ public class ConfigurationTest {
|
||||
System.out.println("after: " + DemoConfiguration.TEST_NUMBER.get());
|
||||
|
||||
System.out.println("> Test Value:");
|
||||
System.out.println("before: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
DemoConfiguration.Sub.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||
System.out.println("after: " + DemoConfiguration.Sub.UUID_CONFIG_VALUE.get());
|
||||
System.out.println("before: " + DemoConfiguration.SUB.UUID_CONFIG_VALUE.get());
|
||||
DemoConfiguration.SUB.UUID_CONFIG_VALUE.set(UUID.randomUUID());
|
||||
System.out.println("after: " + DemoConfiguration.SUB.UUID_CONFIG_VALUE.get());
|
||||
|
||||
System.out.println("> Test List:");
|
||||
|
||||
System.out.println(" Before:");
|
||||
DemoConfiguration.Sub.That.OPERATORS.forEach(System.out::println);
|
||||
DemoConfiguration.SUB.That.OPERATORS.forEach(System.out::println);
|
||||
List<UUID> operators = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID()).collect(Collectors.toList());
|
||||
DemoConfiguration.Sub.That.OPERATORS.set(operators);
|
||||
DemoConfiguration.SUB.That.OPERATORS.set(operators);
|
||||
System.out.println(" After:");
|
||||
DemoConfiguration.Sub.That.OPERATORS.forEach(System.out::println);
|
||||
DemoConfiguration.SUB.That.OPERATORS.forEach(System.out::println);
|
||||
|
||||
System.out.println("> Clear List:");
|
||||
System.out.println(" Before: size :" + DemoConfiguration.Sub.That.OPERATORS.size());
|
||||
DemoConfiguration.Sub.That.OPERATORS.modify(List::clear);
|
||||
System.out.println(" After size :" + DemoConfiguration.Sub.That.OPERATORS.size());
|
||||
System.out.println(" Before: size :" + DemoConfiguration.SUB.That.OPERATORS.size());
|
||||
DemoConfiguration.SUB.That.OPERATORS.modify(List::clear);
|
||||
System.out.println(" After size :" + DemoConfiguration.SUB.That.OPERATORS.size());
|
||||
|
||||
System.out.println("> Test Section:");
|
||||
System.out.println(DemoConfiguration.MODEL_TEST.get());
|
||||
DemoConfiguration.MODEL_TEST.set(TestModel.random());
|
||||
System.out.println(DemoConfiguration.USERS.get());
|
||||
DemoConfiguration.USERS.add(UserRecord.random());
|
||||
|
||||
// System.out.println("> Test Maps:");
|
||||
// DemoConfiguration.USERS.forEach((k, v) -> System.out.println(k + ": " + v));
|
||||
@@ -64,19 +64,19 @@ public class ConfigurationTest {
|
||||
|
||||
public static void testInner(ConfigurationHolder<?> provider) {
|
||||
|
||||
TestConfiguration TEST = new TestConfiguration();
|
||||
RegistryConfig TEST = new RegistryConfig();
|
||||
|
||||
provider.initialize(TEST);
|
||||
|
||||
System.out.println("> Test Inner value before:");
|
||||
System.out.println(TEST.INNER.INNER_VALUE.getNotNull());
|
||||
System.out.println(TEST.INSTANCE.INNER_VALUE.getNotNull());
|
||||
|
||||
double after = Math.random() * 200D;
|
||||
System.out.println("> Test Inner value -> " + after);
|
||||
TEST.INNER.INNER_VALUE.set(after);
|
||||
TEST.INSTANCE.INNER_VALUE.set(after);
|
||||
|
||||
System.out.println("> Test Inner value after:");
|
||||
System.out.println(TEST.INNER.INNER_VALUE.getNotNull());
|
||||
System.out.println(TEST.INSTANCE.INNER_VALUE.getNotNull());
|
||||
|
||||
}
|
||||
|
||||
|
||||
+8
-11
@@ -4,7 +4,8 @@ import cc.carm.lib.configuration.Configuration;
|
||||
import cc.carm.lib.configuration.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.annotation.InlineComment;
|
||||
import cc.carm.lib.configuration.demo.tests.model.TestModel;
|
||||
import cc.carm.lib.configuration.demo.DatabaseConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.model.UserRecord;
|
||||
import cc.carm.lib.configuration.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
|
||||
@@ -27,18 +28,14 @@ public interface DemoConfiguration extends Configuration {
|
||||
|
||||
// 支持通过 Class<?> 变量标注子配置,一并注册。
|
||||
// 注意: 若对应类也有注解,则优先使用类的注解。
|
||||
@ConfigPath("other-class-config") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。
|
||||
@HeaderComment({"", "Something..."}) // 支持给子路径直接打注释
|
||||
@InlineComment("InlineComments for class path")
|
||||
Class<?> OTHER = OtherConfiguration.class;
|
||||
Class<?> DATABASE = DatabaseConfiguration.class;
|
||||
|
||||
@ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@ConfigPath("registered_users") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@HeaderComment({"Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@InlineComment("Section数据也支持InlineComment注释")
|
||||
ConfigValue<TestModel> MODEL_TEST = ConfiguredValue.builderOf(TestModel.class).fromSection()
|
||||
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||
.parse((holder, section) -> TestModel.deserialize(section))
|
||||
.serialize(TestModel::serialize).build();
|
||||
ConfiguredList<UserRecord> USERS = ConfiguredList.builderOf(UserRecord.class).fromSection()
|
||||
.parse(UserRecord::deserialize).serialize(UserRecord::serialize)
|
||||
.defaults(UserRecord.CARM).build();
|
||||
|
||||
// @HeaderComment({"[ID - UUID]对照表", "", "用于测试Map类型的解析与序列化保存"})
|
||||
// ConfiguredMap<Integer, UUID> USERS = ConfiguredMap.builderOf(Integer.class, UUID.class)
|
||||
@@ -52,7 +49,7 @@ public interface DemoConfiguration extends Configuration {
|
||||
* 支持内部类的直接注册。
|
||||
* 注意,需要启用 {@link cc.carm.lib.configuration.source.option.StandardOptions#LOAD_SUB_CLASSES}
|
||||
*/
|
||||
class Sub implements Configuration {
|
||||
class SUB implements Configuration {
|
||||
|
||||
@ConfigPath(value = "uuid-value", root = true)
|
||||
@InlineComment("This is an inline comment")
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import cc.carm.lib.configuration.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
|
||||
|
||||
@HeaderComment("Inner Test")
|
||||
public class TestInnerConfiguration implements Configuration {
|
||||
public class InstanceConfig implements Configuration {
|
||||
|
||||
public final ConfigValue<Double> INNER_VALUE = ConfiguredValue.of(1.0D);
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package cc.carm.lib.configuration.demo.tests.conf;
|
||||
|
||||
public class OtherConfiguration {
|
||||
}
|
||||
+7
-8
@@ -4,24 +4,23 @@ import cc.carm.lib.configuration.Configuration;
|
||||
import cc.carm.lib.configuration.annotation.ConfigPath;
|
||||
import cc.carm.lib.configuration.annotation.HeaderComment;
|
||||
import cc.carm.lib.configuration.annotation.InlineComment;
|
||||
import cc.carm.lib.configuration.demo.tests.model.TestModel;
|
||||
import cc.carm.lib.configuration.demo.tests.model.UserRecord;
|
||||
import cc.carm.lib.configuration.value.ConfigValue;
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredValue;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class TestConfiguration implements Configuration {
|
||||
public class RegistryConfig implements Configuration {
|
||||
|
||||
public final TestInnerConfiguration INNER = new TestInnerConfiguration();
|
||||
|
||||
public final ConfigValue<Double> CLASS_VALUE = ConfiguredValue.of(1.0D);
|
||||
@HeaderComment("Support for configurations as instances")
|
||||
public final InstanceConfig INSTANCE = new InstanceConfig();
|
||||
|
||||
@ConfigPath("test.user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@HeaderComment({"Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@InlineComment("Section数据也支持InlineComment注释")
|
||||
public final ConfigValue<TestModel> TEST_MODEL = ConfiguredValue.builderOf(TestModel.class).fromSection()
|
||||
.defaults(new TestModel("Carm", UUID.randomUUID()))
|
||||
.parse((holder, section) -> TestModel.deserialize(section))
|
||||
public final ConfigValue<UserRecord> TEST_MODEL = ConfiguredValue.builderOf(UserRecord.class).fromSection()
|
||||
.defaults(new UserRecord("Carm", UUID.randomUUID()))
|
||||
.parse((holder, section) -> UserRecord.deserialize(section))
|
||||
.serialize((holder, data) -> data.serialize()).build();
|
||||
|
||||
|
||||
+2
-2
@@ -2,11 +2,11 @@ package cc.carm.lib.configuration.demo.tests.model;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class AbstractModel {
|
||||
public abstract class AbstractRecord {
|
||||
|
||||
protected final @NotNull String name;
|
||||
|
||||
public AbstractModel(@NotNull String name) {
|
||||
public AbstractRecord(@NotNull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
+11
-9
@@ -7,20 +7,22 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TestModel extends AbstractModel {
|
||||
public class UserRecord extends AbstractRecord {
|
||||
|
||||
public UUID uuid;
|
||||
public static final UserRecord CARM = new UserRecord("Carm", UUID.fromString("f7b3b3b3-3b3b-3b3b-3b3b-3b3b3b3b3b3b"));
|
||||
|
||||
public TestModel(String name, UUID uuid) {
|
||||
protected UUID uuid;
|
||||
|
||||
public UserRecord(String name, UUID uuid) {
|
||||
super(name);
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
public void uuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
public UUID uuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@@ -33,16 +35,16 @@ public class TestModel extends AbstractModel {
|
||||
return map;
|
||||
}
|
||||
|
||||
public static TestModel deserialize(ConfigureSection section) {
|
||||
public static UserRecord deserialize(ConfigureSection 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 TestModel(name, UUID.fromString(uuidString));
|
||||
return new UserRecord(name, UUID.fromString(uuidString));
|
||||
}
|
||||
|
||||
public static TestModel random() {
|
||||
return new TestModel(UUID.randomUUID().toString().substring(0, 5), UUID.randomUUID());
|
||||
public static UserRecord random() {
|
||||
return new UserRecord(UUID.randomUUID().toString().substring(0, 5), UUID.randomUUID());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user