From c60ba074d9568f34b65fab9d917b9dc5c96c93e8 Mon Sep 17 00:00:00 2001 From: LSeng Date: Sun, 16 Feb 2025 21:59:03 +0800 Subject: [PATCH] fix(comment): Fixed wrong behavior of inline regex comments --- .../demo/tests/conf/DemoConfiguration.java | 3 +-- .../demo/tests/conf/RegistryConfig.java | 2 ++ .../annotation/InlineComment.java | 2 +- .../configuration/source/yaml/YAMLSource.java | 20 +++++++++---------- .../yaml/src/test/java/sample/SampleTest.java | 2 +- .../src/test/java/yaml/test/YamlTests.java | 4 +++- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/demo/src/main/java/cc/carm/lib/configuration/demo/tests/conf/DemoConfiguration.java b/demo/src/main/java/cc/carm/lib/configuration/demo/tests/conf/DemoConfiguration.java index 72f278b..6c09159 100644 --- a/demo/src/main/java/cc/carm/lib/configuration/demo/tests/conf/DemoConfiguration.java +++ b/demo/src/main/java/cc/carm/lib/configuration/demo/tests/conf/DemoConfiguration.java @@ -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 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 OPERATORS = ConfiguredList diff --git a/demo/src/main/java/cc/carm/lib/configuration/demo/tests/conf/RegistryConfig.java b/demo/src/main/java/cc/carm/lib/configuration/demo/tests/conf/RegistryConfig.java index 48c0df7..4a44d48 100644 --- a/demo/src/main/java/cc/carm/lib/configuration/demo/tests/conf/RegistryConfig.java +++ b/demo/src/main/java/cc/carm/lib/configuration/demo/tests/conf/RegistryConfig.java @@ -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 TEST_MODEL = ConfiguredValue.builderOf(UserRecord.class).fromSection() .defaults(new UserRecord("Carm", UUID.randomUUID())) .parse((holder, section) -> UserRecord.deserialize(section)) diff --git a/features/commentable/src/main/java/cc/carm/lib/configuration/annotation/InlineComment.java b/features/commentable/src/main/java/cc/carm/lib/configuration/annotation/InlineComment.java index 45c51e7..93a59cf 100644 --- a/features/commentable/src/main/java/cc/carm/lib/configuration/annotation/InlineComment.java +++ b/features/commentable/src/main/java/cc/carm/lib/configuration/annotation/InlineComment.java @@ -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. - *

e.g. for section, set {"^foo", "*", "bar"} will be set like + *

e.g. for section, set "foo.*.bar" will be set like *

      *   section:
      *     foo:
diff --git a/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLSource.java b/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLSource.java
index 3ae6e71..543f6b9 100644
--- a/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLSource.java
+++ b/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLSource.java
@@ -192,34 +192,34 @@ public class YAMLSource extends FileConfigSource pathComment = holder().metadata(key).get(CommentableMeta.INLINE);
+    public @Nullable String getInlineComment(@NotNull String path, @Nullable String sectionKey) {
+        Map pathComment = holder().metadata(path).get(CommentableMeta.INLINE);
         if (pathComment == null || pathComment.isEmpty()) return null;
         if (sectionKey == null) return pathComment.get(null);
-
-        for (Map.Entry entry : pathComment.entrySet()) {
-            if (entry.getKey().equals(sectionKey)) return entry.getValue();
-            Pattern pattern = Pattern.compile(entry.getKey().replace(".", "\\.").replace("*", ".*"));
+        for (Map.Entry 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;
diff --git a/providers/yaml/src/test/java/sample/SampleTest.java b/providers/yaml/src/test/java/sample/SampleTest.java
index dbf4ffa..2ddaaeb 100644
--- a/providers/yaml/src/test/java/sample/SampleTest.java
+++ b/providers/yaml/src/test/java/sample/SampleTest.java
@@ -4,7 +4,7 @@ import cc.carm.lib.configuration.source.ConfigurationHolder;
 import cc.carm.lib.configuration.source.yaml.YAMLConfigFactory;
 import org.junit.Test;
 
-public class SampleTest {
+public class  SampleTest {
 
     @Test
     public void test() {
diff --git a/providers/yaml/src/test/java/yaml/test/YamlTests.java b/providers/yaml/src/test/java/yaml/test/YamlTests.java
index 0d1e892..b140e5b 100644
--- a/providers/yaml/src/test/java/yaml/test/YamlTests.java
+++ b/providers/yaml/src/test/java/yaml/test/YamlTests.java
@@ -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 holder = YAMLConfigFactory.from("target/tests.yml")
                 .resourcePath("configs/sample.yml").build();
 
         ConfigurationTest.testDemo(holder);