Permalink
Browse files

CDRIVER-99 allow DBRefs

  • Loading branch information...
1 parent dba1666 commit 61ae288881bd035088d1a1f025ae33eb6609c476 @banker banker committed May 25, 2012
Showing with 33 additions and 1 deletion.
  1. +20 −1 src/encoding.c
  2. +13 −0 test/validate_test.c
View
@@ -101,6 +101,24 @@ static int isLegalUTF8( const unsigned char *source, int length ) {
return 1;
}
+/* If the name is part of a db ref ($ref, $db, or $id), then return true. */
+static int bson_string_is_db_ref( const unsigned char *string, const int length ) {
+ int result = 0;
+
+ if( length >= 4 ) {
+ if( string[1] == 'r' && string[2] == 'e' && string[3] == 'f' )
+ result = 1;
+ }
+ else if( length >= 3 ) {
+ if( string[1] == 'i' && string[2] == 'd' )
+ result = 1;
+ else if( string[1] == 'd' && string[2] == 'b' )
+ result = 1;
+ }
+
+ return result;
+}
+
static int bson_validate_string( bson *b, const unsigned char *string,
const int length, const char check_utf8, const char check_dot,
const char check_dollar ) {
@@ -109,7 +127,8 @@ static int bson_validate_string( bson *b, const unsigned char *string,
int sequence_length = 1;
if( check_dollar && string[0] == '$' ) {
- b->err |= BSON_FIELD_INIT_DOLLAR;
+ if( !bson_string_is_db_ref( string, length ) )
+ b->err |= BSON_FIELD_INIT_DOLLAR;
}
while ( position < length ) {
View
@@ -52,6 +52,19 @@ int main() {
ASSERT( b.err & BSON_FIELD_HAS_DOT );
+ /* Don't set INIT dollar if deb ref fields are being used. */
+ result = bson_append_string( &b , "$id" , "17" );
+ ASSERT( result == BSON_OK );
+ ASSERT( !(b.err & BSON_FIELD_INIT_DOLLAR) );
+
+ result = bson_append_string( &b , "$ref" , "17" );
+ ASSERT( result == BSON_OK );
+ ASSERT( !(b.err & BSON_FIELD_INIT_DOLLAR) );
+
+ result = bson_append_string( &b , "$db" , "17" );
+ ASSERT( result == BSON_OK );
+ ASSERT( !(b.err & BSON_FIELD_INIT_DOLLAR) );
+
result = bson_append_string( &b , "$ab" , "17" );
ASSERT( result == BSON_OK );
ASSERT( b.err & BSON_FIELD_INIT_DOLLAR );

0 comments on commit 61ae288

Please sign in to comment.