Skip to content

Commit

Permalink
[2.3.0] 版本更新
Browse files Browse the repository at this point in the history
- [U] 基于 tchristofferson/ConfigUpdater 项目重写YAML相关配置文件的注释部分。
- [A] 为 @ConfigComment 注解添加 ”statWrap“ 与 "endWrap" 两个选项,用于实现不同样式的注释。
  • Loading branch information
CarmJos committed Apr 23, 2022
1 parent 760ac81 commit 390815b
Show file tree
Hide file tree
Showing 30 changed files with 358 additions and 120 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>2.2.0</version>
<version>2.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cc.carm.lib.configuration.core.annotation.ConfigComment;
import cc.carm.lib.configuration.core.annotation.ConfigPath;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -62,7 +63,7 @@ protected void initializeClass(@NotNull Class<?> clazz,
@Nullable ConfigPath fieldPath, @Nullable ConfigComment filedComments,
boolean saveDefaults, boolean loadSubClasses) {
String path = getClassPath(clazz, parentPath, fieldName, fieldPath);
if (path != null) setComments(path, getClassComments(clazz, filedComments));
this.provider.setComment(path, getClassComments(clazz, filedComments));
for (Field field : clazz.getDeclaredFields()) {
initializeField(clazz, field, path, saveDefaults, loadSubClasses);
}
Expand All @@ -82,21 +83,21 @@ protected void initializeClass(@NotNull Class<?> clazz,


protected void initializeValue(@NotNull ConfigValue<?> value, @NotNull String path,
@NotNull String[] comments, boolean saveDefaults) {
@Nullable ConfigCommentInfo comments, boolean saveDefaults) {
value.initialize(provider, saveDefaults, path, comments);
}

private void initializeField(@NotNull Class<?> source, @NotNull Field field, @Nullable String parent,
boolean saveDefaults, boolean loadSubClasses) {

try {
field.setAccessible(true);
Object object = field.get(source);
if (object instanceof ConfigValue<?>) {
String path = getFieldPath(field, parent);
String[] comments = readComments(field.getAnnotation(ConfigComment.class));
initializeValue((ConfigValue<?>) object, path, comments, saveDefaults);
setComments(path, comments);
initializeValue(
(ConfigValue<?>) object, getFieldPath(field, parent),
ConfigCommentInfo.fromAnnotation(field.getAnnotation(ConfigComment.class)),
saveDefaults
);
} else if (object instanceof Class<?>) {
initializeClass(
(Class<?>) object, parent, field.getName(),
Expand All @@ -109,26 +110,15 @@ private void initializeField(@NotNull Class<?> source, @NotNull Field field, @Nu
}
}

protected void setComments(@NotNull String path, @Nullable ConfigComment filedComments) {
setComments(path, readComments(filedComments));
}

protected void setComments(@NotNull String path, @NotNull String[] comments) {
if (comments.length <= 0) return;
this.provider.setComments(path, comments);
}

protected static @NotNull String[] readComments(@Nullable ConfigComment filedComments) {
if (filedComments == null) return new String[0];
if (String.join("", filedComments.value()).length() <= 0) return new String[0];
return filedComments.value();
protected void setComments(@Nullable String path, @Nullable ConfigCommentInfo comments) {
if (comments != null) this.provider.setComment(path, comments);
}

protected static @NotNull String[] getClassComments(@NotNull Class<?> clazz,
@Nullable ConfigComment fieldAnnotation) {
String[] clazzComments = readComments(clazz.getAnnotation(ConfigComment.class));
if (clazzComments.length > 0) return clazzComments;
else return readComments(fieldAnnotation);
protected static @Nullable ConfigCommentInfo getClassComments(@NotNull Class<?> clazz,
@Nullable ConfigComment fieldAnnotation) {
ConfigCommentInfo classComments = ConfigCommentInfo.fromAnnotation(clazz.getAnnotation(ConfigComment.class));
if (classComments != null) return classComments;
return ConfigCommentInfo.fromAnnotation(fieldAnnotation);
}

protected static @Nullable String getClassPath(@NotNull Class<?> clazz,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,32 @@
@NotNull
String[] value() default "";

/**
* 首行换行,即会在注释开始前进行一次换行,与上方配置分离,优化观感。
* 如:
* <blockquote><pre>
* some-key: "SomeValue"
*
* # 注释第一行
* # 注释第二行
* startWrap: true
* </pre></blockquote>
*
* @return 是否在结尾添加换行符
*/
boolean startWrap() default true;

/**
* 末尾换行,即会在注释结束后进行一次换行,如:
* <blockquote><pre>
* # 注释第一行
* # 注释第二行
*
* endWrap: true
* </pre></blockquote>
* <p> 该功能可用于编写配置文件的顶部注释。
*
* @return 是否在结尾添加换行符
*/
boolean endWrap() default false;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cc.carm.lib.configuration.core.builder;

import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.NotNull;
Expand All @@ -13,6 +14,9 @@ public abstract class AbstractConfigBuilder<T, B extends AbstractConfigBuilder<T
protected @Nullable String path;

protected @NotNull String[] comments = new String[0];
protected boolean startWrap = true;
protected boolean endWrap = false;

protected @Nullable T defaultValue;

public AbstractConfigBuilder(Class<? super P> providerClass) {
Expand All @@ -38,10 +42,34 @@ public AbstractConfigBuilder(Class<? super P> providerClass) {
return getThis();
}

public @NotNull B setStartWarp(boolean enable) {
this.startWrap = enable;
return getThis();
}

public @NotNull B startWarp() {
return setStartWarp(true);
}

public @NotNull B setEndWarp(boolean enable) {
this.endWrap = enable;
return getThis();
}

public @NotNull B endWarp() {
return setEndWarp(true);
}

public @NotNull B defaults(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
return getThis();
}

protected @Nullable ConfigCommentInfo buildComments() {
ConfigCommentInfo info = ConfigCommentInfo.of(this.comments, this.startWrap, this.endWrap);
if (info.equals(ConfigCommentInfo.defaults())) return null;
else return info;
}


}
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
package cc.carm.lib.configuration.core.builder;

import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class CommonConfigBuilder<T, B extends CommonConfigBuilder<T, B>>
extends AbstractConfigBuilder<T, B, ConfigurationProvider<?>> {

protected @Nullable ConfigurationProvider<?> provider;
protected @Nullable String path;

protected @NotNull String[] comments = new String[0];
protected @Nullable T defaultValue;

public CommonConfigBuilder() {
super(ConfigurationProvider.class);
}

protected abstract @NotNull B getThis();

public abstract @NotNull ConfigValue<?> build();

public @NotNull B from(@Nullable ConfigurationProvider<?> provider) {
this.provider = provider;
return getThis();
}

public @NotNull B path(@Nullable String path) {
this.path = path;
return getThis();
}

public @NotNull B comments(@NotNull String... comments) {
this.comments = comments;
return getThis();
}

public @NotNull B defaults(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
return getThis();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public SourceListBuilder(@NotNull Class<S> sourceClass, @NotNull ConfigDataFunct
@Override
public @NotNull ConfiguredList<V> build() {
return new ConfiguredList<>(
this.provider, this.path, this.comments,
this.provider, this.path, this.buildComments(),
this.valueClass, this.defaultValue,
this.sourceParser.andThen(this.valueParser),
this.valueSerializer.andThen(sourceSerializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public <MAP extends Map<K, V>> SourceMapBuilder<MAP, S, K, V> supplier(@NotNull
@Override
public @NotNull ConfiguredMap<K, V> build() {
return new ConfiguredMap<>(
this.provider, this.path, this.comments,
this.provider, this.path,this.buildComments(),
this.defaultValue, this.supplier,
this.keyClass, this.keyParser,
this.valueClass, this.sourceParser.andThen(this.valueParser),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public SectionValueBuilder(@NotNull Class<V> valueClass,
@Override
public @NotNull ConfiguredSection<V> build() {
return new ConfiguredSection<>(
this.provider, this.path, this.comments,
this.provider, this.path,this.buildComments(),
this.valueClass, this.defaultValue,
this.parser, this.serializer
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
import cc.carm.lib.configuration.core.function.ConfigValueParser;
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -57,7 +58,7 @@ public SourceValueBuilder(@NotNull Class<S> sourceClass, @NotNull ConfigDataFunc
@Override
public @NotNull ConfiguredValue<V> build() {
return new ConfiguredValue<>(
this.provider, this.path, this.comments,
this.provider, this.path, this.buildComments(),
this.valueClass, this.defaultValue,
this.valueParser.compose(this.sourceParser),
this.valueSerializer.andThen(sourceSerializer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cc.carm.lib.configuration.core.source;

import cc.carm.lib.configuration.core.annotation.ConfigComment;
import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

public class ConfigCommentInfo {

public static @NotNull ConfigCommentInfo DEFAULT_INFO = of(new String[0], true, false);

protected final @NotNull String[] comments;
protected final boolean startWrap;
protected final boolean endWrap;

public ConfigCommentInfo(@NotNull String[] comments, boolean startWrap, boolean endWrap) {
this.comments = comments;
this.startWrap = startWrap;
this.endWrap = endWrap;
}

public @NotNull String[] getComments() {
return comments;
}

public boolean endWrap() {
return endWrap;
}

public boolean startWrap() {
return startWrap;
}

public static @NotNull ConfigCommentInfo of(@NotNull String[] comments, boolean startWrap, boolean endWrap) {
return new ConfigCommentInfo(comments, startWrap, endWrap);
}

public static @NotNull ConfigCommentInfo defaults() {
return DEFAULT_INFO;
}

@Contract("!null->!null")
public static @Nullable ConfigCommentInfo fromAnnotation(@Nullable ConfigComment comment) {
if (comment == null) return null;
else return new ConfigCommentInfo(comment.value(), comment.startWrap(), comment.endWrap());
}

public static @NotNull ConfigCommentInfo fromValue(@NotNull ConfigValue<?> value) {
return Optional.ofNullable(value.getComments()).orElse(defaults());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConfigCommentInfo that = (ConfigCommentInfo) o;
return startWrap == that.startWrap && endWrap == that.endWrap && Arrays.equals(getComments(), that.getComments());
}

@Override
public int hashCode() {
int result = Objects.hash(startWrap, endWrap);
result = 31 * result + Arrays.hashCode(getComments());
return result;
}

@Override
public String toString() {
return "ConfigCommentInfo{" +
"comments=" + Arrays.toString(comments) +
", startWrap=" + startWrap +
", endWrap=" + endWrap +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public boolean isExpired(long time) {

public abstract void save() throws Exception;

public abstract void setComments(@NotNull String path, @NotNull String... comments);
public abstract void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment);

public abstract @Nullable String[] getComments(@NotNull String path);
public abstract @Nullable ConfigCommentInfo getComment(@Nullable String path);

public abstract @NotNull ConfigInitializer<? extends ConfigurationProvider<W>> getInitializer();

Expand Down
Loading

0 comments on commit 390815b

Please sign in to comment.