diff --git a/core/pom.xml b/core/pom.xml index 36380d8..5ab246d 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 2.3.0 + 3.0.0 4.0.0 diff --git a/core/src/main/java/cc/carm/lib/configuration/core/ConfigInitializer.java b/core/src/main/java/cc/carm/lib/configuration/core/ConfigInitializer.java index f9c9ba7..7428d87 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/ConfigInitializer.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/ConfigInitializer.java @@ -1,14 +1,16 @@ package cc.carm.lib.configuration.core; -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.annotation.HeaderComment; +import cc.carm.lib.configuration.core.annotation.InlineComment; 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; import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.List; /** * 配置文件类初始化方法 @@ -45,7 +47,7 @@ public class ConfigInitializer> { public void initialize(@NotNull Class clazz, boolean saveDefaults, boolean loadSubClasses) { initializeClass( - clazz, null, + clazz, null, null, null, null, null, saveDefaults, loadSubClasses ); @@ -60,33 +62,29 @@ public class ConfigInitializer> { protected void initializeClass(@NotNull Class clazz, @Nullable String parentPath, @Nullable String fieldName, - @Nullable ConfigPath fieldPath, @Nullable ConfigComment filedComments, + @Nullable ConfigPath fieldPath, + @Nullable HeaderComment fieldHeaderComments, + @Nullable InlineComment fieldInlineComments, boolean saveDefaults, boolean loadSubClasses) { String path = getClassPath(clazz, parentPath, fieldName, fieldPath); - this.provider.setComment(path, getClassComments(clazz, filedComments)); + this.provider.setHeaderComment(path, getClassHeaderComments(clazz, fieldHeaderComments)); + if (path != null) this.provider.setInlineComment(path, readInlineComments(fieldInlineComments)); + for (Field field : clazz.getDeclaredFields()) { initializeField(clazz, field, path, saveDefaults, loadSubClasses); } + if (!loadSubClasses) return; Class[] classes = clazz.getDeclaredClasses(); - if (loadSubClasses && classes.length > 0) { - // 逆向加载,保持顺序。 - for (int i = classes.length - 1; i >= 0; i--) { - initializeClass( - classes[i], path, classes[i].getSimpleName(), - null, null, - saveDefaults, true - ); - } + for (int i = classes.length - 1; i >= 0; i--) { // 逆向加载,保持顺序。 + initializeClass( + classes[i], path, classes[i].getSimpleName(), + null, null, null, + saveDefaults, true + ); } } - - protected void initializeValue(@NotNull ConfigValue value, @NotNull String path, - @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 { @@ -95,14 +93,16 @@ public class ConfigInitializer> { if (object instanceof ConfigValue) { initializeValue( (ConfigValue) object, getFieldPath(field, parent), - ConfigCommentInfo.fromAnnotation(field.getAnnotation(ConfigComment.class)), + field.getAnnotation(HeaderComment.class), + field.getAnnotation(InlineComment.class), saveDefaults ); } else if (object instanceof Class) { initializeClass( (Class) object, parent, field.getName(), field.getAnnotation(ConfigPath.class), - field.getAnnotation(ConfigComment.class), + field.getAnnotation(HeaderComment.class), + field.getAnnotation(InlineComment.class), saveDefaults, loadSubClasses ); } @@ -110,15 +110,36 @@ public class ConfigInitializer> { } } - protected void setComments(@Nullable String path, @Nullable ConfigCommentInfo comments) { - if (comments != null) this.provider.setComment(path, comments); + protected void initializeValue(@NotNull ConfigValue value, @NotNull String path, + @Nullable HeaderComment fieldHeaderComment, + @Nullable InlineComment fieldInlineComment, + boolean saveDefaults) { + value.initialize( + provider, saveDefaults, path, + readHeaderComments(fieldHeaderComment), + readInlineComments(fieldInlineComment) + ); } - protected static @Nullable ConfigCommentInfo getClassComments(@NotNull Class clazz, - @Nullable ConfigComment fieldAnnotation) { - ConfigCommentInfo classComments = ConfigCommentInfo.fromAnnotation(clazz.getAnnotation(ConfigComment.class)); + protected static @Nullable List getClassHeaderComments(@NotNull Class clazz, + @Nullable HeaderComment fieldAnnotation) { + List classComments = readHeaderComments(clazz.getAnnotation(HeaderComment.class)); if (classComments != null) return classComments; - return ConfigCommentInfo.fromAnnotation(fieldAnnotation); + else return readHeaderComments(fieldAnnotation); + } + + + protected static List readHeaderComments(@Nullable HeaderComment annotation) { + if (annotation == null) return null; + String[] value = annotation.value(); + return value.length > 0 ? Arrays.asList(value) : null; + } + + + protected static @Nullable String readInlineComments(@Nullable InlineComment annotation) { + if (annotation == null) return null; + String value = annotation.value(); + return value.length() > 0 ? value : null; } protected static @Nullable String getClassPath(@NotNull Class clazz, diff --git a/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java b/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java deleted file mode 100644 index 5874024..0000000 --- a/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java +++ /dev/null @@ -1,45 +0,0 @@ -package cc.carm.lib.configuration.core.annotation; - -import org.jetbrains.annotations.NotNull; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.TYPE, ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface ConfigComment { - - @NotNull - String[] value() default ""; - - /** - * 首行换行,即会在注释开始前进行一次换行,与上方配置分离,优化观感。 - * 如: - *
-     * some-key: "SomeValue"
-     *
-     * # 注释第一行
-     * # 注释第二行
-     * startWrap: true
-     * 
- * - * @return 是否在结尾添加换行符 - */ - boolean startWrap() default true; - - /** - * 末尾换行,即会在注释结束后进行一次换行,如: - *
-     * # 注释第一行
-     * # 注释第二行
-     *
-     * endWrap: true
-     * 
- *

该功能可用于编写配置文件的顶部注释。 - * - * @return 是否在结尾添加换行符 - */ - boolean endWrap() default false; -} diff --git a/core/src/main/java/cc/carm/lib/configuration/core/annotation/HeaderComment.java b/core/src/main/java/cc/carm/lib/configuration/core/annotation/HeaderComment.java new file mode 100644 index 0000000..04702e2 --- /dev/null +++ b/core/src/main/java/cc/carm/lib/configuration/core/annotation/HeaderComment.java @@ -0,0 +1,39 @@ +package cc.carm.lib.configuration.core.annotation; + +import org.jetbrains.annotations.NotNull; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 顶部注释,用于给对应配置的顶部添加注释,便于使用者阅读查看。 + *

如: + *

+ * # 注释第一行
+ * # 注释第二行
+ * foo: "bar"
+ * 
+ */ +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface HeaderComment { + + /** + * 注释内容,若内容长度为0则会视为一个空行。 + *

{"foo","","bar"} + * 会被添加为 + *

+     * # foo
+     *
+     * # bar
+     * foo: "bar"
+     * 
+ * + * @return 注释内容 + */ + @NotNull + String[] value() default ""; + +} diff --git a/core/src/main/java/cc/carm/lib/configuration/core/annotation/InlineComment.java b/core/src/main/java/cc/carm/lib/configuration/core/annotation/InlineComment.java new file mode 100644 index 0000000..94a4ae6 --- /dev/null +++ b/core/src/main/java/cc/carm/lib/configuration/core/annotation/InlineComment.java @@ -0,0 +1,33 @@ +package cc.carm.lib.configuration.core.annotation; + +import org.jetbrains.annotations.NotNull; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 行内注释,用于给对应配置的所在行添加注释,便于使用者阅读查看。 + * 如: + *
+ * foo: "bar" # 注释内容
+ * 
+ */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface InlineComment { + + /** + * 注释内容,若内容长度为0则不会添加注释 + *

"foobar" 将被设定为 + *

+     * foo: "bar" # foobar
+     * 
+ * + * @return 注释内容 + */ + @NotNull + String value() default ""; + +} diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java index 0be89fd..0b368c4 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java @@ -1,11 +1,13 @@ 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; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; +import java.util.List; + public abstract class AbstractConfigBuilder, P extends ConfigurationProvider> { protected final Class providerClass; @@ -13,9 +15,8 @@ public abstract class AbstractConfigBuilder headerComments; + protected @Nullable String inlineComment; protected @Nullable T defaultValue; @@ -38,38 +39,26 @@ public abstract class AbstractConfigBuilder comments) { + this.headerComments = comments; return getThis(); } - public @NotNull B setStartWarp(boolean enable) { - this.startWrap = enable; + public @NotNull B inlineComment(@NotNull String comment) { + this.inlineComment = comment; 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; - } - - } diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/ConfigListBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/ConfigListBuilder.java index 68e539b..ab6d7ec 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/ConfigListBuilder.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/ConfigListBuilder.java @@ -1,8 +1,10 @@ package cc.carm.lib.configuration.core.builder.list; +import cc.carm.lib.configuration.core.annotation.InlineComment; import cc.carm.lib.configuration.core.function.ConfigDataFunction; import org.jetbrains.annotations.NotNull; + public class ConfigListBuilder { protected final @NotNull Class valueClass; diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java index 3a09cda..f9b96fc 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java @@ -65,7 +65,8 @@ public class SourceListBuilder extends CommonConfigBuilder, Source @Override public @NotNull ConfiguredList build() { return new ConfiguredList<>( - this.provider, this.path, this.buildComments(), + this.provider, this.path, + this.headerComments, this.inlineComment, this.valueClass, this.defaultValue, this.sourceParser.andThen(this.valueParser), this.valueSerializer.andThen(sourceSerializer) diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java index 3345c6d..8fdd916 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java @@ -89,7 +89,8 @@ public class SourceMapBuilder, S, K, V> extends CommonConfig @Override public @NotNull ConfiguredMap build() { return new ConfiguredMap<>( - this.provider, this.path,this.buildComments(), + this.provider, this.path, + this.headerComments, this.inlineComment, this.defaultValue, this.supplier, this.keyClass, this.keyParser, this.valueClass, this.sourceParser.andThen(this.valueParser), diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SectionValueBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SectionValueBuilder.java index 4366f73..6e7a0c3 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SectionValueBuilder.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SectionValueBuilder.java @@ -45,7 +45,8 @@ public class SectionValueBuilder @Override public @NotNull ConfiguredSection build() { return new ConfiguredSection<>( - this.provider, this.path,this.buildComments(), + this.provider, this.path, + this.headerComments, this.inlineComment, this.valueClass, this.defaultValue, this.parser, this.serializer ); diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SourceValueBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SourceValueBuilder.java index f52f0ee..5d6caea 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SourceValueBuilder.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/value/SourceValueBuilder.java @@ -3,7 +3,6 @@ package cc.carm.lib.configuration.core.builder.value; 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; @@ -58,7 +57,8 @@ public class SourceValueBuilder extends CommonConfigBuilder build() { return new ConfiguredValue<>( - this.provider, this.path, this.buildComments(), + this.provider, this.path, + this.headerComments, this.inlineComment, this.valueClass, this.defaultValue, this.valueParser.compose(this.sourceParser), this.valueSerializer.andThen(sourceSerializer) diff --git a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigCommentInfo.java b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigCommentInfo.java deleted file mode 100644 index 47818a3..0000000 --- a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigCommentInfo.java +++ /dev/null @@ -1,80 +0,0 @@ -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 + - '}'; - } -} diff --git a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationProvider.java b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationProvider.java index 66153e6..8ed0b53 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationProvider.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationProvider.java @@ -4,6 +4,9 @@ import cc.carm.lib.configuration.core.ConfigInitializer; import cc.carm.lib.configuration.core.ConfigurationRoot; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Unmodifiable; + +import java.util.List; public abstract class ConfigurationProvider { @@ -27,9 +30,15 @@ public abstract class ConfigurationProvider { public abstract void save() throws Exception; - public abstract void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment); + public abstract void setHeaderComment(@Nullable String path, @Nullable List comments); - public abstract @Nullable ConfigCommentInfo getComment(@Nullable String path); + public abstract void setInlineComment(@NotNull String path, @Nullable String comment); + + @Nullable + @Unmodifiable + public abstract List getHeaderComment(@Nullable String path); + + public abstract @Nullable String getInlineComment(@NotNull String path); public abstract @NotNull ConfigInitializer> getInitializer(); diff --git a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationReader.java b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationReader.java index 2985e33..ba4deba 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationReader.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/source/ConfigurationReader.java @@ -4,6 +4,11 @@ import cc.carm.lib.configuration.core.function.ConfigValueParser; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Unmodifiable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; interface ConfigurationReader { @@ -26,7 +31,6 @@ interface ConfigurationReader { return getWrapper().isType(path, Byte.class); } - default @Nullable Byte getByte(@NotNull String path) { return getByte(path, (byte) 0); } @@ -36,12 +40,10 @@ interface ConfigurationReader { return getWrapper().get(path, def, ConfigValueParser.byteValue()); } - default boolean isShort(@NotNull String path) { return getWrapper().isType(path, Short.class); } - default @Nullable Short getShort(@NotNull String path) { return getShort(path, (short) 0); } @@ -56,7 +58,6 @@ interface ConfigurationReader { return getWrapper().isType(path, Integer.class); } - default @Nullable Integer getInt(@NotNull String path) { return getInt(path, 0); } @@ -71,7 +72,6 @@ interface ConfigurationReader { return getWrapper().isType(path, Long.class); } - default @Nullable Long getLong(@NotNull String path) { return getLong(path, 0L); } @@ -86,7 +86,6 @@ interface ConfigurationReader { return getWrapper().isType(path, Float.class); } - default @Nullable Float getFloat(@NotNull String path) { return getFloat(path, 0.0F); } @@ -101,7 +100,6 @@ interface ConfigurationReader { return getWrapper().isType(path, Double.class); } - default @Nullable Double getDouble(@NotNull String path) { return getDouble(path, 0.0D); } @@ -116,7 +114,6 @@ interface ConfigurationReader { return getWrapper().isType(path, Boolean.class); } - default @Nullable Character getChar(@NotNull String path) { return getChar(path, null); } @@ -140,5 +137,54 @@ interface ConfigurationReader { return getWrapper().get(path, def, String.class); } + @Unmodifiable + default @NotNull List getStringList(@NotNull String path) { + return parseList(getWrapper().getList(path), ConfigValueParser.castToString()); + } + + @Unmodifiable + default @NotNull List getIntegerList(@NotNull String path) { + return parseList(getWrapper().getList(path), ConfigValueParser.intValue()); + } + + @Unmodifiable + default @NotNull List getLongList(@NotNull String path) { + return parseList(getWrapper().getList(path), ConfigValueParser.longValue()); + } + + @Unmodifiable + default @NotNull List getDoubleList(@NotNull String path) { + return parseList(getWrapper().getList(path), ConfigValueParser.doubleValue()); + } + + @Unmodifiable + default @NotNull List getFloatList(@NotNull String path) { + return parseList(getWrapper().getList(path), ConfigValueParser.floatValue()); + } + + @Unmodifiable + default @NotNull List getByteList(@NotNull String path) { + return parseList(getWrapper().getList(path), ConfigValueParser.byteValue()); + } + + @Unmodifiable + default @NotNull List getCharList(@NotNull String path) { + return parseList(getWrapper().getList(path), ConfigValueParser.castObject(Character.class)); + } + + @Unmodifiable + static @NotNull List parseList(@Nullable List list, ConfigValueParser parser) { + if (list == null) return Collections.emptyList(); + List values = new ArrayList<>(); + for (Object o : list) { + try { + T parsed = parser.parse(o, null); + if (parsed != null) values.add(parsed); + } catch (Exception ignored) { + } + } + return values; + } + } diff --git a/core/src/main/java/cc/carm/lib/configuration/core/value/ConfigValue.java b/core/src/main/java/cc/carm/lib/configuration/core/value/ConfigValue.java index 426ca7d..02feae2 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/value/ConfigValue.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/value/ConfigValue.java @@ -1,12 +1,13 @@ package cc.carm.lib.configuration.core.value; import cc.carm.lib.configuration.core.builder.ConfigBuilder; -import cc.carm.lib.configuration.core.source.ConfigCommentInfo; import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Unmodifiable; +import java.util.List; import java.util.Objects; import java.util.Optional; @@ -18,26 +19,37 @@ public abstract class ConfigValue { protected @Nullable ConfigurationProvider provider; protected @Nullable String configPath; - protected @Nullable ConfigCommentInfo comments; + + protected @Nullable List headerComments; + protected @Nullable String inlineComments; protected @Nullable T defaultValue; - public ConfigValue(@Nullable ConfigurationProvider provider, - @Nullable String configPath, @Nullable ConfigCommentInfo comments, + public ConfigValue(@Nullable ConfigurationProvider provider, @Nullable String configPath, + @Nullable List headerComments, @Nullable String inlineComments, @Nullable T defaultValue) { this.provider = provider; this.configPath = configPath; - this.comments = comments; + this.headerComments = headerComments; + this.inlineComments = inlineComments; this.defaultValue = defaultValue; } - public void initialize(@NotNull ConfigurationProvider provider, boolean saveDefault, - @NotNull String configPath, @Nullable ConfigCommentInfo comments) { + public void initialize(@NotNull ConfigurationProvider provider, boolean saveDefault, @NotNull String configPath, + @Nullable List headerComments, @Nullable String inlineComments) { if (this.provider == null) this.provider = provider; if (this.configPath == null) this.configPath = configPath; - if (this.comments == null) this.comments = comments; + if (this.headerComments == null) this.headerComments = headerComments; + if (this.inlineComments == null) this.inlineComments = inlineComments; if (saveDefault) setDefault(); - if (getComments() != null) this.provider.setComment(getConfigPath(), getComments()); + + if (getHeaderComments() != null) { + this.provider.setHeaderComment(getConfigPath(), getHeaderComments()); + } + if (getInlineComments() != null) { + this.provider.setInlineComment(getConfigPath(), getInlineComments()); + } + } public @Nullable T getDefaultValue() { @@ -145,8 +157,13 @@ public abstract class ConfigValue { getConfiguration().set(getConfigPath(), value); } - public @Nullable ConfigCommentInfo getComments() { - return comments; + public @Nullable String getInlineComments() { + return inlineComments; + } + + @Unmodifiable + public @Nullable List getHeaderComments() { + return headerComments; } } diff --git a/core/src/main/java/cc/carm/lib/configuration/core/value/impl/CachedConfigValue.java b/core/src/main/java/cc/carm/lib/configuration/core/value/impl/CachedConfigValue.java index 93a9b4a..eadcb08 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/value/impl/CachedConfigValue.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/value/impl/CachedConfigValue.java @@ -1,11 +1,11 @@ package cc.carm.lib.configuration.core.value.impl; -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; import org.jetbrains.annotations.Nullable; +import java.util.List; + public abstract class CachedConfigValue extends ConfigValue { @@ -13,8 +13,9 @@ public abstract class CachedConfigValue extends ConfigValue { protected long parsedTime = -1; public CachedConfigValue(@Nullable ConfigurationProvider provider, @Nullable String sectionPath, - @Nullable ConfigCommentInfo comments, @Nullable T defaultValue) { - super(provider, sectionPath, comments, defaultValue); + @Nullable List headerComments, @Nullable String inlineComments, + @Nullable T defaultValue) { + super(provider, sectionPath, headerComments, inlineComments, defaultValue); } protected T updateCache(T value) { diff --git a/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredList.java b/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredList.java index 9eb95b2..79471b0 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredList.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredList.java @@ -1,8 +1,8 @@ package cc.carm.lib.configuration.core.value.type; +import cc.carm.lib.configuration.core.annotation.HeaderComment; import cc.carm.lib.configuration.core.builder.list.ConfigListBuilder; import cc.carm.lib.configuration.core.function.ConfigDataFunction; -import cc.carm.lib.configuration.core.source.ConfigCommentInfo; import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import org.jetbrains.annotations.NotNull; @@ -17,17 +17,18 @@ public class ConfiguredList extends CachedConfigValue> { return builder().asList(valueClass); } + @HeaderComment protected final @NotNull Class valueClass; protected final @NotNull ConfigDataFunction parser; protected final @NotNull ConfigDataFunction serializer; - public ConfiguredList(@Nullable ConfigurationProvider provider, - @Nullable String sectionPath, @Nullable ConfigCommentInfo comments, + public ConfiguredList(@Nullable ConfigurationProvider provider, @Nullable String sectionPath, + @Nullable List headerComments, @Nullable String inlineComments, @NotNull Class valueClass, @Nullable List defaultValue, @NotNull ConfigDataFunction parser, @NotNull ConfigDataFunction serializer) { - super(provider, sectionPath, comments, defaultValue); + super(provider, sectionPath, headerComments, inlineComments, defaultValue); this.valueClass = valueClass; this.parser = parser; this.serializer = serializer; diff --git a/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredMap.java b/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredMap.java index 0393dd2..9c93a8d 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredMap.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredMap.java @@ -2,7 +2,6 @@ package cc.carm.lib.configuration.core.value.type; import cc.carm.lib.configuration.core.builder.map.ConfigMapBuilder; import cc.carm.lib.configuration.core.function.ConfigDataFunction; -import cc.carm.lib.configuration.core.source.ConfigCommentInfo; import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; @@ -10,6 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Supplier; @@ -32,14 +32,14 @@ public class ConfiguredMap extends CachedConfigValue> { protected final @NotNull ConfigDataFunction keySerializer; protected final @NotNull ConfigDataFunction valueSerializer; - public ConfiguredMap(@Nullable ConfigurationProvider provider, - @Nullable String sectionPath, @Nullable ConfigCommentInfo comments, + public ConfiguredMap(@Nullable ConfigurationProvider provider, @Nullable String sectionPath, + @Nullable List headerComments, @Nullable String inlineComments, @Nullable Map defaultValue, @NotNull Supplier> supplier, @NotNull Class keyClass, @NotNull ConfigDataFunction keyParser, @NotNull Class valueClass, @NotNull ConfigDataFunction valueParser, @NotNull ConfigDataFunction keySerializer, @NotNull ConfigDataFunction valueSerializer) { - super(provider, sectionPath, comments, defaultValue); + super(provider, sectionPath, headerComments, inlineComments, defaultValue); this.supplier = supplier; this.keyClass = keyClass; this.valueClass = valueClass; diff --git a/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredSection.java b/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredSection.java index 9912e81..e92414d 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredSection.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredSection.java @@ -3,13 +3,13 @@ package cc.carm.lib.configuration.core.value.type; import cc.carm.lib.configuration.core.builder.value.SectionValueBuilder; 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.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.ConfigurationWrapper; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.Map; import java.util.Optional; @@ -24,12 +24,12 @@ public class ConfiguredSection extends CachedConfigValue { protected final @NotNull ConfigValueParser parser; protected final @NotNull ConfigDataFunction> serializer; - public ConfiguredSection(@Nullable ConfigurationProvider provider, - @Nullable String sectionPath, @Nullable ConfigCommentInfo comments, + public ConfiguredSection(@Nullable ConfigurationProvider provider, @Nullable String sectionPath, + @Nullable List headerComments, @Nullable String inlineComments, @NotNull Class valueClass, @Nullable V defaultValue, @NotNull ConfigValueParser parser, @NotNull ConfigDataFunction> serializer) { - super(provider, sectionPath, comments, defaultValue); + super(provider, sectionPath, headerComments, inlineComments, defaultValue); this.valueClass = valueClass; this.parser = parser; this.serializer = serializer; diff --git a/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredValue.java b/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredValue.java index 00b4ed1..f032a92 100644 --- a/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredValue.java +++ b/core/src/main/java/cc/carm/lib/configuration/core/value/type/ConfiguredValue.java @@ -3,12 +3,12 @@ package cc.carm.lib.configuration.core.value.type; import cc.carm.lib.configuration.core.builder.value.ConfigValueBuilder; 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.source.ConfigurationProvider; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.Optional; public class ConfiguredValue extends CachedConfigValue { @@ -30,13 +30,13 @@ public class ConfiguredValue extends CachedConfigValue { protected final @NotNull ConfigValueParser parser; protected final @NotNull ConfigDataFunction serializer; - public ConfiguredValue(@Nullable ConfigurationProvider provider, - @Nullable String sectionPath, @Nullable ConfigCommentInfo comments, + public ConfiguredValue(@Nullable ConfigurationProvider provider, @Nullable String sectionPath, + @Nullable List headerComments, @Nullable String inlineComments, @NotNull Class valueClass, @Nullable V defaultValue, @NotNull ConfigValueParser parser, @NotNull ConfigDataFunction serializer) { - super(provider, sectionPath, comments, defaultValue); + super(provider, sectionPath, headerComments, inlineComments, defaultValue); this.valueClass = valueClass; this.parser = parser; this.serializer = serializer; diff --git a/impl/json/pom.xml b/impl/json/pom.xml index bf39558..383e490 100644 --- a/impl/json/pom.xml +++ b/impl/json/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 2.3.0 + 3.0.0 ../../pom.xml 4.0.0 diff --git a/impl/json/src/main/java/cc/carm/lib/configuration/json/JSONConfigProvider.java b/impl/json/src/main/java/cc/carm/lib/configuration/json/JSONConfigProvider.java index f92a308..ea70ddf 100644 --- a/impl/json/src/main/java/cc/carm/lib/configuration/json/JSONConfigProvider.java +++ b/impl/json/src/main/java/cc/carm/lib/configuration/json/JSONConfigProvider.java @@ -1,7 +1,6 @@ package cc.carm.lib.configuration.json; import cc.carm.lib.configuration.core.ConfigInitializer; -import cc.carm.lib.configuration.core.source.ConfigCommentInfo; import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.source.impl.FileConfigProvider; import com.google.gson.Gson; @@ -9,10 +8,12 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonSerializer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Unmodifiable; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.LinkedHashMap; +import java.util.List; /** * Some code comes from BungeeCord's implementation of the JsonConfiguration. @@ -69,15 +70,24 @@ public class JSONConfigProvider extends FileConfigProvider { } @Override - public void setComment(@Nullable String path, @Nullable ConfigCommentInfo comment) { + public void setHeaderComment(@Nullable String path, @Nullable List comments) { // JSON doesn't support comments; } @Override - public @Nullable ConfigCommentInfo getComment(@Nullable String path) { + public void setInlineComment(@NotNull String path, @Nullable String comment) { + // JSON doesn't support comments; + } + + @Override + public @Nullable @Unmodifiable List getHeaderComment(@Nullable String path) { return null; } + @Override + public @Nullable String getInlineComment(@NotNull String path) { + return null; + } @Override public @NotNull ConfigInitializer> getInitializer() { diff --git a/impl/json/src/main/java/cc/carm/lib/configuration/json/JSONValue.java b/impl/json/src/main/java/cc/carm/lib/configuration/json/JSONValue.java deleted file mode 100644 index 112519f..0000000 --- a/impl/json/src/main/java/cc/carm/lib/configuration/json/JSONValue.java +++ /dev/null @@ -1,4 +0,0 @@ -package cc.carm.lib.configuration.json; - -public abstract class JSONValue { -} diff --git a/impl/json/src/test/java/config/source/DatabaseConfiguration.java b/impl/json/src/test/java/config/source/DatabaseConfiguration.java index f78d3ce..78027b5 100644 --- a/impl/json/src/test/java/config/source/DatabaseConfiguration.java +++ b/impl/json/src/test/java/config/source/DatabaseConfiguration.java @@ -1,12 +1,12 @@ package config.source; import cc.carm.lib.configuration.core.ConfigurationRoot; -import cc.carm.lib.configuration.core.annotation.ConfigComment; +import cc.carm.lib.configuration.core.annotation.HeaderComment; import cc.carm.lib.configuration.core.annotation.ConfigPath; import cc.carm.lib.configuration.core.value.ConfigValue; import cc.carm.lib.configuration.core.value.type.ConfiguredValue; -@ConfigComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"}) +@HeaderComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"}) public class DatabaseConfiguration extends ConfigurationRoot { @ConfigPath("driver") diff --git a/impl/yaml/pom.xml b/impl/yaml/pom.xml index 5f87f9f..b13d73e 100644 --- a/impl/yaml/pom.xml +++ b/impl/yaml/pom.xml @@ -5,7 +5,7 @@ easyconfiguration-parent cc.carm.lib - 2.3.0 + 3.0.0 ../../pom.xml 4.0.0 @@ -29,7 +29,7 @@ org.bspfsystems yamlconfiguration - 1.0.11 + 1.1.0 compile diff --git a/impl/yaml/src/main/java/cc/carm/lib/configuration/EasyConfiguration.java b/impl/yaml/src/main/java/cc/carm/lib/configuration/EasyConfiguration.java index 6105812..d69c5c4 100644 --- a/impl/yaml/src/main/java/cc/carm/lib/configuration/EasyConfiguration.java +++ b/impl/yaml/src/main/java/cc/carm/lib/configuration/EasyConfiguration.java @@ -30,5 +30,4 @@ public class EasyConfiguration { return from(new File(fileName), source); } - } diff --git a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLComments.java b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLComments.java index 0ef84c3..ddf1f86 100644 --- a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLComments.java +++ b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLComments.java @@ -1,17 +1,15 @@ package cc.carm.lib.configuration.yaml; -import cc.carm.lib.configuration.core.source.ConfigCommentInfo; import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection; import org.bspfsystems.yamlconfiguration.file.FileConfiguration; import org.bspfsystems.yamlconfiguration.file.YamlConfiguration; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Unmodifiable; import java.io.BufferedWriter; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.StringJoiner; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -19,38 +17,55 @@ import static cc.carm.lib.configuration.yaml.YAMLConfigProvider.SEPARATOR; public class YAMLComments { - Map comments = new HashMap<>(); + protected final @NotNull Map> headerComments = new HashMap<>(); + protected final @NotNull Map inlineComments = new HashMap<>(); - protected Map getComments() { - return comments; + protected @NotNull Map> getHeaderComments() { + return headerComments; } - public void set(@Nullable String path, @Nullable ConfigCommentInfo comments) { + protected @NotNull Map getInlineComments() { + return inlineComments; + } + + public void setHeaderComments(@Nullable String path, @Nullable List comments) { + if (comments == null) { - getComments().remove(path); + getHeaderComments().remove(path); } else { - getComments().put(path, comments); + getHeaderComments().put(path, comments); } } - public @NotNull ConfigCommentInfo get(@Nullable String path) { - return getComments().getOrDefault(path, ConfigCommentInfo.defaults()); + + public void setInlineComment(@NotNull String path, @Nullable String comment) { + if (comment == null) { + getInlineComments().remove(path); + } else { + getInlineComments().put(path, comment); + } } - public @Nullable String buildComments(@NotNull String indents, @Nullable String path) { - ConfigCommentInfo comments = get(path); - if (!String.join("", comments.getComments()).isEmpty()) { - String prefix = comments.startWrap() ? "\n" : ""; - String suffix = comments.endWrap() ? "\n" : ""; - StringJoiner joiner = new StringJoiner("\n", prefix, suffix); - for (String comment : comments.getComments()) { - if (comment.length() == 0) joiner.add(" "); - else joiner.add(indents + "# " + comment); - } - return joiner + "\n"; - } else { - return comments.startWrap() || comments.endWrap() ? "\n" : null; + @Nullable + @Unmodifiable + public List getHeaderComment(@Nullable String path) { + return Optional.ofNullable(getHeaderComments().get(path)).map(Collections::unmodifiableList).orElse(null); + } + + public @Nullable String getInlineComment(@NotNull String path) { + return getInlineComments().get(path); + } + + public @Nullable String buildHeaderComments(@Nullable String path, @NotNull String indents) { + List comments = getHeaderComment(path); + if (comments == null || comments.size() == 0) return null; + + StringJoiner joiner = new StringJoiner("\n"); + for (String comment : comments) { + if (comment.length() == 0) joiner.add(" "); + else joiner.add(indents + "# " + comment); } + return joiner + "\n"; } /** @@ -64,37 +79,56 @@ public class YAMLComments { public void writeComments(@NotNull YamlConfiguration source, @NotNull BufferedWriter writer) throws IOException { FileConfiguration temp = new YamlConfiguration(); // 该对象用于临时记录配置内容 - for (String fullKey : source.getKeys(true)) { - String indents = getIndents(fullKey); - String comment = buildComments(indents, fullKey); - if (comment != null) writer.write(comment); + String configHeader = buildHeaderComments(null, ""); + if (configHeader != null) writer.write(configHeader); + for (String fullKey : source.getKeys(true)) { Object currentValue = source.get(fullKey); + String indents = getIndents(fullKey); + String headerComments = buildHeaderComments(fullKey, indents); + String inlineComment = getInlineComment(fullKey); + + if (headerComments != null) writer.write(headerComments); + String[] splitFullKey = fullKey.split("[" + SEPARATOR + "]"); String trailingKey = splitFullKey[splitFullKey.length - 1]; if (currentValue instanceof ConfigurationSection) { + ConfigurationSection section = (ConfigurationSection) currentValue; writer.write(indents + trailingKey + ":"); - if (!((ConfigurationSection) currentValue).getKeys(false).isEmpty()) { + if (inlineComment != null && inlineComment.length() > 0) { + writer.write(" # " + inlineComment); + } + if (!section.getKeys(false).isEmpty()) { writer.write("\n"); } else { writer.write(" {}\n"); + if (indents.length() == 0) writer.write("\n"); } continue; } temp.set(trailingKey, currentValue); String yaml = temp.saveToString(); - yaml = yaml.substring(0, yaml.length() - 1).replace("\n", "\n" + indents); - String toWrite = indents + yaml + "\n"; temp.set(trailingKey, null); - writer.write(toWrite); - } + yaml = yaml.substring(0, yaml.length() - 1); - String endComment = buildComments("", null); - if (endComment != null) writer.write(endComment); + if (inlineComment != null && inlineComment.length() > 0) { + if (yaml.contains("\n")) { + // section为多行内容,需要 InlineComment 加在首行末尾 + String[] splitLine = yaml.split("\n", 2); + yaml = splitLine[0] + " # " + inlineComment + "\n" + splitLine[1]; + } else { + // 其他情况下就直接加载后面就好。 + yaml += " # " + inlineComment; + } + } + + writer.write(indents + yaml.replace("\n", "\n" + indents) + "\n"); + if (indents.length() == 0) writer.write("\n"); + } writer.close(); } diff --git a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLConfigProvider.java b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLConfigProvider.java index d321063..6484df2 100644 --- a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLConfigProvider.java +++ b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLConfigProvider.java @@ -1,11 +1,11 @@ package cc.carm.lib.configuration.yaml; import cc.carm.lib.configuration.core.ConfigInitializer; -import cc.carm.lib.configuration.core.source.ConfigCommentInfo; import cc.carm.lib.configuration.core.source.impl.FileConfigProvider; import org.bspfsystems.yamlconfiguration.file.YamlConfiguration; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Unmodifiable; import java.io.BufferedWriter; import java.io.File; @@ -13,6 +13,7 @@ import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; public class YAMLConfigProvider extends FileConfigProvider { @@ -45,7 +46,6 @@ public class YAMLConfigProvider extends FileConfigProvider { public void save() throws Exception { configuration.save(getFile()); - // tchristofferson/ConfigUpdater start StringWriter writer = new StringWriter(); this.comments.writeComments(configuration, new BufferedWriter(writer)); @@ -61,13 +61,25 @@ public class YAMLConfigProvider extends FileConfigProvider { } @Override - public void setComment(@Nullable String path, @Nullable ConfigCommentInfo comments) { - this.comments.set(path, comments); + public void setHeaderComment(@Nullable String path, @Nullable List comments) { + this.comments.setHeaderComments(path, comments); } @Override - public @Nullable ConfigCommentInfo getComment(@Nullable String path) { - return this.comments.get(path); + public void setInlineComment(@NotNull String path, @Nullable String comment) { + this.comments.setInlineComment(path, comment); + } + + @Override + @Nullable + @Unmodifiable + public List getHeaderComment(@Nullable String path) { + return this.comments.getHeaderComment(path); + } + + @Override + public @Nullable String getInlineComment(@NotNull String path) { + return this.comments.getInlineComment(path); } @Override diff --git a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLValue.java b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLValue.java index e2a1a1d..b742f17 100644 --- a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLValue.java +++ b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/YAMLValue.java @@ -1,22 +1,23 @@ package cc.carm.lib.configuration.yaml; -import cc.carm.lib.configuration.core.source.ConfigCommentInfo; import cc.carm.lib.configuration.core.source.ConfigurationProvider; import cc.carm.lib.configuration.core.value.impl.CachedConfigValue; import cc.carm.lib.configuration.yaml.builder.YAMLConfigBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; + public abstract class YAMLValue extends CachedConfigValue { public static @NotNull YAMLConfigBuilder builder() { return new YAMLConfigBuilder(); } - public YAMLValue(@Nullable YAMLConfigProvider provider, - @Nullable String configPath, @Nullable ConfigCommentInfo comments, + public YAMLValue(@Nullable YAMLConfigProvider provider, @Nullable String configPath, + @Nullable List headerComments, @Nullable String inlineComments, @Nullable T defaultValue) { - super(provider, configPath, comments, defaultValue); + super(provider, configPath, headerComments, inlineComments, defaultValue); } public YAMLConfigProvider getYAMLProvider() { diff --git a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/builder/serializable/SerializableBuilder.java b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/builder/serializable/SerializableBuilder.java index 11f9714..33f1090 100644 --- a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/builder/serializable/SerializableBuilder.java +++ b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/builder/serializable/SerializableBuilder.java @@ -21,7 +21,7 @@ public class SerializableBuilder @Override public @NotNull ConfiguredSerializable build() { - return new ConfiguredSerializable<>(this.provider, this.path, buildComments(), this.valueClass, this.defaultValue); + return new ConfiguredSerializable<>(this.provider, this.path, this.headerComments, this.inlineComment, this.valueClass, this.defaultValue); } diff --git a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/value/ConfiguredSerializable.java b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/value/ConfiguredSerializable.java index aa2d7f4..d4bb8eb 100644 --- a/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/value/ConfiguredSerializable.java +++ b/impl/yaml/src/main/java/cc/carm/lib/configuration/yaml/value/ConfiguredSerializable.java @@ -1,12 +1,12 @@ package cc.carm.lib.configuration.yaml.value; -import cc.carm.lib.configuration.core.source.ConfigCommentInfo; import cc.carm.lib.configuration.yaml.YAMLConfigProvider; import cc.carm.lib.configuration.yaml.YAMLValue; import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerializable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.Optional; public class ConfiguredSerializable extends YAMLValue { @@ -22,10 +22,10 @@ public class ConfiguredSerializable extends protected final @NotNull Class valueClass; - public ConfiguredSerializable(@Nullable YAMLConfigProvider provider, - @Nullable String configPath, @Nullable ConfigCommentInfo comments, + public ConfiguredSerializable(@Nullable YAMLConfigProvider provider, @Nullable String configPath, + @Nullable List headerComments, @Nullable String inlineComments, @NotNull Class valueClass, @Nullable T defaultValue) { - super(provider, configPath, comments, defaultValue); + super(provider, configPath, headerComments, inlineComments, defaultValue); this.valueClass = valueClass; } diff --git a/impl/yaml/src/test/java/config/source/DatabaseConfiguration.java b/impl/yaml/src/test/java/config/source/DatabaseConfiguration.java index 404f571..4a51600 100644 --- a/impl/yaml/src/test/java/config/source/DatabaseConfiguration.java +++ b/impl/yaml/src/test/java/config/source/DatabaseConfiguration.java @@ -1,35 +1,29 @@ package config.source; import cc.carm.lib.configuration.core.ConfigurationRoot; -import cc.carm.lib.configuration.core.annotation.ConfigComment; import cc.carm.lib.configuration.core.annotation.ConfigPath; +import cc.carm.lib.configuration.core.annotation.HeaderComment; import cc.carm.lib.configuration.core.value.ConfigValue; import cc.carm.lib.configuration.core.value.type.ConfiguredValue; -@ConfigComment({"数据库配置", " 用于提供数据库连接,进行数据库操作。"}) +@HeaderComment({"", "数据库配置", " 用于提供数据库连接,进行数据库操作。"}) public class DatabaseConfiguration extends ConfigurationRoot { @ConfigPath("driver") - @ConfigComment(value = { + @HeaderComment({ "数据库驱动配置,请根据数据库类型设置。", "- MySQL: com.mysql.cj.jdbc.Driver", "- MariaDB(推荐): org.mariadb.jdbc.Driver", - }, startWrap = false) + }) protected static final ConfigValue DRIVER_NAME = ConfiguredValue.of( String.class, "com.mysql.cj.jdbc.Driver" ); - @ConfigComment(startWrap = false) protected static final ConfigValue HOST = ConfiguredValue.of(String.class, "127.0.0.1"); - @ConfigComment(startWrap = false) protected static final ConfigValue PORT = ConfiguredValue.of(Integer.class, 3306); - @ConfigComment(startWrap = false) protected static final ConfigValue DATABASE = ConfiguredValue.of(String.class, "minecraft"); - @ConfigComment(startWrap = false) protected static final ConfigValue USERNAME = ConfiguredValue.of(String.class, "root"); - @ConfigComment(startWrap = false) protected static final ConfigValue PASSWORD = ConfiguredValue.of(String.class, "password"); - @ConfigComment(startWrap = false) protected static final ConfigValue EXTRA = ConfiguredValue.of(String.class, "?useSSL=false"); protected static String buildJDBC() { diff --git a/impl/yaml/src/test/java/config/source/DemoConfiguration.java b/impl/yaml/src/test/java/config/source/DemoConfiguration.java index ba07388..356c4d2 100644 --- a/impl/yaml/src/test/java/config/source/DemoConfiguration.java +++ b/impl/yaml/src/test/java/config/source/DemoConfiguration.java @@ -2,8 +2,9 @@ package config.source; import cc.carm.lib.configuration.core.ConfigInitializer; import cc.carm.lib.configuration.core.ConfigurationRoot; -import cc.carm.lib.configuration.core.annotation.ConfigComment; import cc.carm.lib.configuration.core.annotation.ConfigPath; +import cc.carm.lib.configuration.core.annotation.HeaderComment; +import cc.carm.lib.configuration.core.annotation.InlineComment; import cc.carm.lib.configuration.core.value.ConfigValue; import cc.carm.lib.configuration.core.value.type.ConfiguredList; import cc.carm.lib.configuration.core.value.type.ConfiguredMap; @@ -17,22 +18,20 @@ import java.util.Objects; import java.util.UUID; -@ConfigComment({"给根类添加的注释将显示在文件的末尾。"}) +@HeaderComment({ + "此处内容将显示在配置文件的最上方", + ""/*添加一个空行与其他配置的注释分割*/ +}) public class DemoConfiguration extends ConfigurationRoot { @ConfigPath(root = true) - @ConfigComment(value = { - "有时候,需要在配置文件最上面显示点东西,", - "此时就推荐添加一个可以用到但并不重要的参数到最上面", - "并给他添加对应的注释。" - }, startWrap = false, endWrap = true) protected static final ConfigValue VERSION = ConfiguredValue.of(Double.class, 1.0D); - // 支持通过 Class 变量标注子配置,一并注册。 // 注意: 若对应类也有注解,则优先使用类的注解。 @ConfigPath("impl-test") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。 - @ConfigComment("Something...") // 支持给子路径直接打注释 + @HeaderComment({"", "Something..."}) // 支持给子路径直接打注释 + @InlineComment("InlineComments for class path") public static final Class IMPL = ImplConfiguration.class; // 子配置文件 @@ -40,14 +39,15 @@ public class DemoConfiguration extends ConfigurationRoot { public static final Class DB_CONFIG = DatabaseConfiguration.class; @ConfigPath("user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。 - @ConfigComment({"Section类型数据测试"}) // 通过注解给配置添加注释。 + @HeaderComment({"", "Section类型数据测试"}) // 通过注解给配置添加注释。 + @InlineComment("Section数据也支持InlineComment注释") public static final ConfigValue MODEL_TEST = ConfiguredSection .builder(TestModel.class) .defaults(new TestModel("Carm", UUID.randomUUID())) .parseValue((section, defaultValue) -> TestModel.deserialize(section)) .serializeValue(TestModel::serialize).build(); - @ConfigComment({"[ID-UUID] 对照表", "", "用于测试Map类型的解析与序列化保存"}) + @HeaderComment({"", "[ID - UUID]对照表", "", "用于测试Map类型的解析与序列化保存"}) public static final ConfigValue> USERS = ConfiguredMap .builder(Integer.class, UUID.class).fromString() .parseKey(Integer::parseInt) @@ -62,6 +62,7 @@ public class DemoConfiguration extends ConfigurationRoot { public static class Sub extends ConfigurationRoot { @ConfigPath(value = "uuid-value", root = true) + @InlineComment("This is an inline comment") public static final ConfigValue UUID_CONFIG_VALUE = ConfiguredValue .builder(UUID.class).fromString() .parseValue((data, defaultValue) -> UUID.fromString(data)) diff --git a/impl/yaml/src/test/java/config/source/ImplConfiguration.java b/impl/yaml/src/test/java/config/source/ImplConfiguration.java index 0acdcf8..1000cde 100644 --- a/impl/yaml/src/test/java/config/source/ImplConfiguration.java +++ b/impl/yaml/src/test/java/config/source/ImplConfiguration.java @@ -10,7 +10,9 @@ import config.model.TestModel; @ConfigPath(root = true) public class ImplConfiguration extends ConfigurationRoot { - public static final ConfigValue TEST = ConfiguredSerializable.of(TestModel.class, TestModel.random()); + public static final ConfigValue TEST = ConfiguredSerializable.of( + TestModel.class, TestModel.random() + ); } diff --git a/pom.xml b/pom.xml index d045080..5039333 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ cc.carm.lib easyconfiguration-parent pom - 2.3.0 + 3.0.0 core impl/yaml