Snake YAML: 在YAML模板中出现重复键的问题

huangapple go评论60阅读模式
英文:

Snake yaml : Issue with duplicate keys in Yaml template

问题

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

我正在使用SnakeYAML读取Yaml模板,并使用动态内容进行更新,然后生成带有新值的新Yaml文件。

我按照以下步骤更新Yaml文件。

  1. 假设以下是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.

  1. Assume below is the Yaml template
--------------------------------
version: snapshot-01
kind: sample
metadata:
  name: abc
options: &quot;&lt;placeholder&gt;&quot;
--------------------------------

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

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

I am replacing the required fields dynamically as shown below.

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

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: &quot;v-1.0&quot;
kind: sample
metadata:
  name: abc
options:  &quot;newOptions&quot;
--------------------------------

I got some issue now

The template needs to be changed as below

--------------------------------
version: snapshot-01
kind: sample
metadata:
  name: abc
options: &quot;&lt;placeholder&gt;&quot;
---
version: v2
kind: sample
metadata:
 type: &lt;abc&gt; 
--------------------------------

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: &quot;v-1.0&quot;
kind: sample
metadata:
  name: abc
options:  &quot;newOptions&quot;
---
version: v2-0
kind: sample
metadata:
 type: &quot;newType&quot;

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&lt;Object&gt; output = new ArrayList&lt;Object&gt;();
boolean first = true;
for (Map&lt;String, Object&gt; doc : yaml.loadAll(inputStream)) {
  if (first) {
    doc.put(&quot;version&quot;,&quot;v-1.0&quot;);
    doc.put(&quot;options&quot;,&quot;newOptions&quot;);
    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.

huangapple
  • 本文由 发表于 2020年9月18日 03:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63944965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定