Debian Codegraph @

hitch/story/merge-documents.story

raw
1 Merge YAML documents:
2 docs: howto/merge-yaml-documents
3 based on: strictyaml
4 description: |
5 Loaded YAML can be combined and dumped with the comments intact.
6 given:
7 yaml_snippet_1: |
8 # Some comment
9
10 a: â # value comment
11
12 # Another comment
13 b:
14 x: 4
15 y: 5
16 c:
17 - a: 1
18 - b: 2
19 yaml_snippet_2: |
20 x: 8
21
22 # y is now 9
23 y: 9
24 setup: |
25 from strictyaml import Map, MapPattern, Str, Seq, Int, load
26
27 schema_1 = Map({
28 "a": Str(),
29 "b": Map({"x": Int(), "y": Int()}),
30 "c": Seq(MapPattern(Str(), Str())),
31 })
32
33 schema_2 = Map({"x": Int(), "y": Int()})
34
35 yaml_1 = load(yaml_snippet_1, schema_1)
36 yaml_2 = load(yaml_snippet_2, schema_2)
37
38 yaml_1['b'] = yaml_2
39 steps:
40 - Run:
41 code: print(yaml_1.as_yaml())
42 will output: |-
43 # Some comment
44
45 a: â # value comment
46
47 # Another comment
48 b:
49 x: 8
50
51 # y is now 9
52 y: 9
53 c:
54 - a: 1
55 - b: 2