[GODRIVER-1240] Sort with unordered map type Created: 20/Aug/19  Updated: 29/Aug/19  Resolved: 22/Aug/19

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: 1.1.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Pierre Durand Assignee: Divjot Arora (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The SetSort() option currently accepts an interface{} value.

But it is incorrect to use an unordered map here, because it could have unexpected behavior.

I would expect it to return an error (or panic).

 

The same problem exists for index keys.

 

The error message should recommend to use bson.A.



 Comments   
Comment by Divjot Arora (Inactive) [ 29/Aug/19 ]

pierrre Because we have a codec-based customizable BSON layer, a user could write a codec for bson.M. This would essentially be the same as defining a MarshalBSON for the type and would allow a user to specify an ordering. Because of this, it isn't really possible for us to know if users are using a plain bson.M or something more customized.

Comment by Pierre Durand [ 22/Aug/19 ]

implements MarshalBSON and therefore can enforce an ordering

Right, I didn't think about this.

Generally, we recommend using ordered types like bson.D and using the MongoDB documentation to find out which fields must be ordered.

But I've seen developers using bson.M instead of ordered data structure. I caught this error during a code review. So it's happening for real. It's not an hypothetical issue.

A proposal; maybe we could detect the issue for some specific types that we know will be invalid: bson.M and map[string]interface{}. We could do a type assertion without any reflection.

What do you think ?

Comment by Divjot Arora (Inactive) [ 22/Aug/19 ]

Hi pierrre,

Most of the options that take interface{} convert the given option into a document, which usually needs to be ordered. It would be difficult for us to error if someone gives a map for one of these options because a user could create a new type that is a map but implements MarshalBSON and therefore can enforce an ordering. It would be too strict for us to error in these situations. Generally, we recommend using ordered types like bson.D and using the MongoDB documentation to find out which fields must be ordered.

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