[GODRIVER-731] Maps inside structs are not zeroed Created: 20/Dec/18  Updated: 28/Oct/23  Resolved: 12/Feb/19

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 0.1.0
Fix Version/s: 1.0.0-rc1

Type: Improvement Priority: Minor - P4
Reporter: David Golden Assignee: Divjot Arora (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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.



 Comments   
Comment by Githook User [ 11/Feb/19 ]

Author:

{'name': 'Divjot Arora', 'email': 'divjot.arora@10gen.com', 'username': 'divjotarora'}

Message: Document behavior of default struct decoder for maps and slices.

GODRIVER-731

Change-Id: I15ce9d98f737871f6d2b80af413ac3d043b2dd00
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/1806418001530a9cb06601ff6bb8d7aaf8b678b5

Comment by David Golden [ 08/Feb/19 ]

After discussion, I think we should just document the behavior and leave the behavior matching encoding/json.

Comment by Divjot Arora (Inactive) [ 07/Feb/19 ]

Looked further into this. The behavior we have for maps and slices matches the behavior of encoding/json (add fields to an existing map and clear away an existing slice). Not sure what has to be done here - we can either just update the documentation to make this behavior known or change the behavior to not match encoding/json. david.golden kris.brandow

Comment by Jeffrey Yemin [ 20/Dec/18 ]

Changing type to Improvement as I believe this is the intended behavior.

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