1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 18:48:20 +08:00

fix(comment): Fixed wrong behavior of inline regex comments

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