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

Maps inside structs are not zeroed

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.0.0-rc1
    • Affects Version/s: 0.1.0
    • Component/s: BSON
    • Labels:
      None

      A map inside a struct is not zeroed on decoding.

      For example, given this type:

      type CollectionInfo struct {
              Name    string `bson:"name"`
              Type    string `bson:"type"`
              Options bson.M `bson:"options"`
      }
      

      and this code:

      	info := &CollectionInfo{}
      	for iter.Next(nil) {
      		_ = iter.Decode(info)
      	}
      

      The bson.M fields of the struct will not be zeroed and will accumulate data as the iterator progresses. This may surprise users who expect the struct. (In this case, the correct approach is to allocate a fresh struct inside the loop.)

      This should be documented. Another option might be to add a struct tag to request that a map field be zeroed.

            Assignee:
            divjot.arora@mongodb.com Divjot Arora (Inactive)
            Reporter:
            david.golden@mongodb.com David Golden
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: