如何使用 java 解析 yaml 注释?

How can I parse yaml comments using java?

我想在我的项目中使用一个yml 配置文件。我正在使用 jackson-dataformat-yaml 来解析 yml 文件。但我也需要解析 yml 注释。我在 python using ruamel yaml 中使用了类似的方法。我怎样才能在 java 中做同样的事情?

更新

为了什么?好吧,我想通过使用命令行参数来覆盖我的配置选项。因此,要为每个选项生成描述消息,我想使用我的评论。像这样:

在我的config.yml

# Define a source directory
src: '/foo/bar'

# Define a destination directory
dst: '/foo/baz'

因此,当您 运行 您的程序带有 --help 标志时,您将看到以下输出:

Your program can be ran with the following options: 
--src   Define a source directory
--dst   Define a destination directory

这种模型的主要好处是您永远不需要重复相同的语句两次,因为它们可以从配置文件中检索。

基本上,你有三层数据:

  • 您的配置架构。这定义了要在配置文件中定义的值。
  • 配置文件本身,它描述了当前机器上的常用配置。
  • 一次性开关,覆盖通常的配置。

每个值的作用的描述属于架构,而不属于配置文件本身。想一想:如果有人在他们的机器上编辑配置文件并更改注释,您的帮助输出会突然显示不同的描述。

我的建议是将描述添加到架构中。架构是您将 YAML 加载到的 Java class。我不确定你为什么使用 Jackson,因为它使用 SnakeYaml 作为解析器并且 SnakeYaml 完全能够反序列化为 Java classes,但有更多的配置选项,因为它不会泛化 [=37] =] 像杰克逊那样的YAML。

以下是如何使用 SnakeYaml 执行此操作的一般思路(注意,未经测试):

// ConfigParam.java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ConfigParam { String description(); }

// Configuration.java
public class Configuration {
    @ConfigParam("Define a source directory")
    String src;
    @ConfigParam("Define a destination directory")
    String dst;
}

// loading code
Yaml yaml = new Yaml(new Constructor(Configuration.class));
Configuration config = yaml.loadAs(input, Configuration.class);

// help generation code
System.out.println("Your program can be ran with the following options:")
for (Field field: Configuration.class.getFields()) {
    ConfigParam ann = field.getAnnotation(ConfigParam.class);
    if (ann != null) {
        System.out.println(String.format("--%s  %s", field.getName(), ann.description());
    }
}

为了将实际参数映射到配置,您还可以遍历 class 字段并将参数映射到字段名称 after 加载配置(替换给定值的标准值)。