-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaml.go
More file actions
45 lines (35 loc) · 874 Bytes
/
Copy pathyaml.go
File metadata and controls
45 lines (35 loc) · 874 Bytes
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
package render
import (
"fmt"
"io"
"gopkg.in/yaml.v3"
)
var YAMLDefaultIndent = 2
// YAML is a Handler that marshals the given value to YAML.
type YAML struct {
// Indent controls how many spaces will be used for indenting nested blocks
// in the output YAML. When Indent is zero, YAMLDefaultIndent will be used.
Indent int
}
var (
_ Handler = (*YAML)(nil)
_ FormatsHandler = (*YAML)(nil)
)
// Render marshals the given value to YAML.
func (y *YAML) Render(w io.Writer, v any) error {
indent := y.Indent
if indent == 0 {
indent = YAMLDefaultIndent
}
enc := yaml.NewEncoder(w)
enc.SetIndent(indent)
err := enc.Encode(v)
if err != nil {
return fmt.Errorf("%w: %w", ErrFailed, err)
}
return nil
}
// Formats returns a list of format strings that this Handler supports.
func (y *YAML) Formats() []string {
return []string{"yaml", "yml"}
}