mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
fix(comment): Fixed wrong behavior of inline regex comments
This commit is contained in:
@@ -46,8 +46,6 @@ public interface DemoConfiguration extends Configuration {
|
||||
@ConfigPath("registered_users") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@HeaderComments({"Section类型数据测试"}) // 通过注解给配置添加注释。
|
||||
@InlineComment("默认地注释会加到Section的首行末尾") // 通过注解给配置添加注释。
|
||||
@InlineComment(value = "用户名(匹配注释)", regex = "name") // 通过注解给配置添加注释。
|
||||
@InlineComment(value = "信息", regex = "info.*") // 通过注解给配置添加注释。
|
||||
ConfiguredList<UserRecord> ALLOWLISTS = ConfiguredList.builderOf(UserRecord.class).fromSection()
|
||||
.parse(UserRecord::deserialize).serialize(UserRecord::serialize)
|
||||
.defaults(UserRecord.CARM).build();
|
||||
@@ -81,6 +79,7 @@ public interface DemoConfiguration extends Configuration {
|
||||
.build();
|
||||
|
||||
@HeaderComments({"内部类的内部类测试", "通过这种方式,您可以轻易实现多层次的配置文件结构"})
|
||||
@FooterComments({"-------------"})
|
||||
public interface That extends Configuration {
|
||||
|
||||
ConfiguredList<UUID> OPERATORS = ConfiguredList
|
||||
|
||||
@@ -18,6 +18,8 @@ public class RegistryConfig implements Configuration {
|
||||
|
||||
@ConfigPath("test.user") // 通过注解规定配置文件中的路径,若不进行注解则以变量名自动生成。
|
||||
@FooterComments({"12313213212"})
|
||||
@InlineComment(value = "用户名(匹配注释)", regex = "name") // 通过注解给配置添加注释。
|
||||
@InlineComment(value = "信息", regex = {"info.*", "info.game.*"}) // 通过注解给配置添加注释。
|
||||
public final ConfigValue<UserRecord> TEST_MODEL = ConfiguredValue.builderOf(UserRecord.class).fromSection()
|
||||
.defaults(new UserRecord("Carm", UUID.randomUUID()))
|
||||
.parse((holder, section) -> UserRecord.deserialize(section))
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ public @interface InlineComment {
|
||||
* If the regex is not empty, the comment will be added to
|
||||
* all sub paths if the regex matches the value.
|
||||
* If the regex is empty, the comment will be added to the current path.
|
||||
* <p> e.g. for section, set <b>{"^foo", "*", "bar"}</b> will be set like
|
||||
* <p> e.g. for section, set <b>"foo.*.bar"</b> will be set like
|
||||
* <blockquote><pre>
|
||||
* section:
|
||||
* foo:
|
||||
|
||||
+10
-10
@@ -192,34 +192,34 @@ public class YAMLSource extends FileConfigSource<MemorySection, Map<String, Obje
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getInlineComment(@NotNull String key) {
|
||||
String comment = getInlineComment(key, null);
|
||||
public @Nullable String getInlineComment(@NotNull String path) {
|
||||
String comment = getInlineComment(path, null);
|
||||
if (comment != null) return comment;
|
||||
|
||||
String sep = String.valueOf(separator());
|
||||
|
||||
// If the comment is not found, try to get the comment from the parent section
|
||||
String[] keys = key.split(sep);
|
||||
String[] keys = path.split(Pattern.quote(sep));
|
||||
if (keys.length == 1) return null;
|
||||
|
||||
// Try every possible parent key&child key combination
|
||||
for (int i = 1; i < keys.length; i++) {
|
||||
String parentKey = String.join(sep, Arrays.copyOfRange(keys, 0, i));
|
||||
String childKey = String.join(sep, Arrays.copyOfRange(keys, i, keys.length));
|
||||
comment = getInlineComment(childKey, parentKey);
|
||||
comment = getInlineComment(parentKey, childKey);
|
||||
if (comment != null) return comment;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public @Nullable String getInlineComment(@NotNull String key, @Nullable String sectionKey) {
|
||||
Map<String, String> pathComment = holder().metadata(key).get(CommentableMeta.INLINE);
|
||||
public @Nullable String getInlineComment(@NotNull String path, @Nullable String sectionKey) {
|
||||
Map<String, String> pathComment = holder().metadata(path).get(CommentableMeta.INLINE);
|
||||
if (pathComment == null || pathComment.isEmpty()) return null;
|
||||
if (sectionKey == null) return pathComment.get(null);
|
||||
|
||||
for (Map.Entry<String, String> entry : pathComment.entrySet()) {
|
||||
if (entry.getKey().equals(sectionKey)) return entry.getValue();
|
||||
Pattern pattern = Pattern.compile(entry.getKey().replace(".", "\\.").replace("*", ".*"));
|
||||
for (Map.Entry<String/*regex*/, String/*content*/> entry : pathComment.entrySet()) {
|
||||
if (entry.getKey() == null) continue;
|
||||
if (Objects.equals(entry.getKey(), sectionKey)) return entry.getValue();
|
||||
Pattern pattern = Pattern.compile(entry.getKey().replace(".", "\\.").replace("*", "(.*)"));
|
||||
if (pattern.matcher(sectionKey).matches()) return entry.getValue();
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -4,17 +4,19 @@ import cc.carm.lib.configuration.commentable.CommentableMeta;
|
||||
import cc.carm.lib.configuration.demo.tests.ConfigurationTest;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.yaml.YAMLConfigFactory;
|
||||
import cc.carm.lib.configuration.source.yaml.YAMLSource;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class YamlTests {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ConfigurationHolder<?> holder = YAMLConfigFactory.from("target/tests.yml")
|
||||
ConfigurationHolder<YAMLSource> holder = YAMLConfigFactory.from("target/tests.yml")
|
||||
.resourcePath("configs/sample.yml").build();
|
||||
|
||||
ConfigurationTest.testDemo(holder);
|
||||
|
||||
Reference in New Issue
Block a user