Compare commits
6 Commits
df0610c15f
...
v0.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
| 59d7ef29a8 | |||
|
|
62f35bdb78 | ||
| 3dc4566ba8 | |||
|
|
d014878b39 | ||
| 395ed36c4a | |||
|
|
6b4a485ae5 |
111
.gitea/workflows/ci.yml
Normal file
111
.gitea/workflows/ci.yml
Normal file
@@ -0,0 +1,111 @@
|
||||
name: CI/CD
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
types:
|
||||
- closed
|
||||
|
||||
jobs:
|
||||
build-deploy:
|
||||
runs-on: act_runner_java
|
||||
if: ${{ github.event.pull_request.merged == true }}
|
||||
env:
|
||||
JAVA_HOME: /usr/lib/jvm/java-21-openjdk
|
||||
steps:
|
||||
- name: Checkout code
|
||||
run: |
|
||||
git clone ${{ github.server_url }}/${{ github.repository }}.git .
|
||||
git checkout ${{ github.sha }}
|
||||
- name: Set up environment
|
||||
run: |
|
||||
echo "PR #${{ github.event.number }} merged into master"
|
||||
echo "Source branch: ${{ github.event.pull_request.head.ref }}"
|
||||
echo "Target branch: ${{ github.event.pull_request.base.ref }}"
|
||||
- name: Run tests
|
||||
run: |
|
||||
echo "Running test suite..."
|
||||
- name: Build project
|
||||
run: |
|
||||
mvn -B -DskipTests clean package source:jar javadoc:jar
|
||||
- name: Deploy to Nexus
|
||||
if: success()
|
||||
run: |
|
||||
if [ -z "${{ secrets.NEXUS_USERNAME }}" ] || [ -z "${{ secrets.NEXUS_PASSWORD }}" ]; then
|
||||
echo "Missing secrets.NEXUS_USERNAME or secrets.NEXUS_PASSWORD"
|
||||
exit 1
|
||||
fi
|
||||
mkdir -p ~/.m2
|
||||
cat > ~/.m2/settings.xml <<EOF
|
||||
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
|
||||
<servers>
|
||||
<server>
|
||||
<id>timi-nexus</id>
|
||||
<username>${{ secrets.NEXUS_USERNAME }}</username>
|
||||
<password>${{ secrets.NEXUS_PASSWORD }}</password>
|
||||
</server>
|
||||
</servers>
|
||||
</settings>
|
||||
EOF
|
||||
version=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.version)
|
||||
artifact_id=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.artifactId)
|
||||
main_jar="target/${artifact_id}-${version}.jar"
|
||||
sources_jar="target/${artifact_id}-${version}-sources.jar"
|
||||
javadoc_jar="target/${artifact_id}-${version}-javadoc.jar"
|
||||
if [ ! -f "$main_jar" ] || [ ! -f "$sources_jar" ] || [ ! -f "$javadoc_jar" ]; then
|
||||
echo "Missing build artifacts in target"
|
||||
exit 1
|
||||
fi
|
||||
mvn -B deploy:deploy-file \
|
||||
-Dfile="$main_jar" \
|
||||
-Dsources="$sources_jar" \
|
||||
-Djavadoc="$javadoc_jar" \
|
||||
-DpomFile="./pom.xml" \
|
||||
-Durl="https://nexus.imyeyu.com/repository/maven-releases/" \
|
||||
-DrepositoryId="timi-nexus" \
|
||||
-Dhttps.protocols=TLSv1.2 \
|
||||
-Djdk.tls.client.protocols=TLSv1.2
|
||||
- name: Create release
|
||||
if: ${{ success() && startsWith(github.event.pull_request.title, 'v') }}
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.RUNNER_TOKEN }}
|
||||
GITEA_SERVER_URL: ${{ github.server_url }}
|
||||
GITEA_REPOSITORY: ${{ github.repository }}
|
||||
RELEASE_TAG: ${{ github.event.pull_request.title }}
|
||||
RELEASE_TARGET: ${{ github.sha }}
|
||||
run: |
|
||||
if [ -z "$GITEA_TOKEN" ]; then
|
||||
echo "Missing secrets.RUNNER_TOKEN"
|
||||
exit 1
|
||||
fi
|
||||
api_url="$GITEA_SERVER_URL/api/v1/repos/$GITEA_REPOSITORY/releases"
|
||||
payload=$(cat <<EOF
|
||||
{
|
||||
"tag_name": "$RELEASE_TAG",
|
||||
"name": "$RELEASE_TAG",
|
||||
"target_commitish": "$RELEASE_TARGET",
|
||||
"draft": false,
|
||||
"prerelease": false
|
||||
}
|
||||
EOF
|
||||
)
|
||||
response=$(curl -sS -X POST "$api_url" \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$payload")
|
||||
release_id=$(echo "$response" | grep -o '"id":[0-9]*' | head -n 1 | grep -o '[0-9]*')
|
||||
if [ -z "$release_id" ] || echo "$response" | grep -q '"message"'; then
|
||||
echo "Create release failed: $response"
|
||||
exit 1
|
||||
fi
|
||||
echo "Release created: id=$release_id"
|
||||
for asset_path in target/*.jar; do
|
||||
asset_name=$(basename "$asset_path")
|
||||
curl -sS -X POST "$api_url/$release_id/assets?name=$asset_name" \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary @"$asset_path"
|
||||
done
|
||||
82
pom.xml
82
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.config</groupId>
|
||||
<artifactId>timi-config</artifactId>
|
||||
<version>0.0.2</version>
|
||||
<version>0.0.3</version>
|
||||
|
||||
<properties>
|
||||
<maven.test.skip>true</maven.test.skip>
|
||||
@@ -15,27 +15,99 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>3.1.3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok-maven-plugin</artifactId>
|
||||
<version>1.18.20.0</version>
|
||||
<configuration>
|
||||
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
|
||||
<outputDirectory>${project.build.directory}/delombok</outputDirectory>
|
||||
<addOutputDirectory>false</addOutputDirectory>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>delombok</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.36</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.11.2</version>
|
||||
<configuration>
|
||||
<sourcepath>${project.build.directory}/delombok</sourcepath>
|
||||
<encoding>UTF-8</encoding>
|
||||
<charset>UTF-8</charset>
|
||||
<docencoding>UTF-8</docencoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>timi_nexus</id>
|
||||
<url>https://nexus.imyeyu.com/repository/maven-releases/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>timi_nexus</id>
|
||||
<url>https://nexus.imyeyu.com/repository/maven-public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>2.2</version>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.io</groupId>
|
||||
<artifactId>timi-io</artifactId>
|
||||
<version>0.0.2</version>
|
||||
<version>0.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.10.3</version>
|
||||
<version>6.0.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.34</version>
|
||||
<version>1.18.46</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -11,8 +11,12 @@ 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.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -40,6 +44,9 @@ public class ConfigLoader<T> {
|
||||
|
||||
/**
|
||||
* 创建配置加载器,源路径和目标路径相同
|
||||
*
|
||||
* @param path 源路径/目标路径
|
||||
* @param clazz 配置类型
|
||||
*/
|
||||
public ConfigLoader(String path, Class<T> clazz) {
|
||||
this(path, path, clazz);
|
||||
@@ -69,7 +76,7 @@ public class ConfigLoader<T> {
|
||||
LoaderOptions loaderOptions = new LoaderOptions();
|
||||
loaderOptions.setAllowRecursiveKeys(false); // 禁止递归键
|
||||
loaderOptions.setMaxAliasesForCollections(50); // 限制别名数量
|
||||
loaderOptions.setCodePointLimit((int) (IOSize.MB * 3)); // 限制 3 MB
|
||||
loaderOptions.setCodePointLimit((int) (IOSize.MB * 10)); // 限制 10 MB
|
||||
|
||||
DumperOptions dumperOptions = new DumperOptions();
|
||||
dumperOptions.setIndent(4);
|
||||
@@ -96,13 +103,7 @@ public class ConfigLoader<T> {
|
||||
public T load() {
|
||||
try {
|
||||
String content = IO.toString(file);
|
||||
String normalized = content.replaceAll("\t", " ");
|
||||
if (converters.isEmpty()) {
|
||||
// SnakeYAML 自动转换 - 使用指定类型构造
|
||||
value = yaml.load(normalized);
|
||||
return value;
|
||||
}
|
||||
Object raw = rawYaml.load(normalized);
|
||||
Object raw = rawYaml.load(content);
|
||||
if (raw == null) {
|
||||
value = null;
|
||||
return null;
|
||||
@@ -129,9 +130,8 @@ public class ConfigLoader<T> {
|
||||
Object toSerialize = applyConvertersOnDump(value);
|
||||
// SnakeYAML 序列化
|
||||
String yamlContent = yaml.dump(toSerialize);
|
||||
// 空格转制表符
|
||||
String toWrite = yamlContent.replaceAll("( ){4}", " ");
|
||||
IO.toFile(file, toWrite);
|
||||
|
||||
IO.toFile(file, yamlContent);
|
||||
} catch (Exception e) {
|
||||
throw new TimiException(TimiCode.ERROR, "dump config error: " + file.getAbsolutePath(), e);
|
||||
}
|
||||
@@ -163,11 +163,10 @@ public class ConfigLoader<T> {
|
||||
Class<?> currentClass = target.getClass();
|
||||
while (currentClass != null && currentClass != Object.class) {
|
||||
for (Field field : currentClass.getDeclaredFields()) {
|
||||
String fieldName = field.getName();
|
||||
if (!map.containsKey(fieldName)) {
|
||||
Object rawValue = resolveMapValue(map, field.getName());
|
||||
if (rawValue == null && !containsFieldKey(map, field.getName())) {
|
||||
continue;
|
||||
}
|
||||
Object rawValue = map.get(fieldName);
|
||||
Object converted = convertFieldValue(field, rawValue);
|
||||
field.setAccessible(true);
|
||||
field.set(target, converted);
|
||||
@@ -176,6 +175,17 @@ public class ConfigLoader<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean containsFieldKey(Map<?, ?> map, String fieldName) {
|
||||
return map.containsKey(fieldName) || map.containsKey(toSnakeCase(fieldName));
|
||||
}
|
||||
|
||||
private Object resolveMapValue(Map<?, ?> map, String fieldName) {
|
||||
if (map.containsKey(fieldName)) {
|
||||
return map.get(fieldName);
|
||||
}
|
||||
return map.get(toSnakeCase(fieldName));
|
||||
}
|
||||
|
||||
private Object convertFieldValue(Field field, Object rawValue) {
|
||||
BaseConverter<?, ?> converter = converters.get(field.getType());
|
||||
if (converter != null) {
|
||||
@@ -188,12 +198,40 @@ public class ConfigLoader<T> {
|
||||
if (fieldType.isEnum()) {
|
||||
return toEnumValue(fieldType, rawValue.toString());
|
||||
}
|
||||
if (fieldType.isArray() && rawValue instanceof Iterable<?> rawIterable) {
|
||||
return toArrayValue(fieldType.getComponentType(), rawIterable);
|
||||
}
|
||||
if (rawValue instanceof Map<?, ?> rawMap && shouldMapToBean(fieldType)) {
|
||||
return mapToBean(rawMap, fieldType);
|
||||
}
|
||||
return convertScalarValue(fieldType, rawValue);
|
||||
}
|
||||
|
||||
private Object toArrayValue(Class<?> componentType, Iterable<?> rawIterable) {
|
||||
List<Object> values = new ArrayList<>();
|
||||
for (Object rawElement : rawIterable) {
|
||||
values.add(convertArrayElement(componentType, rawElement));
|
||||
}
|
||||
Object array = Array.newInstance(componentType, values.size());
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
Array.set(array, i, values.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
private Object convertArrayElement(Class<?> componentType, Object rawElement) {
|
||||
if (rawElement == null) {
|
||||
return null;
|
||||
}
|
||||
if (componentType.isEnum()) {
|
||||
return toEnumValue(componentType, rawElement.toString());
|
||||
}
|
||||
if (rawElement instanceof Map<?, ?> rawMap && shouldMapToBean(componentType)) {
|
||||
return mapToBean(rawMap, componentType);
|
||||
}
|
||||
return convertScalarValue(componentType, rawElement);
|
||||
}
|
||||
|
||||
private boolean shouldMapToBean(Class<?> fieldType) {
|
||||
if (fieldType.isPrimitive() || fieldType.isEnum()) {
|
||||
return false;
|
||||
@@ -251,35 +289,92 @@ public class ConfigLoader<T> {
|
||||
}
|
||||
|
||||
/** 序列化时应用转换器 */
|
||||
@SuppressWarnings("unchecked")
|
||||
private <K> K applyConvertersOnDump(K obj) {
|
||||
if (obj == null || converters.isEmpty()) {
|
||||
return obj;
|
||||
private Object applyConvertersOnDump(Object obj) {
|
||||
return toYamlValue(null, obj);
|
||||
}
|
||||
try {
|
||||
// 创建副本
|
||||
K copy = (K) obj.getClass().getDeclaredConstructor().newInstance();
|
||||
Class<?> currentClass = obj.getClass();
|
||||
while (currentClass != null && currentClass != Object.class) {
|
||||
for (Field field : currentClass.getDeclaredFields()) {
|
||||
field.setAccessible(true);
|
||||
Object fieldValue = field.get(obj);
|
||||
|
||||
private Object toYamlValue(Field field, Object value) {
|
||||
if (field != null) {
|
||||
BaseConverter<?, ?> converter = converters.get(field.getType());
|
||||
if (converter != null) {
|
||||
// 应用转换器
|
||||
Object yamlValue = converter.serialize0(field, fieldValue);
|
||||
field.set(copy, yamlValue);
|
||||
} else {
|
||||
// 直接复制
|
||||
field.set(copy, fieldValue);
|
||||
return converter.serialize0(field, value);
|
||||
}
|
||||
}
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
Class<?> valueType = value.getClass();
|
||||
if (valueType.isEnum()) {
|
||||
return ((Enum<?>) value).name();
|
||||
}
|
||||
if (isScalarType(valueType)) {
|
||||
return value;
|
||||
}
|
||||
if (valueType.isArray()) {
|
||||
int length = Array.getLength(value);
|
||||
List<Object> result = new ArrayList<>(length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
result.add(toYamlValue(null, Array.get(value, i)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (value instanceof Iterable<?> iterable) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object item : iterable) {
|
||||
result.add(toYamlValue(null, item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (value instanceof Map<?, ?> mapValue) {
|
||||
Map<Object, Object> result = new LinkedHashMap<>();
|
||||
for (Map.Entry<?, ?> entry : mapValue.entrySet()) {
|
||||
result.put(entry.getKey(), toYamlValue(null, entry.getValue()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (!shouldMapToBean(valueType)) {
|
||||
return value;
|
||||
}
|
||||
try {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
Class<?> currentClass = valueType;
|
||||
while (currentClass != null && currentClass != Object.class) {
|
||||
for (Field declaredField : currentClass.getDeclaredFields()) {
|
||||
declaredField.setAccessible(true);
|
||||
Object fieldValue = toYamlValue(declaredField, declaredField.get(value));
|
||||
if (fieldValue != null) {
|
||||
result.put(toSnakeCase(declaredField.getName()), fieldValue);
|
||||
}
|
||||
}
|
||||
currentClass = currentClass.getSuperclass();
|
||||
}
|
||||
return copy;
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
throw new TimiException(TimiCode.ERROR, "converter error on dump", e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isScalarType(Class<?> type) {
|
||||
return type.isPrimitive()
|
||||
|| type == String.class
|
||||
|| Number.class.isAssignableFrom(type)
|
||||
|| type == Boolean.class
|
||||
|| type == Character.class;
|
||||
}
|
||||
|
||||
private String toSnakeCase(String fieldName) {
|
||||
StringBuilder builder = new StringBuilder(fieldName.length() + 4);
|
||||
for (int i = 0; i < fieldName.length(); i++) {
|
||||
char current = fieldName.charAt(i);
|
||||
if (Character.isUpperCase(current)) {
|
||||
if (i != 0) {
|
||||
builder.append('_');
|
||||
}
|
||||
builder.append(Character.toLowerCase(current));
|
||||
continue;
|
||||
}
|
||||
builder.append(current);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user