From 9f1fc5bf90d726177007b9cbe74705bbcc59e842 Mon Sep 17 00:00:00 2001 From: carm Date: Mon, 17 Feb 2025 00:11:56 +0800 Subject: [PATCH] refactor(comment): Extract common comment functions --- .../commentable/Commentable.java | 62 +++++++++++++++++++ .../CommentableOptions.java | 2 +- .../source/yaml/YAMLConfigFactory.java | 3 +- .../configuration/source/yaml/YAMLSource.java | 46 +++----------- 4 files changed, 74 insertions(+), 39 deletions(-) create mode 100644 features/commentable/src/main/java/cc/carm/lib/configuration/commentable/Commentable.java rename features/commentable/src/main/java/cc/carm/lib/configuration/{option => commentable}/CommentableOptions.java (96%) diff --git a/features/commentable/src/main/java/cc/carm/lib/configuration/commentable/Commentable.java b/features/commentable/src/main/java/cc/carm/lib/configuration/commentable/Commentable.java new file mode 100644 index 0000000..37928a8 --- /dev/null +++ b/features/commentable/src/main/java/cc/carm/lib/configuration/commentable/Commentable.java @@ -0,0 +1,62 @@ +package cc.carm.lib.configuration.commentable; + +import cc.carm.lib.configuration.source.ConfigurationHolder; +import cc.carm.lib.configuration.source.loader.ConfigurationInitializer; +import cc.carm.lib.configuration.source.option.StandardOptions; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.regex.Pattern; + +public class Commentable { + + public static void registerMeta(@NotNull ConfigurationInitializer initializer) { + CommentableMeta.register(initializer); + } + + public static @Nullable String getInlineComment(@NotNull ConfigurationHolder holder, @NotNull String path) { + String comment = getInlineComment(holder, path, null); + if (comment != null) return comment; + + String sep = String.valueOf(holder.options().get(StandardOptions.PATH_SEPARATOR)); + + // If the comment is not found, try to get the comment from the parent section + 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(holder, parentKey, childKey); + if (comment != null) return comment; + } + return null; + } + + public static @Nullable String getInlineComment(@NotNull ConfigurationHolder holder, @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() == 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; + } + + public static @Nullable List getHeaderComments(@NotNull ConfigurationHolder holder, @Nullable String path) { + return holder.metadata(path).get(CommentableMeta.HEADER); + } + + public static @Nullable List getFooterComments(@NotNull ConfigurationHolder holder, @Nullable String path) { + return holder.metadata(path).get(CommentableMeta.FOOTER); + } + +} diff --git a/features/commentable/src/main/java/cc/carm/lib/configuration/option/CommentableOptions.java b/features/commentable/src/main/java/cc/carm/lib/configuration/commentable/CommentableOptions.java similarity index 96% rename from features/commentable/src/main/java/cc/carm/lib/configuration/option/CommentableOptions.java rename to features/commentable/src/main/java/cc/carm/lib/configuration/commentable/CommentableOptions.java index 94610ea..d58aa3e 100644 --- a/features/commentable/src/main/java/cc/carm/lib/configuration/option/CommentableOptions.java +++ b/features/commentable/src/main/java/cc/carm/lib/configuration/commentable/CommentableOptions.java @@ -1,4 +1,4 @@ -package cc.carm.lib.configuration.option; +package cc.carm.lib.configuration.commentable; import cc.carm.lib.configuration.source.option.ConfigurationOption; diff --git a/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLConfigFactory.java b/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLConfigFactory.java index 2cc8e09..69894c0 100644 --- a/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLConfigFactory.java +++ b/providers/yaml/src/main/java/cc/carm/lib/configuration/source/yaml/YAMLConfigFactory.java @@ -1,5 +1,6 @@ package cc.carm.lib.configuration.source.yaml; +import cc.carm.lib.configuration.commentable.Commentable; import cc.carm.lib.configuration.commentable.CommentableMeta; import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.source.file.FileConfigFactory; @@ -125,7 +126,7 @@ public class YAMLConfigFactory extends FileConfigFactory(this.adapters, this.options, this.metadata, this.initializer) { final @NotNull YAMLSource source = new YAMLSource(this, configFile, sourcePath); 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 543f6b9..0df42c1 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 @@ -1,7 +1,7 @@ package cc.carm.lib.configuration.source.yaml; -import cc.carm.lib.configuration.commentable.CommentableMeta; -import cc.carm.lib.configuration.option.CommentableOptions; +import cc.carm.lib.configuration.commentable.Commentable; +import cc.carm.lib.configuration.commentable.CommentableOptions; import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.source.file.FileConfigSource; import cc.carm.lib.configuration.source.option.StandardOptions; @@ -25,9 +25,10 @@ import java.io.Reader; import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.*; -import java.util.regex.Pattern; -public class YAMLSource extends FileConfigSource, YAMLSource> implements CommentedSection { +public class YAMLSource + extends FileConfigSource, YAMLSource> + implements CommentedSection { protected final @NotNull YamlConstructor yamlConstructor; protected final @NotNull YamlRepresenter yamlRepresenter; @@ -192,47 +193,18 @@ public class YAMLSource extends FileConfigSource 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() == 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; + public @Nullable String getInlineComment(@NotNull String key) { + return Commentable.getInlineComment(holder(), key); } @Override public @Nullable List getHeaderComments(@Nullable String key) { - return holder().metadata(key).get(CommentableMeta.HEADER); + return Commentable.getHeaderComments(holder(), key); } @Override public @Nullable List getFooterComments(@Nullable String key) { - return holder().metadata(key).get(CommentableMeta.FOOTER); + return Commentable.getFooterComments(holder(), key); } public static class YamlRepresenter extends Representer {