Open
Description
Describe the bug
When marshalling a type with a custom text marshaller, returning an empty string (via an empty []byte{} array) for output results in the string being written to the actual YAML.
This breaks subsequent unmarshalling if the expectation is that an empty string should be a valid value.
To Reproduce
// You can edit this code!
// Click here and start typing.
package main
import (
"fmt"
"github.com/goccy/go-yaml"
)
type T struct {
X string
}
// UnmarshalText implements the encoding.TextUnmarshaler.
func (t *T) UnmarshalText(text []byte) error {
if string(text) == "" {
t.X = "THE NULL VALUE"
} else {
t.X = string(text)
}
return nil
}
// MarshalText implements the encoding.TextMarshaler.
func (t *T) MarshalText() ([]byte, error) {
if t.X == "THE NULL VALUE" {
return []byte{}, nil
}
return []byte(t.X), nil
}
func main() {
x := T{"THE NULL VALUE"}
output, _ := yaml.Marshal(&x)
fmt.Println(string(output))
}
Expected behavior
The above program should print an empty line when encoding THE NULL VALUE
since it is returned as an empty text marshalling. Instead the YAML is outputted as the string <nil>
- which is then read in as the string <nil>
.
Screenshots
If applicable, add screenshots to help explain your problem.
Version Variables
- Go version: 1.24
- go-yaml's Version: v1.16.0