feat(deps): replace yaml.v3 with goccy/go-yaml library

Replace yaml.v3 with goccy/go-yaml, as the former is now unmaintained.

Also upgrade minimum Go version to 1.21.0 and update testify to v1.10.0.

Add support for configuring encoder options in YAML renderer to
provide more flexibility in YAML output formatting. Include new options
for sequence indentation and automatic integer conversion.

Implement support for both yaml.InterfaceMarshaler and 
yaml.BytesMarshaler interfaces with appropriate test cases. Rename mock
implementation to clarify interface implementation.
This commit is contained in:
2025-06-11 03:47:01 +01:00
parent 3d82cdc7e3
commit 66560625d6
8 changed files with 410 additions and 179 deletions

54
yaml.go
View File

@@ -4,16 +4,31 @@ import (
"fmt"
"io"
"gopkg.in/yaml.v3"
"github.com/goccy/go-yaml"
)
var YAMLDefaultIndent = 2
var (
// YAMLDefaultIndent is the default number of spaces to use for indenting
// nested blocks in the output YAML.
YAMLDefaultIndent = 2
// YAMLDefaultIndentSequence is the default value for the IndentSequence
// option passed to yaml.NewEncoder(). When true, sequences will be
// indented by default.
YAMLDefaultIndentSequence = true
)
// 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 is the number of spaces to use for indenting nested blocks in the
// output YAML. When empty/zero, YAMLDefaultIndent will be used.
Indent int
// EncodeOptions is a list of options to pass to yaml.NewEncoder().
//
// These options will be appended to the default indent and sequence indent
// options. With duplicate options, the last one will take precedence.
EncodeOptions []yaml.EncodeOption
}
var (
@@ -21,21 +36,38 @@ var (
_ 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
func (y *YAML) buildEncodeOptions() []yaml.EncodeOption {
indent := YAMLDefaultIndent
if y.Indent > 0 {
indent = y.Indent
}
enc := yaml.NewEncoder(w)
enc.SetIndent(indent)
opts := []yaml.EncodeOption{
yaml.Indent(indent),
yaml.IndentSequence(YAMLDefaultIndentSequence),
}
if len(y.EncodeOptions) > 0 {
opts = append(opts, y.EncodeOptions...)
}
return opts
}
// Render marshals the given value to YAML.
func (y *YAML) Render(w io.Writer, v any) error {
enc := yaml.NewEncoder(w, y.buildEncodeOptions()...)
err := enc.Encode(v)
if err != nil {
return fmt.Errorf("%w: %w", ErrFailed, err)
}
err = enc.Close()
if err != nil {
return fmt.Errorf("%w: %w", ErrFailed, err)
}
return nil
}