go-yaml / yaml

YAML support for the Go language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unmarshaler ignored: cannot parse 'ReadOnly' as int: strconv.ParseInt: parsing "deny": invalid syntax

andig opened this issue · comments

I'm trying to parse an enum from string:

readonly: deny

with readonly defined as:

type ReadOnlyMode int

const (
	ReadOnlyFalse ReadOnlyMode = iota
	ReadOnlyDeny
	ReadOnlyTrue
)

Both Text- and Yaml unmarshaler are implemented:

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

func (m *ReadOnlyMode) UnmarshalText(text []byte) error {
	mode, err := ReadOnlyModeString(string(text))
	if err == nil {
		*m = mode
	}

	return err
}

var _ yaml.Unmarshaler = (*ReadOnlyMode)(nil)

func (m *ReadOnlyMode) UnmarshalYAML(value *yaml.Node) error {
	mode, err := ReadOnlyModeString(value.Value)
	if err == nil {
		*m = mode
	}

	return err
}

Yet decoding fails:

cannot parse 'ReadOnly' as int: strconv.ParseInt: parsing "deny": invalid syntax

It seems this may be a question of order: should it be possible for the presence of an unmarshaler to override the type detection (int)?