Initial project
This commit is contained in:
128
.gitignore
vendored
128
.gitignore
vendored
@ -1,98 +1,38 @@
|
|||||||
# ---> JetBrains
|
|
||||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
|
||||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
|
||||||
|
|
||||||
# User-specific stuff
|
|
||||||
.idea/**/workspace.xml
|
|
||||||
.idea/**/tasks.xml
|
|
||||||
.idea/**/usage.statistics.xml
|
|
||||||
.idea/**/dictionaries
|
|
||||||
.idea/**/shelf
|
|
||||||
|
|
||||||
# AWS User-specific
|
|
||||||
.idea/**/aws.xml
|
|
||||||
|
|
||||||
# Generated files
|
|
||||||
.idea/**/contentModel.xml
|
|
||||||
|
|
||||||
# Sensitive or high-churn files
|
|
||||||
.idea/**/dataSources/
|
|
||||||
.idea/**/dataSources.ids
|
|
||||||
.idea/**/dataSources.local.xml
|
|
||||||
.idea/**/sqlDataSources.xml
|
|
||||||
.idea/**/dynamic.xml
|
|
||||||
.idea/**/uiDesigner.xml
|
|
||||||
.idea/**/dbnavigator.xml
|
|
||||||
|
|
||||||
# Gradle
|
|
||||||
.idea/**/gradle.xml
|
|
||||||
.idea/**/libraries
|
|
||||||
|
|
||||||
# Gradle and Maven with auto-import
|
|
||||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
|
||||||
# since they will be recreated, and may cause churn. Uncomment if using
|
|
||||||
# auto-import.
|
|
||||||
# .idea/artifacts
|
|
||||||
# .idea/compiler.xml
|
|
||||||
# .idea/jarRepositories.xml
|
|
||||||
# .idea/modules.xml
|
|
||||||
# .idea/*.iml
|
|
||||||
# .idea/modules
|
|
||||||
# *.iml
|
|
||||||
# *.ipr
|
|
||||||
|
|
||||||
# CMake
|
|
||||||
cmake-build-*/
|
|
||||||
|
|
||||||
# Mongo Explorer plugin
|
|
||||||
.idea/**/mongoSettings.xml
|
|
||||||
|
|
||||||
# File-based project format
|
|
||||||
*.iws
|
|
||||||
|
|
||||||
# IntelliJ
|
|
||||||
out/
|
|
||||||
|
|
||||||
# mpeltonen/sbt-idea plugin
|
|
||||||
.idea_modules/
|
|
||||||
|
|
||||||
# JIRA plugin
|
|
||||||
atlassian-ide-plugin.xml
|
|
||||||
|
|
||||||
# Cursive Clojure plugin
|
|
||||||
.idea/replstate.xml
|
|
||||||
|
|
||||||
# SonarLint plugin
|
|
||||||
.idea/sonarlint/
|
|
||||||
|
|
||||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
|
||||||
com_crashlytics_export_strings.xml
|
|
||||||
crashlytics.properties
|
|
||||||
crashlytics-build.properties
|
|
||||||
fabric.properties
|
|
||||||
|
|
||||||
# Editor-based Rest Client
|
|
||||||
.idea/httpRequests
|
|
||||||
|
|
||||||
# Android studio 3.1+ serialized cache file
|
|
||||||
.idea/caches/build_file_checksums.ser
|
|
||||||
|
|
||||||
# ---> Maven
|
|
||||||
target/
|
target/
|
||||||
pom.xml.tag
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
pom.xml.releaseBackup
|
!**/src/main/**/target/
|
||||||
pom.xml.versionsBackup
|
!**/src/test/**/target/
|
||||||
pom.xml.next
|
|
||||||
release.properties
|
|
||||||
dependency-reduced-pom.xml
|
|
||||||
buildNumber.properties
|
|
||||||
.mvn/timing.properties
|
|
||||||
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
|
|
||||||
.mvn/wrapper/maven-wrapper.jar
|
|
||||||
|
|
||||||
# Eclipse m2e generated files
|
### IntelliJ IDEA ###
|
||||||
# Eclipse Core
|
.idea/modules.xml
|
||||||
.project
|
.idea/jarRepositories.xml
|
||||||
# JDT-specific (Eclipse Java Development Tools)
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
.classpath
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
124
.idea/uiDesigner.xml
generated
Normal file
124
.idea/uiDesigner.xml
generated
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
42
pom.xml
Normal file
42
pom.xml
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.imyeyu.config</groupId>
|
||||||
|
<artifactId>timi-config</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.test.skip>true</maven.test.skip>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.yaml</groupId>
|
||||||
|
<artifactId>snakeyaml</artifactId>
|
||||||
|
<version>2.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.imyeyu.io</groupId>
|
||||||
|
<artifactId>timi-io</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.34</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
36
src/main/java/com/imyeyu/config/BaseConverter.java
Normal file
36
src/main/java/com/imyeyu/config/BaseConverter.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.imyeyu.config;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-04-12 00:44
|
||||||
|
*/
|
||||||
|
public abstract class BaseConverter<T, K> {
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
K serialize0(Field field, Object t) {
|
||||||
|
return serialize(field, (T) t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
T deserialize0(Field field, Object data) {
|
||||||
|
return deserialize(field, (K) data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当组件值需要写入配置时的转换,默认直接 toString
|
||||||
|
*
|
||||||
|
* @param t 组件值类型
|
||||||
|
* @return 配置值
|
||||||
|
*/
|
||||||
|
protected abstract K serialize(Field field, T t);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当获取配置并即将设置到组件时调用
|
||||||
|
*
|
||||||
|
* @param data 配置值
|
||||||
|
* @return 返回值
|
||||||
|
*/
|
||||||
|
protected abstract T deserialize(Field field, K data);
|
||||||
|
}
|
||||||
237
src/main/java/com/imyeyu/config/ConfigLoader.java
Normal file
237
src/main/java/com/imyeyu/config/ConfigLoader.java
Normal file
@ -0,0 +1,237 @@
|
|||||||
|
package com.imyeyu.config;
|
||||||
|
|
||||||
|
import com.imyeyu.io.IO;
|
||||||
|
import com.imyeyu.java.TimiJava;
|
||||||
|
import com.imyeyu.java.bean.timi.TimiCode;
|
||||||
|
import com.imyeyu.java.bean.timi.TimiException;
|
||||||
|
import com.imyeyu.java.ref.Ref;
|
||||||
|
import com.imyeyu.java.ref.RefField;
|
||||||
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
|
import org.yaml.snakeyaml.LoaderOptions;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
import org.yaml.snakeyaml.constructor.Constructor;
|
||||||
|
import org.yaml.snakeyaml.representer.Representer;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-04-10 00:44
|
||||||
|
*/
|
||||||
|
public class ConfigLoader<T> {
|
||||||
|
|
||||||
|
private final File file;
|
||||||
|
private final Yaml yaml;
|
||||||
|
private final Class<T> clazz;
|
||||||
|
private final List<RawMapper> rawMappingList = new ArrayList<>();
|
||||||
|
private final Map<Class<?>, BaseConverter<?, ?>> converterMap = new HashMap<>();
|
||||||
|
|
||||||
|
private T value;
|
||||||
|
private LinkedHashMap<String, Object> rawValue;
|
||||||
|
|
||||||
|
public ConfigLoader(String srcPath, Class<T> clazz) {
|
||||||
|
this(srcPath, srcPath, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigLoader(String srcPath, String distPath, Class<T> clazz) {
|
||||||
|
this.clazz = clazz;
|
||||||
|
|
||||||
|
LoaderOptions loaderOptions = new LoaderOptions();
|
||||||
|
Constructor constructor = new Constructor(loaderOptions);
|
||||||
|
|
||||||
|
DumperOptions dumperOptions = new DumperOptions();
|
||||||
|
dumperOptions.setIndent(4);
|
||||||
|
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
|
dumperOptions.setIndicatorIndent(4);
|
||||||
|
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
|
||||||
|
dumperOptions.setIndentWithIndicator(true);
|
||||||
|
Representer representer = new ConfigRepresenter(dumperOptions);
|
||||||
|
|
||||||
|
yaml = new Yaml(constructor, representer, dumperOptions);
|
||||||
|
|
||||||
|
file = new File(distPath);
|
||||||
|
|
||||||
|
if (!file.exists()) {
|
||||||
|
try {
|
||||||
|
IO.resourceToDisk(getClass(), srcPath, distPath);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new TimiException(TimiCode.ERROR, "load default config error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public T load() {
|
||||||
|
try {
|
||||||
|
rawValue = yaml.load(IO.toString(file).replaceAll("\t", " "));
|
||||||
|
|
||||||
|
if (clazz == null || clazz.equals(Object.class)) {
|
||||||
|
value = (T) rawValue;
|
||||||
|
} else {
|
||||||
|
// 解析配置对象
|
||||||
|
rawMappingList.clear();
|
||||||
|
value = clazz.getDeclaredConstructor().newInstance();
|
||||||
|
deserializeToValue(value, rawValue, "");
|
||||||
|
}
|
||||||
|
return this.value;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new TimiException(TimiCode.ERROR, "load config error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dump() {
|
||||||
|
try {
|
||||||
|
serializeToRawValue();
|
||||||
|
IO.toFile(file, yaml.dump(rawValue).replaceAll("( ){4}", "\t"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new TimiException(TimiCode.ERROR, "dump config error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addConverter(Class<?> type, BaseConverter<?, ?> converter) {
|
||||||
|
converterMap.put(type, converter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeConverter(Class<?> type) {
|
||||||
|
converterMap.remove(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private void serializeToRawValue() throws Exception {
|
||||||
|
for (int i = 0; i < rawMappingList.size(); i++) {
|
||||||
|
RawMapper rawMapper = rawMappingList.get(i);
|
||||||
|
String[] bukkit = rawMapper.rawStack.split("#");
|
||||||
|
|
||||||
|
Map<String, Object> raw = rawValue;
|
||||||
|
String key;
|
||||||
|
if (bukkit.length == 1) {
|
||||||
|
key = bukkit[0];
|
||||||
|
} else {
|
||||||
|
for (int j = 0; j < bukkit.length - 1; j++) {
|
||||||
|
raw = (Map<String, Object>) rawValue.get(bukkit[j]);
|
||||||
|
}
|
||||||
|
key = bukkit[bukkit.length - 1];
|
||||||
|
}
|
||||||
|
Object value = rawMapper.refField.getGetter().invoke(rawMapper.owner);
|
||||||
|
|
||||||
|
// 字段注解序列化
|
||||||
|
Converter fieldConverter = rawMapper.refField.getType().getAnnotation(Converter.class);
|
||||||
|
if (fieldConverter != null) {
|
||||||
|
BaseConverter<?, ?> converter = Ref.newInstance(fieldConverter.value());
|
||||||
|
raw.put(key, converter.serialize0(rawMapper.refField.getField(), value));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 类型序列化
|
||||||
|
if (converterMap.containsKey(rawMapper.refField.getType())) {
|
||||||
|
BaseConverter<?, ?> converter = converterMap.get(rawMapper.refField.getType());
|
||||||
|
raw.put(key, converter.serialize0(rawMapper.refField.getField(), value));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 枚举序列化
|
||||||
|
if (Enum.class.isAssignableFrom(rawMapper.refField.getType())) {
|
||||||
|
raw.put(key, rawMapper.refField.getGetter().invoke(rawMapper.owner).toString());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 默认序列化
|
||||||
|
raw.put(key, rawMapper.refField.getGetter().invoke(rawMapper.owner));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private void deserializeToValue(Object object, Map<?, ?> map, String stack) throws Exception {
|
||||||
|
for (Map.Entry<?, ?> item : map.entrySet()) {
|
||||||
|
RefField refField = Ref.field(object.getClass(), item.getKey().toString());
|
||||||
|
|
||||||
|
if (!(item.getValue() instanceof Map<?, ?>)) {
|
||||||
|
// 缓存映射
|
||||||
|
RawMapper rawMapper = new RawMapper();
|
||||||
|
rawMapper.owner = object;
|
||||||
|
rawMapper.refField = refField;
|
||||||
|
rawMapper.rawStack = stack + item.getKey();
|
||||||
|
rawMappingList.add(rawMapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
Class<?> type = refField.getType();
|
||||||
|
// 字段注解反序列化
|
||||||
|
Converter fieldConverter = refField.getType().getAnnotation(Converter.class);
|
||||||
|
if (fieldConverter != null) {
|
||||||
|
BaseConverter<?, ?> converter = Ref.newInstance(fieldConverter.value());
|
||||||
|
refField.getSetter().invoke(object, converter.deserialize0(refField.getField(), item.getValue()));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 类型反序列化
|
||||||
|
if (converterMap.containsKey(type)) {
|
||||||
|
refField.getSetter().invoke(object, converterMap.get(type).deserialize0(refField.getField(), item.getValue()));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 默认反序列化
|
||||||
|
if (item.getValue() instanceof Map<?, ?> valueMap) {
|
||||||
|
// 子级对象
|
||||||
|
Object newObject = type.getDeclaredConstructor().newInstance();
|
||||||
|
refField.getSetter().invoke(object, newObject);
|
||||||
|
deserializeToValue(newObject, valueMap, stack + item.getKey() + "#");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 数组
|
||||||
|
if (item.getValue() instanceof List<?> valueList) {
|
||||||
|
// 数组
|
||||||
|
if (type.isArray()) {
|
||||||
|
Class<?> arrayType = type.getComponentType();
|
||||||
|
Object array = Array.newInstance(arrayType, valueList.size());
|
||||||
|
if (arrayType.isPrimitive()) {
|
||||||
|
// 基本类型数组
|
||||||
|
if (TimiJava.isNotEmpty(valueList)) {
|
||||||
|
Method method = Ref.getMethod(valueList.get(0).getClass(), arrayType.getName() + "Value");
|
||||||
|
for (int i = 0; i < valueList.size(); i++) {
|
||||||
|
Array.set(array, i, method.invoke(valueList.get(i)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i < valueList.size(); i++) {
|
||||||
|
Array.set(array, i, arrayType.cast(valueList.get(i)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
refField.getSetter().invoke(object, array);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 列表
|
||||||
|
refField.getSetter().invoke(object, valueList);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 枚举
|
||||||
|
if (Enum.class.isAssignableFrom(type)) {
|
||||||
|
refField.getSetter().invoke(object, Ref.toType((Class<Enum<?>>) type, item.getValue().toString()));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
refField.getSetter().invoke(object, item.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getRawValue() {
|
||||||
|
return rawValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-04-10 00:45
|
||||||
|
*/
|
||||||
|
private static class RawMapper {
|
||||||
|
|
||||||
|
Object owner;
|
||||||
|
|
||||||
|
RefField refField;
|
||||||
|
|
||||||
|
String rawStack;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/main/java/com/imyeyu/config/ConfigRepresenter.java
Normal file
18
src/main/java/com/imyeyu/config/ConfigRepresenter.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.imyeyu.config;
|
||||||
|
|
||||||
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
|
import org.yaml.snakeyaml.nodes.Tag;
|
||||||
|
import org.yaml.snakeyaml.representer.Representer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-05-01 14:03
|
||||||
|
*/
|
||||||
|
public class ConfigRepresenter extends Representer {
|
||||||
|
|
||||||
|
public ConfigRepresenter(DumperOptions options) {
|
||||||
|
super(options);
|
||||||
|
|
||||||
|
this.nullRepresenter = data -> representScalar(Tag.NULL, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/main/java/com/imyeyu/config/Converter.java
Normal file
14
src/main/java/com/imyeyu/config/Converter.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.imyeyu.config;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-04-26 00:30
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface Converter {
|
||||||
|
|
||||||
|
Class<? extends BaseConverter<?, ?>> value();
|
||||||
|
}
|
||||||
15
src/main/java/com/imyeyu/config/StringConverter.java
Normal file
15
src/main/java/com/imyeyu/config/StringConverter.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package com.imyeyu.config;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-04-27 10:38
|
||||||
|
*/
|
||||||
|
public abstract class StringConverter<T> extends BaseConverter<T, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String serialize(Field field, T t) {
|
||||||
|
return t.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
42
src/test/java/test/Config.java
Normal file
42
src/test/java/test/Config.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-04-25 15:37
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
public enum EnumTest {
|
||||||
|
|
||||||
|
TEST1,
|
||||||
|
|
||||||
|
TEST2
|
||||||
|
}
|
||||||
|
|
||||||
|
private String str;
|
||||||
|
|
||||||
|
private int num;
|
||||||
|
|
||||||
|
private Obj obj;
|
||||||
|
|
||||||
|
private boolean bool;
|
||||||
|
|
||||||
|
private EnumTest enumTest;
|
||||||
|
|
||||||
|
private ConfigConverterTest converterTest;
|
||||||
|
|
||||||
|
private int[] list;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Obj {
|
||||||
|
|
||||||
|
private String objStr;
|
||||||
|
|
||||||
|
private List<String> objList;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/test/java/test/ConfigConverterTest.java
Normal file
13
src/test/java/test/ConfigConverterTest.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-04-26 18:08
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class ConfigConverterTest {
|
||||||
|
|
||||||
|
private String value;
|
||||||
|
}
|
||||||
106
src/test/java/test/TestConfig.java
Normal file
106
src/test/java/test/TestConfig.java
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import com.imyeyu.config.BaseConverter;
|
||||||
|
import com.imyeyu.config.ConfigLoader;
|
||||||
|
import com.imyeyu.io.IO;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @version 2024-04-25 11:13
|
||||||
|
*/
|
||||||
|
public class TestConfig {
|
||||||
|
|
||||||
|
private static final String FILE_PATH = "test.yaml";
|
||||||
|
|
||||||
|
public <T> ConfigLoader<T> load(Class<T> clazz) {
|
||||||
|
ConfigLoader<T> loader = new ConfigLoader<>(FILE_PATH, clazz);
|
||||||
|
loader.load();
|
||||||
|
return loader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigLoader<T> reload(Class<T> clazz) {
|
||||||
|
IO.destroy(new File(FILE_PATH));
|
||||||
|
return load(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLoad() {
|
||||||
|
ConfigLoader<Object> loader = reload(null);
|
||||||
|
loader.load();
|
||||||
|
assert loader.getRawValue() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRaw() {
|
||||||
|
ConfigLoader<Object> loader = reload(Object.class);
|
||||||
|
Map<String, Object> rawValue = loader.getRawValue();
|
||||||
|
rawValue.put("str", "str value update");
|
||||||
|
loader.dump();
|
||||||
|
loader.load();
|
||||||
|
rawValue = loader.getRawValue();
|
||||||
|
assert rawValue.get("str").equals("str value update");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testObject() {
|
||||||
|
ConfigLoader<Config> loader = reload(Config.class);
|
||||||
|
Config value = loader.getValue();
|
||||||
|
assert value.getStr().equals("str value");
|
||||||
|
assert value.getNum() == 123;
|
||||||
|
assert value.getList().length == 4;
|
||||||
|
assert value.getObj() != null;
|
||||||
|
assert value.getObj().getObjStr().equals("test str");
|
||||||
|
assert value.getObj().getObjList().size() == 2;
|
||||||
|
|
||||||
|
value.setStr("dump object test");
|
||||||
|
value.getObj().getObjList().add(0, "new list item test");
|
||||||
|
loader.dump();
|
||||||
|
|
||||||
|
value = loader.load();
|
||||||
|
assert value.getStr().equals("dump object test");
|
||||||
|
assert value.getObj().getObjList().get(0).equals("new list item test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnum() {
|
||||||
|
ConfigLoader<Config> loader = reload(Config.class);
|
||||||
|
Config value = loader.getValue();
|
||||||
|
assert value.getEnumTest() == Config.EnumTest.TEST2;
|
||||||
|
value.setEnumTest(Config.EnumTest.TEST1);
|
||||||
|
loader.dump();
|
||||||
|
value = loader.load();
|
||||||
|
assert value.getEnumTest() == Config.EnumTest.TEST1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConverter() {
|
||||||
|
IO.destroy(new File("test_converter.yaml"));
|
||||||
|
|
||||||
|
ConfigLoader<Config> loader = new ConfigLoader<>("test_converter.yaml", Config.class);
|
||||||
|
loader.addConverter(ConfigConverterTest.class, new BaseConverter<ConfigConverterTest, String>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String serialize(Field field, ConfigConverterTest configConverterTest) {
|
||||||
|
return configConverterTest.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ConfigConverterTest deserialize(Field field, String data) {
|
||||||
|
ConfigConverterTest configConverterTest = new ConfigConverterTest();
|
||||||
|
configConverterTest.setValue(data);
|
||||||
|
return configConverterTest;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Config value = loader.load();
|
||||||
|
assert value.getConverterTest().getValue().equals("converter value");
|
||||||
|
value.getConverterTest().setValue("converter test update");
|
||||||
|
loader.dump();
|
||||||
|
value = loader.load();
|
||||||
|
assert value.getConverterTest().getValue().equals("converter test update");
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/test/resources/test.yaml
Normal file
16
src/test/resources/test.yaml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# comment
|
||||||
|
str: "str value"
|
||||||
|
num: 123
|
||||||
|
bool: true
|
||||||
|
enum_test: TEST2
|
||||||
|
obj:
|
||||||
|
# comment 2
|
||||||
|
obj_str: test str
|
||||||
|
obj_list:
|
||||||
|
- str item0
|
||||||
|
- str item1
|
||||||
|
list:
|
||||||
|
- 0
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
17
src/test/resources/test_converter.yaml
Normal file
17
src/test/resources/test_converter.yaml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# comment
|
||||||
|
str: "str value"
|
||||||
|
num: 123
|
||||||
|
bool: true
|
||||||
|
enum_test: TEST2
|
||||||
|
converter_test: converter value
|
||||||
|
obj:
|
||||||
|
# comment 2
|
||||||
|
obj_str: test str
|
||||||
|
obj_list:
|
||||||
|
- str item0
|
||||||
|
- str item1
|
||||||
|
list:
|
||||||
|
- 0
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
15
test.yaml
Normal file
15
test.yaml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
str: dump object test
|
||||||
|
num: 123
|
||||||
|
bool: true
|
||||||
|
enum_test: TEST2
|
||||||
|
obj:
|
||||||
|
obj_str: test str
|
||||||
|
obj_list:
|
||||||
|
- new list item test
|
||||||
|
- str item0
|
||||||
|
- str item1
|
||||||
|
list:
|
||||||
|
- 0
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
15
test_converter.yaml
Normal file
15
test_converter.yaml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
str: str value
|
||||||
|
num: 123
|
||||||
|
bool: true
|
||||||
|
enum_test: TEST2
|
||||||
|
converter_test: converter test update
|
||||||
|
obj:
|
||||||
|
obj_str: test str
|
||||||
|
obj_list:
|
||||||
|
- str item0
|
||||||
|
- str item1
|
||||||
|
list:
|
||||||
|
- 0
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
Reference in New Issue
Block a user