-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Implemented Default
for Bson
, Document
, and ObjectId
#82
Conversation
Looks great actually. I will merge now. cc @kyeah |
OID default should generate a new id via .new, i believe; what do yall think? |
Well, it depends on what you need for |
I'm actually a bit unsure that having Default for ObjectId is a good idea. At least in terms of usage with MongoDB, an all-zero value of ObjectId seems like a very bad idea (you can just use regular integers as your _id fields if you want to have control over the value), and having it generate a new one seems a little weird also given that two calls to ObjectId::default() won't return the same thing, which I feel like violates how Default is expected to behave. |
@zonyitoo I implemented |
I'm not certain it's really worth it just to avoid having to do The only argument in favor of implementing ObjectId::default I can think of is so that users can derive Default on structs that they want to serialize/deserialize to and from the database. This is also a pretty clear argument against having it being initialized to all zeroes. I don't think it would actually be terrible to implement it as a wrapper around ObjectId::new for this purpose, but I think it's a good idea to add a (doc) comment mentioning that it won't always return the same value. |
src/oid.rs
Outdated
/// method will be different every time it is executed. | ||
fn default() -> ObjectId { | ||
// Attempt to create a new `ObjectId`; if that fails return an empty `ObjectId` | ||
ObjectId::new().unwrap_or(ObjectId::with_bytes([0; 12])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unwrap_or_else
would be better
Oh geez, I had forgotten that ObjectId::new returns a Result. Given that the only reason I can think of to even have Default on ObjectId is to be able to serialize/deserialize structs, it seems extremely risky to just have silent failures; I can't imagine that many people would bother checking the resulting struct/bson doc to see if the ObjectId is all zeros even if we told them to, especially given that there isn't any easy way to do so, as the array field is (correctly) non-public. I think I'm back to recommending against having Default implemented for ObjectId, only more strongly than before. |
I agree, too much unexpected behavior and I guess it doesn't really fit in this situation. |
LGTM. Thanks for bearing with me when I kept changing my mind! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Completely agree with the conversation today -- too much unpredictable behavior for a Default implementation. Was out today, so appeeciate y'all talking this one through to it's conclusion.
Will be released tonight (GMT+8) |
Implemented
Default
forBson
,Document
, andObjectId
. I also fixed some warnings with the tests.