-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
reflect: Struct.PkgPath may be empty even if Name is unexported #21122
Comments
Some evidence in support of changing the behavior. The following all make the assumption that
In the standard library, I did not find a single case where the current behavior is desired. I glanced through a bunch of usages of this inside Google and it seems pervasive that people expect this property to be true. |
CL https://golang.org/cl/50711 mentions this issue. |
…tructs https://golang.org/cl/33773 fixes the JSON marshaler to avoid serializing embedded fields on unexported types of non-struct types. However, Go allows embedding pointer to types, so the check for whether the field is a non-struct type must first dereference the pointer to get at the underlying type. Furthermore, due to a edge-case in the behavior of StructField.PkgPath not being a reliable indicator of whether the field is unexported (see #21122), we use our own logic to determine whether the field is exported or not. The logic in this CL may be simplified depending on what happens in #21122. Fixes #21121 Updates #21122 Change-Id: I8dfd1cdfac8a87950df294a566fb96dfd04fd749 Reviewed-on: https://go-review.googlesource.com/50711 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
Change https://golang.org/cl/53643 mentions this issue: |
I ran my change through global testing (at Google) to detect regressions, and only 1 target failed as a result of this change. The failure was in Given that fixing this issue also fixes another bug in |
Change https://golang.org/cl/60410 mentions this issue: |
Change https://golang.org/cl/65550 mentions this issue: |
CL 60410 fixes the compiler such that reflect.StructField.PkgPath is non-empty if and only if the field is unexported. Given that property, we can cleanup the logic in the json encoder to avoid parsing the field name to detect export properties. Updates #21122 Change-Id: Ic01b9c4ca76386774846b742b0c1b9b948f53e7c Reviewed-on: https://go-review.googlesource.com/65550 Run-TryBot: Joe Tsai <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
Since Go1.0, the following:
Has printed the following:
Notice that StructField.PkgPath for an embedded unexported pointer type is empty, while it is populated for an unexported non-pointer type. This is surprising since the documentation for StructField.PkgPath says the following:
Although not explicitly stated, I would expect it to be non-empty for unexported field names.
We should either:
Given that the current behavior has persisted for 9 releases of Go, perhaps we should just document this? However, this difference in behavior has led to bugs (see #21121).
\cc @ianlancetaylor @rsc @crawshaw
The text was updated successfully, but these errors were encountered: