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

TextUnmarshallerHookFunc panics on string based types #327

Open
prochac opened this issue Apr 18, 2023 · 0 comments
Open

TextUnmarshallerHookFunc panics on string based types #327

prochac opened this issue Apr 18, 2023 · 0 comments

Comments

@prochac
Copy link
Contributor

prochac commented Apr 18, 2023

I don't know how it names properly, but what I mean is:

type MyString string

How I hit on the problem is with json.Decoder and UseNumber.

It marshals JSON object to map[string]any, but for JSON number it uses json.Number instead of float64, that can lose some precision.

func main() {
	hook := mapstructure.TextUnmarshallerHookFunc()

	input := json.Number("42")
	var target big.Int

	mapstructure.DecodeHookExec(
		hook,
		reflect.ValueOf(input),
		reflect.ValueOf(target),
	)
}
panic: interface conversion: interface {} is json.Number, not string
github.com/mitchellh/mapstructure.TextUnmarshallerHookFunc.func1({0x4e12c0?, 0x4aeb20?}, {0x4e12c0, 0x4b0ae0}, {0x4aeb20?, 0xc000108330})
	/tmp/gopath931946810/pkg/mod/github.com/mitchellh/mapstructure@v1.5.0/decode_hooks.go:274 +0x15c

https://go.dev/play/p/Ip7bWnkDK-r

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