[GODRIVER-971] ability to marshal empty slices as bson [] rather than nil Created: 17/Apr/19 Updated: 27/Oct/23 Resolved: 13/Jan/20 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Sam Kleinman (Inactive) | Assignee: | Unassigned |
| Resolution: | Gone away | Votes: | 0 |
| Labels: | evg, mgocompat | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Epic Link: | mgo migration tool | ||||||||||||
| Description |
|
If a field in a struct is a slice, and it has no elements, and it is not omit empty, then it’s BSON value is nil. This means that you can’t treat it like an array (e.g. use $push,) and we ran into issues with $in as well. In MGO these types were [], and it appears that our application depends on this. |
| Comments |
| Comment by Divjot Arora (Inactive) [ 13/Jan/20 ] |
|
This was done as part of |
| Comment by Sam Kleinman (Inactive) [ 11/Jul/19 ] |
|
I think in order to support a migration path from mgobson to this bson library we need a top level switch. We have enough types (and some vendored types, plus type aliases/etc.) that it doesn't seem particularly tractable to just "omitempty" everywhere. Also, while omiting empty fields will fix some tests, it's not the same semantics as mgobson. |
| Comment by David Golden [ 18/Apr/19 ] |
|
It may be consistent with encoding/json, but because we're creating documents for a database where shape matters for what operators can be used, there's a reasonable argument to be made to keep the mgo behavior. OTOH, a struct with a nil slice field wouldn't roundtrip, as the decoded struct would an allocated slice field. That's not great, either. I think we should investigate what other static language drivers do in this situation? If we do keep the current behavior, we should probably document this clearly and recommend omitempty for slice type fields. Secondary question, if this behavior changes, should it also change for nil map[X]Y for consistency? |
| Comment by Sam Kleinman (Inactive) [ 18/Apr/19 ] |
|
I think that's fine generally, I just don't have a path out of using mgobson without some way to control this. |
| Comment by Kristofer Brandow (Inactive) [ 18/Apr/19 ] |
|
sam.kleinman, the current behavior is that if the slice is nil we will make a BSON Null and if it's of zero length, but allocated then we will make it a BSON Array that's empty. This is consistent with the encoding/json package: https://play.golang.org/p/lD5UqxAKtKj. |
| Comment by Sam Kleinman (Inactive) [ 18/Apr/19 ] |
|
Both cases, I think. Given that you can use the append variable to a nil slice, and that slices in structs are nil in the default construction, both are important to us. |
| Comment by David Golden [ 18/Apr/19 ] |
When you say "has no elements", do you mean the field is nil (i.e. zero value), or that the field is non-nil but has length zero, or are both cases a problem? |