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

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

Snake yaml : Issue with duplicate keys in Yaml template

问题

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

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

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

  1. 假设以下是Yaml模板
  1. --------------------------------
  2. version: snapshot-01
  3. kind: sample
  4. metadata:
  5. name: abc
  6. options: "<placeholder>"
  7. --------------------------------

我将Yaml转换为Map,使用如下所示的SnakeYAML

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

我动态替换所需字段,如下所示。

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

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

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

生成的Yaml文件是:

  1. version: "v-1.0"
  2. kind: sample
  3. metadata:
  4. name: abc
  5. options: "newOptions"
  6. --------------------------------

我现在遇到了一些问题

模板需要按以下方式更改

  1. --------------------------------
  2. version: snapshot-01
  3. kind: sample
  4. metadata:
  5. name: abc
  6. options: "<placeholder>"
  7. ---
  8. version: v2
  9. kind: sample
  10. metadata:
  11. type: <abc>
  12. --------------------------------

我必须在模板中包含一些额外的内容,其中包括三个破折号,以及与version、kind和metadata相同的键

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

  1. version: "v-1.0"
  2. kind: sample
  3. metadata:
  4. name: abc
  5. options: "newOptions"
  6. ---
  7. version: v2-0
  8. kind: sample
  9. metadata:
  10. 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
  1. --------------------------------
  2. version: snapshot-01
  3. kind: sample
  4. metadata:
  5. name: abc
  6. options: &quot;&lt;placeholder&gt;&quot;
  7. --------------------------------

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

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

I am replacing the required fields dynamically as shown below.

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

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

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

Generated yaml file is :

  1. version: &quot;v-1.0&quot;
  2. kind: sample
  3. metadata:
  4. name: abc
  5. options: &quot;newOptions&quot;
  6. --------------------------------

I got some issue now

The template needs to be changed as below

  1. --------------------------------
  2. version: snapshot-01
  3. kind: sample
  4. metadata:
  5. name: abc
  6. options: &quot;&lt;placeholder&gt;&quot;
  7. ---
  8. version: v2
  9. kind: sample
  10. metadata:
  11. type: &lt;abc&gt;
  12. --------------------------------

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

  1. version: &quot;v-1.0&quot;
  2. kind: sample
  3. metadata:
  4. name: abc
  5. options: &quot;newOptions&quot;
  6. ---
  7. version: v2-0
  8. kind: sample
  9. metadata:
  10. 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编写一个包含多个文档的文件:

  1. List<Object> output = new ArrayList<Object>();
  2. boolean first = true;
  3. for (Map<String, Object> doc : yaml.loadAll(inputStream)) {
  4. if (first) {
  5. doc.put("version","v-1.0");
  6. doc.put("options","newOptions");
  7. first = false;
  8. }
  9. output.add(doc);
  10. }
  11. 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:

  1. List&lt;Object&gt; output = new ArrayList&lt;Object&gt;();
  2. boolean first = true;
  3. for (Map&lt;String, Object&gt; doc : yaml.loadAll(inputStream)) {
  4. if (first) {
  5. doc.put(&quot;version&quot;,&quot;v-1.0&quot;);
  6. doc.put(&quot;options&quot;,&quot;newOptions&quot;);
  7. first = false;
  8. }
  9. output.add(doc);
  10. }
  11. 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:

确定