[GODRIVER-2820] Consider exported and private struct fields for "Encoder.OmitZeroStruct" Created: 22/Apr/23  Updated: 08/Jan/24

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

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

Epic Link: Go Driver 2.0 BSON
Quarter: FY24Q3
Backwards Compatibility: Major Change
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   

The Encoder configuration OmitZeroStruct (and corresponding StructCodec.EncodeOmitDefaultStruct) currently only considers values for exported struct fields when evaluating the "omitempty" struct tag. If a private struct field is a non-zero value, the Encoder will still omit the value when using "omitempty". That is surprising behavior because it deviates from the standard Go equality rules, which consider exported and private struct fields. Update the logic to instantiate a zero-value struct and use the standard Go comparison logic to check if the given struct is the zero value.

Definition of done:

  • Update the logic in isZero to do the following if Encoder.OmitZeroStruct is enabled:

    return !v.IsValid() || v.IsZero()
    

  • Update documentation on Encoder.OmitZeroStruct to remove the note about only exported struct fields.
  • Update the test case in TestIsZero

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