1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/internal/yaml/yaml.go
fredbi 8d6ef40020
feat(yaml): upgraded from yaml.v2 to yaml.v3 (#3004)
* chore(yaml): upgraded from yaml.v2 to yaml.v3

* provided internal package to take care of backward
  compatible settings:
    * UnmarshalStrict method
    * mute io.EOF unmarshaling errors
    * marshal indenting with 2 chars
* adapted unit tests to new yaml v3

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

* fixed failing tests

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
2022-03-29 14:00:53 -03:00

53 lines
1.1 KiB
Go

// Package yaml wraps gopkg.in/yaml.v3 and helps transition from v2.
package yaml
import (
"bytes"
"errors"
"io"
yaml "gopkg.in/yaml.v3"
)
// UnmarshalStrict unmarshals a YAML document with strict behavior (only declared fields are tolerated).
func UnmarshalStrict(in []byte, out interface{}) error {
decoder := yaml.NewDecoder(bytes.NewReader(in))
decoder.KnownFields(true)
return handleErr(decoder.Decode(out))
}
// Unmarshal some struct as a YAML document, without strict behavior.
func Unmarshal(in []byte, out interface{}) error {
decoder := yaml.NewDecoder(bytes.NewReader(in))
decoder.KnownFields(false)
return handleErr(decoder.Decode(out))
}
// Marshal some struct as a YAML document.
func Marshal(in interface{}) ([]byte, error) {
b := new(bytes.Buffer)
encoder := yaml.NewEncoder(b)
encoder.SetIndent(2) // default is 4
if err := encoder.Encode(in); err != nil {
encoder.Close()
return nil, err
}
encoder.Close()
return b.Bytes(), nil
}
// handleErr mutes io.EOF errors for backward-compatibility.
func handleErr(err error) error {
if err != nil && !errors.Is(err, io.EOF) {
return err
}
return nil
}