Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

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/[email protected]/decode_hooks.go:274 +0x15c

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

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

No branches or pull requests

1 participant