go-yaml / yaml

YAML support for the Go language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UnmarshalText ignored in arrays

ainar-g opened this issue · comments

Code

package main

import (
	"encoding"
	"errors"
	"fmt"
	"log/slog"

	"gopkg.in/yaml.v3"
)

type obj struct {
	Values []value `yaml:"values"`
}

type value struct {
	param int
}

var _ encoding.TextUnmarshaler = (*value)(nil)

func (v *value) UnmarshalText(b []byte) (err error) {
	slog.Info("unmarshal", "b", b)

	return errors.New("unmarshal error")
}

const data = `
values:
  - a::
`

func main() {
	o := &obj{}
	err := yaml.Unmarshal([]byte(data), o)
	slog.Info("result", "err", err, "o", fmt.Sprintf("%#v", o))
}

Expected

The slog.Info in (*value).UnmarshalText to be called; an error.

Actual

2009/11/10 23:00:00 INFO result err=<nil> o=&main.obj{Values:[]main.value{main.value{param:0}}}

https://go.dev/play/p/1ucYRvnupUE