Debian Codegraph @

internal/libyaml/dumper.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // This file contains the Dumper API for writing YAML documents.
5 //
6 // Primary functions:
7 // - Dump: Encode value(s) to YAML (use WithAll for multi-doc)
8 // - NewDumper: Create a streaming dumper to io.Writer
9
10 package libyaml
11
12 import (
13 "bytes"
14 "io"
15 "reflect"
16 )
17
18 // A Dumper writes YAML values to an output stream with configurable options.
19 // It uses a 3-stage pipeline mirroring the Loader:
20 // 1. Representer: Go values → Tagged Node tree
21 // 2. Desolver: Remove inferable tags
22 // 3. Serializer: Node tree → Events → YAML
23 type Dumper struct {
24 representer *Representer
25 desolver *Desolver
26 serializer *Serializer
27 options *Options
28 }
29
30 // NewDumper returns a new Dumper that writes to w with the given options.
31 //
32 // The Dumper should be closed after use to flush all data to w.
33 func NewDumper(w io.Writer, opts ...Option) (*Dumper, error) {
34 o, err := ApplyOptions(opts...)
35 if err != nil {
36 return nil, err
37 }
38 return &Dumper{
39 representer: NewRepresenter(o), // No writer - builds nodes
40 desolver: NewDesolver(o),
41 serializer: NewSerializer(w, o), // Writer here - emits YAML
42 options: o,
43 }, nil
44 }
45
46 // Dump encodes a value to YAML with the given options.
47 //
48 // By default, Dump encodes a single value as a single YAML document.
49 //
50 // Use WithAllDocuments() to encode multiple values as a multi-document stream:
51 //
52 // docs := []Config{config1, config2, config3}
53 // yaml.Dump(docs, yaml.WithAllDocuments())
54 //
55 // When WithAllDocuments is used, in must be a slice.
56 // Each element is encoded as a separate YAML document with "---" separators.
57 //
58 // See [Marshal] for details about the conversion of Go values to YAML.
59 func Dump(in any, opts ...Option) (out []byte, err error) {
60 defer handleErr(&err)
61
62 o, err := ApplyOptions(opts...)
63 if err != nil {
64 return nil, err
65 }
66
67 var buf bytes.Buffer
68 d, err := NewDumper(&buf, func(opts *Options) error {
69 *opts = *o // Copy options
70 return nil
71 })
72 if err != nil {
73 return nil, err
74 }
75
76 if o.AllDocuments {
77 // Multi-document mode: in must be a slice
78 inVal := reflect.ValueOf(in)
79 if inVal.Kind() != reflect.Slice {
80 return nil, &DumpError{
81 Stage: RepresenterStage,
82 Message: "WithAllDocuments requires a slice input",
83 }
84 }
85
86 // Dump each element as a separate document
87 for i := 0; i < inVal.Len(); i++ {
88 if err := d.Dump(inVal.Index(i).Interface()); err != nil {
89 return nil, err
90 }
91 }
92 } else {
93 // Single-document mode
94 if err := d.Dump(in); err != nil {
95 return nil, err
96 }
97 }
98
99 if err := d.Close(); err != nil {
100 return nil, err
101 }
102 return buf.Bytes(), nil
103 }
104
105 // Dump writes the YAML encoding of v to the stream.
106 //
107 // If multiple values are dumped to the stream, the second and subsequent
108 // documents will be preceded with a "---" document separator.
109 //
110 // See the documentation for [Marshal] for details about the conversion of Go
111 // values to YAML.
112 func (d *Dumper) Dump(v any) (err error) {
113 defer handleErr(&err)
114
115 // Stage 1: Represent - Go values → Tagged Node tree
116 node := d.representer.Represent("", reflect.ValueOf(v))
117
118 // Stage 2: Desolve - Remove inferable tags
119 d.desolver.Desolve(node)
120
121 // Stage 3: Serialize - Node tree → Events → YAML
122 d.serializer.Serialize(node)
123
124 return nil
125 }
126
127 // Close closes the Dumper by writing any remaining data.
128 // It does not write a stream terminating string "...".
129 func (d *Dumper) Close() (err error) {
130 defer handleErr(&err)
131 d.serializer.Finish()
132 return nil
133 }
134
135 // SetIndent changes the indentation used when encoding.
136 // This is used by the legacy Encoder.SetIndent() method.
137 func (d *Dumper) SetIndent(spaces int) {
138 if spaces < 0 {
139 failDumpf(SerializerStage, "cannot indent to a negative number of spaces")
140 }
141 // Set on serializer's emitter
142 d.serializer.Emitter.BestIndent = spaces
143 }
144
145 // SetCompactSeqIndent controls whether '- ' is considered part of the indentation.
146 // This is used by the legacy Encoder methods.
147 func (d *Dumper) SetCompactSeqIndent(compact bool) {
148 d.serializer.Emitter.CompactSequenceIndent = compact
149 }