[GODRIVER-2797] Treat named and anonymous private struct fields the same when marshaling Created: 05/Apr/23  Updated: 30/Aug/23  Resolved: 21/Apr/23

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Matt Dale Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Currently the Go BSON library considers a private struct field one that both has a non-empty PkgPath and is not anonymous (i.e. not embedded). For example:

structField.PkgPath != "" && !structField.Anonymous

However, that logic disagrees with the Go reflect definition of a private struct field, which only considers PkgPath != "". As implemented in the StructCodec code, the difference only affects the behavior of the AllowUnexportedFields option, but the result is that the AllowUnexportedFields option only applies to anonymous fields in structs.

That behavior seems to be inspired by similar logic in the mgo BSON code which incorrectly considers all anonymous fields non-private.

Definition of done:

  • Update the AllowUnexportedFields option to consider all private fields valid, not just anonymous ones.
  • Update the mgocompat registry to use a struct encoder that doesn't rely on the AllowUnexportedFields option.


 Comments   
Comment by Matt Dale [ 21/Apr/23 ]

Recent versions of Go restrict access to read and set unexported struct fields,so we're dropping support for AllowUnexportedFields in Go Driver 2.0. Closing this ticket.

Generated at Thu Feb 08 08:39:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.