Unmarshaler ignored: cannot parse 'ReadOnly' as int: strconv.ParseInt: parsing "deny": invalid syntax
andig opened this issue · comments
andig commented
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)?
Roman Sharkov commented
Works fine for me: https://go.dev/play/p/NvGx1tx8vxN