Snake yaml:Yaml 模板中存在重复键的问题 [英] Snake yaml : Issue with duplicate keys in Yaml template

查看:42
本文介绍了Snake yaml:Yaml 模板中存在重复键的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要动态更新的 Yaml 模板.

I have a Yaml template which needs to be updated dynamically.

我正在使用蛇 yaml 读取 Yaml 模板并使用动态内容更新它并使用新值生成新的 yaml 文件

I am reading Yaml template using snake yaml and updating it with dynamic content and generating new yaml file with new values

我正在按照以下步骤更新 yaml 文件.

I am following below steps to update yaml file.

  1. 假设下面是 Yaml 模板

--------------------------------
version: snapshot-01
kind: sample
metadata:
  name: abc
options: "<placeholder>"
--------------------------------

我正在使用snake yaml将yaml转换为Map,如下所示

I am converting yaml into Map using snake yaml as shown below

 Yaml yaml = new Yaml();
 InputStream inputStream = 
 this.getClass().getClassLoader().getResourceAsStream(yamlTemplateLocation);
 Map<String, Object>yamlMap = yaml.load(inputStream); 

我正在动态替换必填字段,如下所示.

I am replacing the required fields dynamically as shown below.

yamlMap.put("version","v-1.0");
yamlMap.put("options","newOptions");

最后,我使用以下代码将地图转换为字符串并作为 Yaml 文件存储:

And finally I am converting map to String and strore as Yaml file using below code :

DumperOptions options = new DumperOptions();
options.setSplitLines(false);
Yaml yaml = new Yaml(options);
System.out.println(yaml.dump(yamlMap));

生成的yaml文件是:

Generated yaml file is :

version: "v-1.0"
kind: sample
metadata:
  name: abc
options:  "newOptions"
--------------------------------


我现在遇到了一些问题


I got some issue now

模板需要修改如下

--------------------------------
version: snapshot-01
kind: sample
metadata:
  name: abc
options: "<placeholder>"
---
version: v2
kind: sample
metadata:
 type: <abc> 
--------------------------------

我必须在模板中包含一些额外的部分,其中包括三个破折号以及相同的键,如版本、种类和元数据

I have to include some extra piece in the template which includes three dashes and also same same keys like version, kind and metadata

现在我需要使用新值更新模板,如下所示

Now I need to update template with new values as shown below

version: "v-1.0"
kind: sample
metadata:
  name: abc
options:  "newOptions"
---
version: v2-0
kind: sample
metadata:
 type: "newType"

我的问题是——>我正在将 yaml 转换为映射以进行更新.那么,如果上例中的 yaml 中存在重复键(如版本、版本),我该如何处理.

My question is --> I am converting yaml to map to update. So how can I handle if there are duplicate keys in yaml (like version, version) in the above example.

有人可以帮我解决这个问题吗?提前致谢!

Could anyone please help me with this? Thanks in advance!

推荐答案

在这种情况下,三个破折号标记 YAML 文档的结束和新文档的开始,这意味着单个文件中有多个 YAML 文档.在这种情况下,您需要使用 loadAll 加载所有文档,然后使用 dumpAll 编写包含多个文档的文件:

Three dashes mark the end of the YAML document and the begin of a new document in this case, meaning you have multiple YAML documents in a single file. In that case, you need to use loadAll to load all documents, and then dumpAll to write a file with multiple documents:

List<Object> output = new ArrayList<Object>();
boolean first = true;
for (Map<String, Object> doc : yaml.loadAll(inputStream)) {
  if (first) {
    doc.put("version","v-1.0");
    doc.put("options","newOptions");
    first = false;
  }
  output.add(doc);
}
System.out.println(yaml.dumpAll(output));

重复键不会有问题,因为它们位于不同的文档中.

You won't have problems with duplicate keys because they are in different documents.

这篇关于Snake yaml:Yaml 模板中存在重复键的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆