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

encoding/json: unexpected behavior when unmarshalling to "boxed" struct #33993

Open
brenol opened this issue Aug 31, 2019 · 3 comments

Comments

@brenol
Copy link

commented Aug 31, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.9 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/goworkspace"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build909978713=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/IOwj_PHRGXK

What did you expect to see?

I did not expect v to be of type map[string]interface{}, as its underlying type is clearly Y.

What did you see instead?

vis of type map[string]interface{}, failing the type assertion.

Note that if it's a pointer to Y, it works correctly.
Also, this came from a bigger project where Sprintf was reporting both vand Y{} with the same underlying type, however, I was unable to reproduce that here.

Also, removing json.Unmarshal fixes the issue, as the boxed Y is cast correctly to Y when there is no unmarshal.

@OneOfOne

This comment has been minimized.

Copy link
Contributor

commented Aug 31, 2019

You need to use a pointer to Y:

https://play.golang.org/p/wYxDOs4QdUn

@katiehockman

This comment has been minimized.

Copy link
Contributor

commented Sep 3, 2019

@brenol can you confirm that this suggestion resolves your issue? If so, this issue can be closed.

You can also check out https://github.com/golang/go/wiki/Questions for more resources.

@brenol

This comment has been minimized.

Copy link
Author

commented Sep 3, 2019

Yes, the suggestion fixes the issue.

However, is this the expected behavior? I mean, as I mentioned, removing json.Unmarshal altogether does work and it does not panic.

My main issue here is: should json.Unmarshal change the underlying type of a by value struct to map[string]interface{}? I tried looking up for some reason this happens but it didn't make sense to me, as the docs don't make it clear why it works with pointers and not with values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.