[CSHARP-509] Remove superfluous Create methods and change how remaining Create methods handle C# null Created: 22/Jun/12  Updated: 22/May/15  Resolved: 21/Apr/14

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.8
Fix Version/s: 2.0

Type: Improvement Priority: Major - P3
Reporter: Brian Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-555 BsonDocument object model methods sho... Closed
related to CSHARP-557 Deprecate many Create methods from Bs... Closed
related to CSHARP-1287 DOCS- BsonValues no longer accept nul... Closed

 Description   

[Original description]

var value = BsonBoolean.Create("false");

value.RawValue == true

?!!!

[Edited description]

The BsonDocument object model has many Create methods that are not strictly required. We are going to do some cleaning up and remove them. There are explicit conversions that do exactly the same thing.

We are leaving the Create methods that take an object parameter, because in that case the user can't call a constructor, because the required BsonValue subclass isn't known at compile time.

We are also changing how the remaining Create methods handle C# null. They will now throw an ArgumentNullException if the value is C# null, with the exception of BsonValue.Create which will map C# null to BsonNull.Value.

We are also changing the Add methods to throw ArgumentNullException if callled with a C# null argument (they used to treat C# null as a no-op).



 Comments   
Comment by Githook User [ 21/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-509: Check for null keys.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/43b9ab26fe9a0f85787ccc4d5c15607efcb54cbd

Comment by Githook User [ 21/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-509: Fixed unit tests for Add and AddRange with C# null.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/baa58b5eadd269619e27ea2c5c660ab3eb74e3d5

Comment by Githook User [ 21/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-509: BsonDocument object model Add methods now throw ArgumentNullException if called with a C# null argument.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/8cad37b22079abde35b3dbdfbe6cd6e023d0fa5e

Comment by Githook User [ 21/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-509: Change how BsonValue Create methods handle C# null.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/2bd507b5b9422346422beedbeea8b1840d92373b

Comment by Githook User [ 21/Apr/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-509: Remove superfluous Create methods.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/010bfa578a4450afd96dee07ad5cc7da5b7d844d

Comment by Robert Stam [ 24/Feb/13 ]

This work was prototyped on the experimental x2.0 branch. This ticket is being reopened for eventual porting to the official 2.0 branch.

Comment by auto [ 10/Sep/12 ]

Author:

{u'date': u'2012-09-10T14:54:59-07:00', u'email': u'craiggwilson@gmail.com', u'name': u'Craig Wilson'}

Message: CSHARP-509: added tests for BsonBoolean.Create.
Branch: x2.0
https://github.com/mongodb/mongo-csharp-driver/commit/14e6cccc0c0015a59c1df48c49ad0bb979418640

Comment by Robert Stam [ 10/Sep/12 ]

Fixed as part of some work on the linked JIRA issues.

Comment by Robert Stam [ 10/Sep/12 ]

Now that we have removed most Create methods in favor of using the constructors directly and removed the special handling of C# null in functional construction (where it used to mean skip this value), we have also now changed the remaining Create methods to map C# null to BsonNull.Value, since we no longer depend on the Create methods letting C# null flow through them.

So now BsonBoolean.Create(null) will throw an exception because C# null can't be mapped to a return type of BsonBoolean, but BsonValue.Create(null) will return BsonNull.Value, which does in fact return false when ToBoolean() is called.

Comment by Robert Stam [ 10/Jul/12 ]

Changed the summary description to reflect that while the issue was initially filed for BsonBoolean.Create("false") it evolved into the more general question of what BsonBoolean.Create(null) should return.

Comment by Robert Stam [ 10/Jul/12 ]

You make a good point, but currently all the BsonXyz.Create methods return C# null when passed a C# null, and we would have to change all of them to be consistent. This would also require changing the return type of BsonBoolean.Create from BsonBoolean to BsonValue because otherwise BsonNull.Value would be an invalid return value (and similarly for all the other Create methods).

I'm leaving this open but rescheduling it to 2.0 so that more time can be given to considering what (if anything) we should change. Also, if this ends up requiring breaking changes then 2.0 is a good time for it since 2.0 will have lots of other breaking changes.

Comment by Brian [ 22/Jun/12 ]

I got it.

But then BsonBoolean.Create(null) must be false
because null and undefined converting are False according to the http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

Comment by Robert Stam [ 22/Jun/12 ]

I can see why this is confusing, but here's the reasoning behind it:

  • the BsonDocument object model is a representation in C# of BSON/JSON documents
  • it therefore follows JavaScript conventions where possible
  • the BsonBoolean.Create method, doesn't "parse" its argument, it "converts" it
  • the conversion is done following JavaScript conventions
  • and in particular, it follows JavaScript's somewhat surprising definition of "truthiness"
  • by JavaScript's definition of "truthiness" the string "false" is considered to be a true value

If you actually want to parse the string instead of convert it using JavaScript's definition of truthiness, then you can write:

var value = BsonBoolean.Create(bool.Parse("false"));

Comment by Brian [ 22/Jun/12 ]

case Conversion.StringToBsonBoolean:
return BsonBoolean.Create(((string) value) != "");

Very good indeed assumption

Generated at Wed Feb 07 21:37:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.