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

feat(json): Finished json version's provider

This commit is contained in:
2025-02-13 01:30:10 +08:00
parent 05ff61a9d9
commit 79f59bafe6
20 changed files with 125 additions and 95 deletions
@@ -14,7 +14,7 @@ public @interface ConfigPath {
/**
* The path value of the current configuration.
* If not set,will generate the path by {@link PathGenerator}.
* If not set,will generate the path by {@link cc.carm.lib.configuration.source.loader.PathGenerator}.
*
* @return The path value of the current configuration
*/
@@ -41,7 +41,7 @@ public abstract class AbstractConfigBuilder<
public abstract @NotNull RESULT build();
public @NotNull SELF provider(@Nullable PROVIDER provider) {
public @NotNull SELF holder(@Nullable PROVIDER provider) {
this.provider = provider;
return self();
}
@@ -83,29 +83,31 @@ public class ConfigurationInitializer {
}
public @Nullable String getFieldPath(ConfigurationHolder<?> holder, @Nullable String parentPath, @NotNull Field field) {
public @Nullable String getFieldPath(@NotNull ConfigurationHolder<?> holder, @Nullable String parentPath, @NotNull Field field) {
return pathGenerator.getFieldPath(holder, parentPath, field);
}
public @Nullable String getClassPath(ConfigurationHolder<?> holder, @Nullable String parentPath,
public @Nullable String getClassPath(@NotNull ConfigurationHolder<?> holder, @Nullable String parentPath,
@NotNull Class<?> clazz, @Nullable Field clazzField) {
return pathGenerator.getClassPath(holder, parentPath, clazz, clazzField);
}
public void initialize(ConfigurationHolder<?> holder, @NotNull Configuration config) throws Exception {
public void initialize(@NotNull ConfigurationHolder<?> holder,
@NotNull Configuration config) throws Exception {
initializeInstance(holder, config, null, null);
if (holder.options().get(StandardOptions.SET_DEFAULTS)) holder.save();
}
public void initialize(ConfigurationHolder<?> holder, @NotNull Class<? extends Configuration> clazz) throws Exception {
public void initialize(@NotNull ConfigurationHolder<?> holder,
@NotNull Class<? extends Configuration> clazz) throws Exception {
initializeStaticClass(holder, clazz, null, null);
if (holder.options().get(StandardOptions.SET_DEFAULTS)) holder.save();
}
// 针对实例类的初始化方法
protected void initializeInstance(@NotNull ConfigurationHolder<?> holder,
@NotNull Configuration root, @Nullable String parentPath, @Nullable Field configField) {
protected void initializeInstance(@NotNull ConfigurationHolder<?> holder, @NotNull Configuration root,
@Nullable String parentPath, @Nullable Field configField) {
String path = getClassPath(holder, parentPath, root.getClass(), configField);
try {
this.classInitializer.whenInitialize(holder, path, root.getClass());
@@ -118,8 +120,9 @@ public class ConfigurationInitializer {
// 针对静态类的初始化方法
@SuppressWarnings("unchecked")
protected void initializeStaticClass(@NotNull ConfigurationHolder<?> holder,
@NotNull Class<?> clazz, @Nullable String parentPath, @Nullable Field configField) {
if (!Configuration.class.isAssignableFrom(clazz)) return; // 只解析继承了 ConfigurationRoot 的类
@NotNull Class<?> clazz,
@Nullable String parentPath, @Nullable Field configField) {
if (!Configuration.class.isAssignableFrom(clazz)) return; // Only Configuration class can be initialized.
String path = getClassPath(holder, parentPath, clazz, configField);
@@ -158,6 +161,9 @@ public class ConfigurationInitializer {
} catch (Exception e) {
e.printStackTrace();
}
if (holder.options().get(StandardOptions.SET_DEFAULTS)) {
value.setDefault();
}
} else if (source instanceof Configuration && object instanceof Configuration) {
// 当且仅当 源字段与字段 均为Configuration实例时,才对目标字段进行下一步初始化加载。
initializeInstance(holder, (Configuration) object, parent, field);
@@ -40,8 +40,9 @@ public class PathGenerator {
public @Nullable String getFieldPath(@NotNull ConfigurationHolder<?> holder,
@Nullable String parentPath, @NotNull Field field) {
ConfigPath path = field.getAnnotation(ConfigPath.class);
if (path == null) return link(holder, parentPath, false, field.getName()); // No annotation, use field name.
else return link(holder, parentPath, path.root(), select(path.value(), field.getName()));
if (path == null)
return link(holder, parentPath, false, covertPath(field.getName())); // No annotation, use field name.
else return link(holder, parentPath, path.root(), select(path.value(), covertPath(field.getName())));
}
public @Nullable String getClassPath(@NotNull ConfigurationHolder<?> holder,
@@ -51,14 +52,14 @@ public class PathGenerator {
// 2. If the class defined as a field, check if the field has a ConfigPath annotation,
// and use filed information.
ConfigPath clazzPath = clazz.getAnnotation(ConfigPath.class);
if (clazzPath != null) return link(holder, parentPath, clazzPath.root(), clazzPath.value());
if (clazzField == null) {
return link(holder, parentPath, false, clazz.getSimpleName()); // No field, use class name.
return link(holder, parentPath, false, parentPath == null ? null : covertPath(clazz.getSimpleName())); // No field, use class name.
}
ConfigPath fieldPath = clazzField.getAnnotation(ConfigPath.class);
if (fieldPath == null) return link(holder, parentPath, false, clazzField.getName());
if (fieldPath == null) return link(holder, parentPath, false, covertPath(clazzField.getName()));
else return getFieldPath(holder, parentPath, clazzField);
}
@@ -70,7 +71,7 @@ public class PathGenerator {
protected @Nullable String link(@NotNull ConfigurationHolder<?> holder,
@Nullable String parent, boolean root, @Nullable String path) {
if (path == null || path.isEmpty()) return root ? null : parent;
return root || parent == null ? covertPath(path) : parent + pathSeparator(holder) + covertPath(path);
return root || parent == null ? path : parent + pathSeparator(holder) + path;
}
public static boolean isBlank(String path) {
@@ -93,16 +94,16 @@ public class PathGenerator {
public static String covertPathName(String name) {
return name
// Replace all uppercase letters with dashes
.replaceAll("[A-Z]", "-$0")
.replaceAll("[A-Z]", "=$0")
// If the first letter is also capitalized,
// it will also be converted and the first dash will need to be removed
.replaceAll("-(.*)", "$1")
.replaceAll("^=(.*)$", "$1")
// Because the name may contain _, it needs to be treated a little differently
.replaceAll("_-([A-Z])", "_$1")
.replaceAll("_=([A-Z])", "_$1")
// The content that is not named in all caps is then converted
.replaceAll("([a-z])-([A-Z])", "$1_$2")
.replaceAll("([a-z])=([A-Z])", "$1_$2")
// Remove any extra horizontal lines
.replace("-", "")
.replaceAll("=", "")
// Replace the underscore with a dash
.replace("_", "-")
// Finally, convert it to all lowercase