-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.go
54 lines (45 loc) · 1.25 KB
/
usage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"github.com/totherme/unstructured"
"gopkg.in/yaml.v3"
)
func main() {
myYaml := `
top-level-list:
- "this first element is a string - perhaps containing metadata"
- name: first real element
type: element-type-1
payload: [1,2,3,4]
- name: second real element
type: element-type-2
payload:
some: embedded structure
`
myData, err := unstructured.ParseYAML(myYaml)
if err != nil {
panic("Couldn't parse my own yaml")
}
myPayloadData, err := myData.GetByPointer("/top-level-list/2/payload/some")
if err != nil {
panic("Couldn't address into my own yaml")
}
myPayloadValue, err := myPayloadData.StringValue()
if err != nil {
panic("I really expected myPayloadData to represent a string...")
}
fmt.Println(myPayloadValue)
myPayloadMap, err := myData.GetByPointer("/top-level-list/2/payload")
if err != nil {
panic("Couldn't address into my own yaml")
}
err = myPayloadMap.SetField("additional-key", []string{"some", "arbitrary", "data"})
if err != nil {
panic("I relly expected myPayloadMap to be an Object that I could write fields into")
}
outputYaml, err := yaml.Marshal(myData.RawValue())
if err != nil {
panic("myData should definitely still be serializable")
}
fmt.Println(string(outputYaml))
}