diff options
author | Eliot Horowitz <eliot@10gen.com> | 2010-01-16 11:08:32 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2010-01-16 11:08:32 -0500 |
commit | 6830dd0dd0462492ec0547050fb0eeb2908591b3 (patch) | |
tree | 39aba7a9caa9b7ea6ba5352d2b631cdcd0ce1a48 | |
parent | cd7148702bd90ab0b300e1f43b5eba5e173c895c (diff) | |
download | mongo-6830dd0dd0462492ec0547050fb0eeb2908591b3.tar.gz |
BSONObj::okForStorage - just checks for $ and . for
-rw-r--r-- | db/jsobj.cpp | 31 | ||||
-rw-r--r-- | db/jsobj.h | 5 | ||||
-rw-r--r-- | dbtests/jsobjtests.cpp | 28 |
3 files changed, 64 insertions, 0 deletions
diff --git a/db/jsobj.cpp b/db/jsobj.cpp index 16dcf5c35c1..8f5a83d70b9 100644 --- a/db/jsobj.cpp +++ b/db/jsobj.cpp @@ -1129,6 +1129,37 @@ namespace mongo { return b.obj(); } + bool BSONObj::okForStorage() const { + BSONObjIterator i( *this ); + while ( i.more() ){ + BSONElement e = i.next(); + const char * name = e.fieldName(); + + if ( strchr( name , '.' ) || + strchr( name , '$' ) ){ + return false; + } + + if ( e.mayEncapsulate() ){ + switch ( e.type() ){ + case Object: + case Array: + if ( ! e.embeddedObject().okForStorage() ) + return false; + break; + case CodeWScope: + if ( ! e.codeWScopeObject().okForStorage() ) + return false; + break; + default: + uassert( 12579, "unhandled cases in BSONObj okForStorage" , 0 ); + } + + } + } + return true; + } + string BSONObj::hexDump() const { stringstream ss; const char *d = objdata(); diff --git a/db/jsobj.h b/db/jsobj.h index 92c9dba7259..e9b0122b21a 100644 --- a/db/jsobj.h +++ b/db/jsobj.h @@ -852,6 +852,11 @@ namespace mongo { bool isValid(); + /** @return if the user is a valid user doc + criter: isValid() no . or $ field names + */ + bool okForStorage() const; + /** @return true if object is empty -- i.e., {} */ bool isEmpty() const { return objsize() <= 5; diff --git a/dbtests/jsobjtests.cpp b/dbtests/jsobjtests.cpp index 1d89537b8c3..0402426cbb0 100644 --- a/dbtests/jsobjtests.cpp +++ b/dbtests/jsobjtests.cpp @@ -1219,6 +1219,32 @@ namespace JsobjTests { }; + class checkForStorageTests { + public: + + void good( string s ){ + BSONObj o = fromjson( s ); + if ( o.okForStorage() ) + return; + throw UserException( 12528 , (string)"should be ok for storage:" + s ); + } + + void bad( string s ){ + BSONObj o = fromjson( s ); + if ( ! o.okForStorage() ) + return; + throw UserException( 12529 , (string)"should NOT be ok for storage:" + s ); + } + + void run(){ + good( "{x:1}" ); + bad( "{'x.y':1}" ); + + good( "{x:{a:2}}" ); + bad( "{x:{'$a':2}}" ); + } + }; + class All : public Suite { public: All() : Suite( "jsobj" ){ @@ -1305,6 +1331,8 @@ namespace JsobjTests { add< ArrayMacroTest >(); add< NumberParsing >(); add< bson2settest >(); + add< checkForStorageTests >(); + } } myall; |