Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- SERVER-2684: special types in shell (e.g. Timestamp) display in var…

…iety of ways, and can confuse users
  • Loading branch information...
commit a6bfe265732c01c7ea2626b6889dbc9bba6918ca 1 parent 025c898
@agirbal agirbal authored
View
2  jstests/objid7.js
@@ -4,7 +4,7 @@ b = new ObjectId( "4c1a478603eba73620000000" )
c = new ObjectId();
assert.eq( a.toString() , b.toString() , "A" )
-assert.eq( a.toString() , "4c1a478603eba73620000000" , "B" );
+assert.eq( a.toString() , "ObjectId(\"4c1a478603eba73620000000\")" , "B" );
assert( a.equals( b ) , "C" )
View
19 jstests/shelltypes.js
@@ -1,42 +1,53 @@
// check that constructor also works without "new"
-var a, b;
+var a;
+var b;
a = new ObjectId();
-b = ObjectId(a.toString());
+b = ObjectId(a.valueOf());
+printjson(a);
assert.eq(tojson(a), tojson(b), "oid");
a = new DBRef("test", "theid");
-b = DBRef(a["$ref"], a["$id"]);
+b = DBRef(a.getRef(), a.getId());
+printjson(a);
assert.eq(tojson(a), tojson(b), "dbref");
a = new DBPointer("test", new ObjectId());
-b = DBPointer(a.ns, a.id);
+b = DBPointer(a.getCollection(), a.getId());
+printjson(a);
assert.eq(tojson(a), tojson(b), "dbpointer");
a = new Timestamp(10, 20);
b = Timestamp(a.t, a.i);
+printjson(a);
assert.eq(tojson(a), tojson(b), "timestamp");
a = new BinData(3,"VQ6EAOKbQdSnFkRmVUQAAA==");
b = BinData(a.type, a.base64());
+printjson(a);
assert.eq(tojson(a), tojson(b), "bindata");
a = new UUID("550e8400e29b41d4a716446655440000");
b = UUID(a.hex());
+printjson(a);
assert.eq(tojson(a), tojson(b), "uuid");
a = new MD5("550e8400e29b41d4a716446655440000");
b = MD5(a.hex());
+printjson(a);
assert.eq(tojson(a), tojson(b), "md5");
a = new HexData(4, "550e8400e29b41d4a716446655440000");
b = HexData(a.type, a.hex());
+printjson(a);
assert.eq(tojson(a), tojson(b), "hexdata");
a = new NumberLong(100);
b = NumberLong(a.toNumber());
+printjson(a);
assert.eq(tojson(a), tojson(b), "long");
a = new NumberInt(100);
b = NumberInt(a.toNumber());
+printjson(a);
assert.eq(tojson(a), tojson(b), "int");
View
22 scripting/sm_db.cpp
@@ -571,16 +571,6 @@ namespace mongo {
JSCLASS_NO_OPTIONAL_MEMBERS
};
- JSBool object_id_tostring(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
- Convertor c(cx);
- return (JSBool) (*rval = c.getProperty( obj , "str" ));
- }
-
- JSFunctionSpec object_id_functions[] = {
- { "toString" , object_id_tostring , 0 , JSPROP_READONLY | JSPROP_PERMANENT, 0 } ,
- { 0 }
- };
-
// dbpointer
JSBool dbpointer_constructor( JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval ) {
@@ -903,7 +893,6 @@ zzz
return JS_TRUE;
}
-
JSClass numberlong_class = {
"NumberLong" , JSCLASS_HAS_PRIVATE ,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
@@ -1033,19 +1022,10 @@ zzz
return *rval = c.toval( ret.c_str() );
}
- JSBool numberint_tojson(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
- Convertor c(cx);
- int val = c.toNumberInt( obj );
- string ret = str::stream() << val;
- return *rval = c.toval( ret.c_str() );
- }
-
-
JSFunctionSpec numberint_functions[] = {
{ "valueOf" , numberint_valueof , 0 , JSPROP_READONLY | JSPROP_PERMANENT, 0 } ,
{ "toNumber" , numberint_tonumber , 0 , JSPROP_READONLY | JSPROP_PERMANENT, 0 } ,
{ "toString" , numberint_tostring , 0 , JSPROP_READONLY | JSPROP_PERMANENT, 0 } ,
- { "tojson" , numberint_tojson , 0 , JSPROP_READONLY | JSPROP_PERMANENT, 0 } ,
{ 0 }
};
@@ -1144,7 +1124,7 @@ zzz
assert( JS_InitClass( cx , global , 0 , &mongo_class , local ? mongo_local_constructor : mongo_external_constructor , 0 , 0 , mongo_functions , 0 , 0 ) );
- assert( JS_InitClass( cx , global , 0 , &object_id_class , object_id_constructor , 0 , 0 , object_id_functions , 0 , 0 ) );
+ assert( JS_InitClass( cx , global , 0 , &object_id_class , object_id_constructor , 0 , 0 , 0 , 0 , 0 ) );
assert( JS_InitClass( cx , global , 0 , &db_class , db_constructor , 2 , 0 , 0 , 0 , 0 ) );
assert( JS_InitClass( cx , global , 0 , &db_collection_class , db_collection_constructor , 4 , 0 , 0 , 0 , 0 ) );
assert( JS_InitClass( cx , global , 0 , &internal_cursor_class , internal_cursor_constructor , 0 , 0 , internal_cursor_functions , 0 , 0 ) );
View
58 shell/mongo_vstudio.cpp
@@ -548,17 +548,21 @@ const StringData _jscode_raw_utils =
"ObjectId.prototype = {}\n"
"\n"
"ObjectId.prototype.toString = function(){\n"
-"return this.str;\n"
+"return \"ObjectId(\" + tojson(this.str) + \")\";\n"
"}\n"
"\n"
"ObjectId.prototype.tojson = function(){\n"
-"return \"ObjectId(\\\"\" + this.str + \"\\\")\";\n"
+"return this.toString();\n"
+"}\n"
+"\n"
+"ObjectId.prototype.valueOf = function(){\n"
+"return this.str;\n"
"}\n"
"\n"
"ObjectId.prototype.isObjectId = true;\n"
"\n"
"ObjectId.prototype.getTimestamp = function(){\n"
-"return new Date(parseInt(this.toString().slice(0,8), 16)*1000);\n"
+"return new Date(parseInt(this.valueOf().slice(0,8), 16)*1000);\n"
"}\n"
"\n"
"ObjectId.prototype.equals = function( other){\n"
@@ -574,15 +578,19 @@ const StringData _jscode_raw_utils =
"}\n"
"\n"
"DBPointer.prototype.tojson = function(indent){\n"
-"return tojson({\"ns\" : this.ns, \"id\" : this.id}, indent);\n"
+"return this.toString();\n"
"}\n"
"\n"
"DBPointer.prototype.getCollection = function(){\n"
"return this.ns;\n"
"}\n"
"\n"
+"DBPointer.prototype.getId = function(){\n"
+"return this.id;\n"
+"}\n"
+"\n"
"DBPointer.prototype.toString = function(){\n"
-"return \"DBPointer \" + this.ns + \":\" + this.id;\n"
+"return \"DBPointer(\" + tojson(this.ns) + \", \" + tojson(this.id) + \")\";\n"
"}\n"
"}\n"
"else {\n"
@@ -598,24 +606,52 @@ const StringData _jscode_raw_utils =
"}\n"
"\n"
"DBRef.prototype.tojson = function(indent){\n"
-"return tojson({\"$ref\" : this.$ref, \"$id\" : this.$id}, indent);\n"
+"return this.toString();\n"
"}\n"
"\n"
"DBRef.prototype.getCollection = function(){\n"
"return this.$ref;\n"
"}\n"
"\n"
+"DBRef.prototype.getRef = function(){\n"
+"return this.$ref;\n"
+"}\n"
+"\n"
+"DBRef.prototype.getId = function(){\n"
+"return this.$id;\n"
+"}\n"
+"\n"
"DBRef.prototype.toString = function(){\n"
-"return this.tojson();\n"
+"return \"DBRef(\" + tojson(this.$ref) + \", \" + tojson(this.$id) + \")\";\n"
"}\n"
"}\n"
"else {\n"
"print( \"warning: no DBRef\" );\n"
"}\n"
"\n"
+"if ( typeof( Timestamp ) != \"undefined\" ){\n"
+"Timestamp.prototype.tojson = function () {\n"
+"return this.toString();\n"
+"}\n"
+"\n"
+"Timestamp.prototype.getTime = function () {\n"
+"return this.t;\n"
+"}\n"
+"\n"
+"Timestamp.prototype.getInc = function () {\n"
+"return this.i;\n"
+"}\n"
+"\n"
+"Timestamp.prototype.toString = function () {\n"
+"return \"Timestamp(\" + this.t + \", \" + this.i + \")\";\n"
+"}\n"
+"}\n"
+"else {\n"
+"print( \"warning: no Timestamp class\" );\n"
+"}\n"
+"\n"
"if ( typeof( BinData ) != \"undefined\" ){\n"
"BinData.prototype.tojson = function () {\n"
-"//return \"BinData type: \" + this.type + \" len: \" + this.len;\n"
"return this.toString();\n"
"}\n"
"\n"
@@ -631,12 +667,6 @@ const StringData _jscode_raw_utils =
"print( \"warning: no BinData class\" );\n"
"}\n"
"\n"
-"/*if ( typeof( UUID ) != \"undefined\" ){\n"
-"UUID.prototype.tojson = function () {\n"
-"return this.toString();\n"
-"}\n"
-"}*/\n"
-"\n"
"if ( typeof _threadInject != \"undefined\" ){\n"
"print( \"fork() available!\" );\n"
"\n"
View
62 shell/utils.js
@@ -543,17 +543,21 @@ if ( ! ObjectId.prototype )
ObjectId.prototype = {}
ObjectId.prototype.toString = function(){
- return this.str;
+ return "ObjectId(" + tojson(this.str) + ")";
}
ObjectId.prototype.tojson = function(){
- return "ObjectId(\"" + this.str + "\")";
+ return this.toString();
+}
+
+ObjectId.prototype.valueOf = function(){
+ return this.str;
}
ObjectId.prototype.isObjectId = true;
ObjectId.prototype.getTimestamp = function(){
- return new Date(parseInt(this.toString().slice(0,8), 16)*1000);
+ return new Date(parseInt(this.valueOf().slice(0,8), 16)*1000);
}
ObjectId.prototype.equals = function( other){
@@ -569,15 +573,19 @@ if ( typeof( DBPointer ) != "undefined" ){
}
DBPointer.prototype.tojson = function(indent){
- return tojson({"ns" : this.ns, "id" : this.id}, indent);
+ return this.toString();
}
DBPointer.prototype.getCollection = function(){
return this.ns;
}
- DBPointer.prototype.toString = function(){
- return "DBPointer " + this.ns + ":" + this.id;
+ DBPointer.prototype.getId = function(){
+ return this.id;
+ }
+
+ DBPointer.prototype.toString = function(){
+ return "DBPointer(" + tojson(this.ns) + ", " + tojson(this.id) + ")";
}
}
else {
@@ -593,24 +601,52 @@ if ( typeof( DBRef ) != "undefined" ){
}
DBRef.prototype.tojson = function(indent){
- return tojson({"$ref" : this.$ref, "$id" : this.$id}, indent);
+ return this.toString();
}
DBRef.prototype.getCollection = function(){
return this.$ref;
}
+ DBRef.prototype.getRef = function(){
+ return this.$ref;
+ }
+
+ DBRef.prototype.getId = function(){
+ return this.$id;
+ }
+
DBRef.prototype.toString = function(){
- return this.tojson();
+ return "DBRef(" + tojson(this.$ref) + ", " + tojson(this.$id) + ")";
}
}
else {
print( "warning: no DBRef" );
}
+if ( typeof( Timestamp ) != "undefined" ){
+ Timestamp.prototype.tojson = function () {
+ return this.toString();
+ }
+
+ Timestamp.prototype.getTime = function () {
+ return this.t;
+ }
+
+ Timestamp.prototype.getInc = function () {
+ return this.i;
+ }
+
+ Timestamp.prototype.toString = function () {
+ return "Timestamp(" + this.t + ", " + this.i + ")";
+ }
+}
+else {
+ print( "warning: no Timestamp class" );
+}
+
if ( typeof( BinData ) != "undefined" ){
BinData.prototype.tojson = function () {
- //return "BinData type: " + this.type + " len: " + this.len;
return this.toString();
}
@@ -620,18 +656,12 @@ if ( typeof( BinData ) != "undefined" ){
BinData.prototype.length = function () {
return this.len;
- }
+ }
}
else {
print( "warning: no BinData class" );
}
-/*if ( typeof( UUID ) != "undefined" ){
- UUID.prototype.tojson = function () {
- return this.toString();
- }
-}*/
-
if ( typeof _threadInject != "undefined" ){
print( "fork() available!" );
Please sign in to comment.
Something went wrong with that request. Please try again.