Debian Codegraph @

example/loader_dumper_demo/main.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Example: Loader Dumper Demo combines loading and dumping operations.
5
6 package main
7
8 import (
9 "bytes"
10 "fmt"
11 "io"
12 "strings"
13
14 "go.yaml.in/yaml/v4"
15 )
16
17 type Config struct {
18 Name string `yaml:"name"`
19 Version string `yaml:"version"`
20 Tags []string `yaml:"tags,omitempty"`
21 }
22
23 func main() {
24 fmt.Println("=== YAML Loader/Dumper Demo ===")
25
26 // Example 1: Basic Loader usage with single document
27 fmt.Println("1. Basic Loader - Single Document:")
28 singleDoc := `name: myapp
29 version: 1.0.0
30 tags:
31 - web
32 - api
33 `
34 var cfg Config
35 loader, err := yaml.NewLoader(strings.NewReader(singleDoc))
36 if err != nil {
37 panic(err)
38 }
39 if err := loader.Load(&cfg); err != nil {
40 panic(err)
41 }
42 fmt.Printf(" Loaded: %+v\n\n", cfg)
43
44 // Example 2: Multi-document stream
45 fmt.Println("2. Multi-Document Loader:")
46 multiDoc := `---
47 name: app1
48 version: 1.0.0
49 ---
50 name: app2
51 version: 2.0.0
52 tags:
53 - experimental
54 ---
55 name: app3
56 version: 3.0.0
57 `
58 loader2, err := yaml.NewLoader(strings.NewReader(multiDoc))
59 if err != nil {
60 panic(err)
61 }
62
63 docNum := 1
64 for {
65 var doc Config
66 err := loader2.Load(&doc)
67 if err == io.EOF {
68 break
69 }
70 if err != nil {
71 panic(err)
72 }
73 fmt.Printf(" Document %d: %+v\n", docNum, doc)
74 docNum++
75 }
76 fmt.Println()
77
78 // Example 3: WithSingleDocument option
79 fmt.Println("3. Loader with WithSingleDocument (stops after first doc):")
80 loader3, err := yaml.NewLoader(strings.NewReader(multiDoc), yaml.WithSingleDocument())
81 if err != nil {
82 panic(err)
83 }
84
85 var firstDoc Config
86 if err := loader3.Load(&firstDoc); err != nil {
87 panic(err)
88 }
89 fmt.Printf(" First document: %+v\n", firstDoc)
90
91 var secondDoc Config
92 err = loader3.Load(&secondDoc)
93 if err == io.EOF {
94 fmt.Println(" Second Load() returned io.EOF (as expected)")
95 }
96
97 // Example 4: Dumper - single document
98 fmt.Println("4. Basic Dumper - Single Document:")
99 var buf bytes.Buffer
100 dumper, err := yaml.NewDumper(&buf)
101 if err != nil {
102 panic(err)
103 }
104
105 cfg1 := Config{Name: "service1", Version: "1.0.0", Tags: []string{"prod"}}
106 if err := dumper.Dump(&cfg1); err != nil {
107 panic(err)
108 }
109 if err := dumper.Close(); err != nil {
110 panic(err)
111 }
112 fmt.Printf(" Output:\n%s\n", buf.String())
113
114 // Example 5: Dumper - multiple documents
115 fmt.Println("5. Dumper - Multiple Documents:")
116 buf.Reset()
117 dumper2, err := yaml.NewDumper(&buf)
118 if err != nil {
119 panic(err)
120 }
121
122 cfg2 := Config{Name: "service1", Version: "1.0.0"}
123 cfg3 := Config{Name: "service2", Version: "2.0.0", Tags: []string{"dev"}}
124 cfg4 := Config{Name: "service3", Version: "3.0.0"}
125
126 if err := dumper2.Dump(&cfg2); err != nil {
127 panic(err)
128 }
129 if err := dumper2.Dump(&cfg3); err != nil {
130 panic(err)
131 }
132 if err := dumper2.Dump(&cfg4); err != nil {
133 panic(err)
134 }
135 if err := dumper2.Close(); err != nil {
136 panic(err)
137 }
138 fmt.Printf(" Output:\n%s\n", buf.String())
139
140 // Example 6: Dumper with options
141 fmt.Println("6. Dumper with WithIndent(4):")
142 buf.Reset()
143 dumper3, err := yaml.NewDumper(&buf, yaml.WithIndent(4))
144 if err != nil {
145 panic(err)
146 }
147
148 cfg5 := Config{Name: "service", Version: "1.0.0", Tags: []string{"a", "b", "c"}}
149 if err := dumper3.Dump(&cfg5); err != nil {
150 panic(err)
151 }
152 if err := dumper3.Close(); err != nil {
153 panic(err)
154 }
155 fmt.Printf(" Output (4-space indent):\n%s\n", buf.String())
156
157 fmt.Println("=== Demo Complete ===")
158 }