Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2820

Consider exported and private struct fields for "Encoder.OmitZeroStruct"

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • 2.1.0
    • Affects Version/s: None
    • Component/s: BSON
    • None
    • Major Change
    • Hide

      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?

      Show
      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?

      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

            Assignee:
            Unassigned Unassigned
            Reporter:
            matt.dale@mongodb.com Matt Dale
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated: