英文:
Snake yaml : Issue with duplicate keys in Yaml template
问题
我有一个需要动态更新的Yaml模板。
我正在使用SnakeYAML读取Yaml模板,并使用动态内容进行更新,然后生成带有新值的新Yaml文件。
我按照以下步骤更新Yaml文件。
- 假设以下是Yaml模板
--------------------------------
version: snapshot-01
kind: sample
metadata:
name: abc
options: "<placeholder>"
--------------------------------
我将Yaml转换为Map,使用如下所示的SnakeYAML
Yaml yaml = new Yaml();
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream(yamlTemplateLocation);
Map<String, Object> yamlMap = yaml.load(inputStream);
我动态替换所需字段,如下所示。
yamlMap.put("version","v-1.0");
yamlMap.put("options","newOptions");
最后,我将Map转换为字符串,并使用以下代码将其存储为Yaml文件:
DumperOptions options = new DumperOptions();
options.setSplitLines(false);
Yaml yaml = new Yaml(options);
System.out.println(yaml.dump(yamlMap));
生成的Yaml文件是:
version: "v-1.0"
kind: sample
metadata:
name: abc
options: "newOptions"
--------------------------------
我现在遇到了一些问题
模板需要按以下方式更改
--------------------------------
version: snapshot-01
kind: sample
metadata:
name: abc
options: "<placeholder>"
---
version: v2
kind: sample
metadata:
type: <abc>
--------------------------------
我必须在模板中包含一些额外的内容,其中包括三个破折号,以及与version、kind和metadata相同的键
现在我需要使用如下所示的新值更新模板
version: "v-1.0"
kind: sample
metadata:
name: abc
options: "newOptions"
---
version: v2-0
kind: sample
metadata:
type: "newType"
我的问题是 -> 我正在将Yaml转换为Map进行更新。那么如果Yaml中存在重复的键(例如version、version),我该如何处理?能否有人在这方面帮助我?提前谢谢!
英文:
I have a Yaml template which needs to be updated dynamically.
I am reading Yaml template using snake yaml and updating it with dynamic content and generating new yaml file with new values
I am following below steps to update yaml file.
- Assume below is the Yaml template
--------------------------------
version: snapshot-01
kind: sample
metadata:
name: abc
options: "<placeholder>"
--------------------------------
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");
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));
Generated yaml file is :
version: "v-1.0"
kind: sample
metadata:
name: abc
options: "newOptions"
--------------------------------
I got some issue now
The template needs to be changed as below
--------------------------------
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"
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!
答案1
得分: 1
三个短横线标志着YAML文档的结尾,在这种情况下标志着新文档的开始,这意味着你在单个文件中有多个YAML文档。在这种情况下,你需要使用loadAll
加载所有文档,然后使用dumpAll
编写一个包含多个文档的文件:
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));
你不会遇到重复键的问题,因为它们位于不同的文档中。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论