-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
cmd/vet: should report json tag conflict for embedded struct #25593
Comments
/cc @robpike @mvdan @josharian |
I'd also wonder if this should be Agreed that this vet enhancement makes sense. |
Offhand this seems a minor addition to the struct tags check if it can be done efficiently. Is it actually a problem that shows up in practice? |
@robpike I'm working on a patch now, which will let us see how many new vet warnings it would see in existing Go code. |
It depends on what you mean. I didn't had such a bug yet in my code. But I do use embedded structs a lot, I do use custom 1-3 char json names (saves about 50% of json length and it's important for us because we store 300KB json in mysql and transfer/decode 600KB or 300KB is a noticeable difference) and I do spend a lot of time and attention to manually check tags to ensure they won't conflict. Actually I've asked one of our developers to write a unit test using reflect to automate this check - and before creating that task I've checked how good metalinter's checkers already handle this case, found vet handle it partially and opened this issue. |
Change https://golang.org/cl/115676 mentions this issue: |
Change https://golang.org/cl/115677 mentions this issue: |
A quick fix is implemented above, on top of a small refactor to use go/types. I quickly checked the Go tree, and it found no new cases. I'm currently downloading https://github.com/rsc/corpus, which should be a large enough corpus of packages to make a decision. GitHub is throttling me to about 100KB/s, so it's going to take a few hours. |
I failed to use https://github.com/rsc/corpus after nearly an hour of trying. Even when using the tarball and recursively downloading deps, I always got stuck with some packages not typechecking properly. If anyone knows how to use it, any help will be appreciated. I settled for some ad-hoc testing on packages that I regularly use and their dependencies. I found that a certain popular package has lots of newly found warnings thanks to this check:
|
This lets us simplify the code considerably. For example, unquoting the tag is no longer necessary, and we can get the field name with a single method call. While at it, fix a typechecking error in testdata/structtag.go, which hadn't been caught since vet still skips past go/types errors in most cases. Using go/types will also let us expand the structtag check more easily if we want to, for example to allow it to check for duplicates in embedded fields. Finally, update one of the test cases to check for regressions when we output invalid tag strings. We also checked that these two changes to testdata/structtag.go didn't fail with the old structtag check. For #25593. Change-Id: Iea4906d0f30a67f36b28c21d8aa96251aae653f5 Reviewed-on: https://go-review.googlesource.com/115676 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Rob Pike <r@golang.org>
I'm pretty confident that this is worth doing - it's very little extra work for vet now that it can rely on Ping @alandonovan @josharian (as per dev.golang.org/owners) for a decision. I've got a working CL that you can try; I just want to be sure we want the change before cleaning it up and adding tests. |
Seems reasonable to me, for go1.12. |
Thanks for the feedback - the CL is now ready to review at https://go-review.googlesource.com/c/go/+/115677. |
What version of Go are you using (
go version
)?go version go1.10.2 linux/amd64
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOCACHE="/home/powerman/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/powerman/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="x86_64-pc-linux-gnu-gcc"
CXX="x86_64-pc-linux-gnu-g++"
CGO_ENABLED="1"
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-build251037373=/tmp/go-build -gno-record-gcc-switches"
What did you do?
https://play.golang.org/p/zvagdPQx1Gf
What did you expect to see?
go vet
should warn about using same json tag name in embedded struct.It already warn about using same json tag name in same struct, so making it detect embedded fields looks like obvious enhancement.
May be related to #17609
The text was updated successfully, but these errors were encountered: