Permalink
Browse files

nicer way to create bson

     BSONField<int> x("x");
     BSONObj o = BSON( x(5) );
     ASSERT_EQUALS( BSON( "x" << 5 ) , o );

     o = BSON( x.gt(5) );
     ASSERT_EQUALS( BSON( "x" << BSON( "$gt" << 5 ) ) , o );
  • Loading branch information...
erh committed Jun 21, 2010
1 parent 66bdde3 commit a7f063dc2178b6745cca289c68687cf6939805ca
Showing with 90 additions and 0 deletions.
  1. +7 −0 bson/bsoninlines.h
  2. +55 −0 bson/bsonobjbuilder.h
  3. +28 −0 dbtests/jsobjtests.cpp
View
@@ -572,4 +572,11 @@ namespace mongo {
}
inline void BSONElement::Val(BSONObj& v) const { v = Obj(); }
+
+ template<typename T>
+ inline BSONFieldValue<BSONObj> BSONField<T>::query( const char * q , const T& t ) const {
+ BSONObjBuilder b;
+ b.append( q , t );
+ return BSONFieldValue<BSONObj>( _name , b.obj() );
+ }
}
View
@@ -33,6 +33,48 @@ namespace mongo {
#pragma warning( disable : 4355 )
#endif
+ template<typename T>
+ class BSONFieldValue {
+ public:
+ BSONFieldValue( const string& name , const T& t ){
+ _name = name;
+ _t = t;
+ }
+
+ const T& value() const { return _t; }
+ const string& name() const { return _name; }
+
+ private:
+ string _name;
+ T _t;
+ };
+
+ template<typename T>
+ class BSONField {
+ public:
+ BSONField( const string& name , const string& longName="" )
+ : _name(name), _longName(longName){}
+ const string& name() const { return _name; }
+ operator string() const { return _name; }
+
+ BSONFieldValue<T> make( const T& t ) const {
+ return BSONFieldValue<T>( _name , t );
+ }
+
+ BSONFieldValue<BSONObj> gt( const T& t ) const { return query( "$gt" , t ); }
+ BSONFieldValue<BSONObj> lt( const T& t ) const { return query( "$lt" , t ); }
+
+ BSONFieldValue<BSONObj> query( const char * q , const T& t ) const;
+
+ BSONFieldValue<T> operator()( const T& t ) const {
+ return BSONFieldValue<T>( _name , t );
+ }
+
+ private:
+ string _name;
+ string _longName;
+ };
+
/** Utility for creating a BSONObj.
See also the BSON() and BSON_ARRAY() macros.
*/
@@ -527,6 +569,19 @@ namespace mongo {
return _s << l;
}
+ template<typename T>
+ BSONObjBuilderValueStream& operator<<( const BSONField<T>& f ) {
+ _s.endField( f.name().c_str() );
+ return _s;
+ }
+
+ template<typename T>
+ BSONObjBuilder& operator<<( const BSONFieldValue<T>& v ) {
+ append( v.name().c_str() , v.value() );
+ return *this;
+ }
+
+
/** @return true if we are using our own bufbuilder, and not an alternate that was given to us in our constructor */
bool owned() const { return &_b == &_buf; }
View
@@ -1512,6 +1512,33 @@ namespace JsobjTests {
}
};
+ class BSONFieldTests {
+ public:
+ void run(){
+ {
+ BSONField<int> x("x");
+ BSONObj o = BSON( x << 5 );
+ ASSERT_EQUALS( BSON( "x" << 5 ) , o );
+ }
+
+ {
+ BSONField<int> x("x");
+ BSONObj o = BSON( x.make(5) );
+ ASSERT_EQUALS( BSON( "x" << 5 ) , o );
+ }
+
+ {
+ BSONField<int> x("x");
+ BSONObj o = BSON( x(5) );
+ ASSERT_EQUALS( BSON( "x" << 5 ) , o );
+
+ o = BSON( x.gt(5) );
+ ASSERT_EQUALS( BSON( "x" << BSON( "$gt" << 5 ) ) , o );
+ }
+
+ }
+ };
+
class All : public Suite {
public:
All() : Suite( "jsobj" ){
@@ -1607,6 +1634,7 @@ namespace JsobjTests {
add< ElementSetTest >();
add< EmbeddedNumbers >();
add< BuilderPartialItearte >();
+ add< BSONFieldTests >();
}
} myall;

0 comments on commit a7f063d

Please sign in to comment.