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: better error message for invalid use of ,string struct tag, trying to unmarshal unquoted value into bool #64831

Open
andig opened this issue Dec 21, 2023 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@andig
Copy link
Contributor

andig commented Dec 21, 2023

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

$ go version
go version go1.21.5 darwin/arm64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/andig/Library/Caches/go-build'
GOENV='/Users/andig/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/andig/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/andig/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.21.5/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.21.5/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.5'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/andig/htdocs/evcc/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/sv/rs_453y57xj86xsbz3kw1mbc0000gn/T/go-build4140977264=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOROOT/bin/go version: go version go1.21.5 darwin/arm64
GOROOT/bin/go tool compile -V: compile version go1.21.5
uname -v: Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:34 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T8103
ProductName:		macOS
ProductVersion:		14.2
BuildVersion:		23C64
lldb --version: lldb-1500.0.200.58
Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)

What did you do?

Unmarshal json containing mixed quotes and unquoted values (upstream- cannot be changed):

ClimateStatus struct {
	PreClimateActive bool `json:"preClimateActive"` // false,
	Defrost          bool `json:"defrost,string"`   // "false",
}

What did you expect to see?

Error message should include the field this error happens on. There could potentially be more error cases that could benefit from more context.

What did you see instead?

Field name omitted- making it trial&error to identify the culprit in a bigger struct.

@thanm
Copy link
Contributor

thanm commented Dec 21, 2023

Thanks for the report. It would be helpful if you could provide a playground link or runnable test case.

@thanm thanm added this to the Backlog milestone Dec 21, 2023
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 21, 2023
@thanm
Copy link
Contributor

thanm commented Dec 21, 2023

@rsc @dsnet per owners

@andig
Copy link
Contributor Author

andig commented Dec 21, 2023

Sorry for the verbose test: https://go.dev/play/p/Cxvz7bneweb. I couldn't repro this with one or two levels of hierarchy. Is there anything limiting the field indicator in length?

@andig
Copy link
Contributor Author

andig commented Mar 18, 2024

evcc-io/evcc#12884 has another annoying error message:

json: invalid use of ,string struct tag, trying to unmarshal "" into int: %!s(<nil>)

Without building an entire test case with data it is totally impossible to find out, which field is causing this.

@andig
Copy link
Contributor Author

andig commented Mar 18, 2024

Another path of action could be to make declarations like

int `json:",string"`

accept "" as 0, following the reasoning that "" is the zero value for string, hence should be the zero value for string-encoded integers as well. Might fit into the json 2.0 discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants
@andig @thanm and others