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: json.Marshal does not error with name conflicts between anonymous structs #8121

Closed
gopherbot opened this issue May 29, 2014 · 6 comments
Milestone

Comments

@gopherbot
Copy link

by trey@jellolabs.com:

My version:
go version go1.2 darwin/amd64

Steps to reproduce:
http://play.golang.org/p/XBaDkueRNM

What happened?
Field name conflicts within anonymous structs result in empty marshal and no error

What should have happened instead?
It should give an error
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-go1.4.

@gopherbot
Copy link
Author

Comment 2:

CL https://golang.org/cl/102030043 mentions this issue.

@OneOfOne
Copy link
Contributor

Comment 3:

Can you please try this CL https://golang.org/cl/102030043?

@gopherbot
Copy link
Author

Comment 4:

CL https://golang.org/cl/145820044 mentions this issue.

@rsc
Copy link
Contributor

rsc commented Oct 6, 2014

Comment 5:

Your CL makes reference to V8 and Python. That's not interesting. This is Go. The Go
rule is that if you have two embedded structs with the same field name at the same depth
in a struct type, that field name is not accessible from the top level of the struct
using the . operator.
json.Marshal uses all the fields that are accessible from the top level using the .
operator. If there are JSON-specific tag overrides, it applies the same Go rule to those
instead of the Go field names. But either way it is the Go semantics that are being used
here, not anything defined by JSON or V8 or Python.
To be very concrete, here is an edited version of your program:
http://play.golang.org/p/aFmsL3WA-L
package main
import (
    "fmt"
)
func main() {
    type a struct {
        Hello string
    }
    type b struct {
        Hello string
    }
    type c struct {
        a
        b
    }
    
    herp := c{a{"hello"},b{"world"}}
    fmt.Println(herp.Hello)
}
This doesn't compile. There is no herp.Hello. Therefore json.Marshal does not marshal
one.

Status changed to WorkingAsIntended.

@gopherbot
Copy link
Author

Comment 6 by trey@jellolabs.com:

Don't you think that json.Marshal should return a runtime error similar to the one given
by the compiler in your example?

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants