Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andig opened this issue Nov 5, 2023 · 0 comments

Comments

@andig
Copy link

andig commented Nov 5, 2023

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant