Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.mongodb.mongosh.result.*
import com.mongodb.mongosh.service.Either
import com.mongodb.mongosh.service.Left
import com.mongodb.mongosh.service.Right
import org.bson.BsonTimestamp
import org.bson.Document
import org.bson.json.JsonReader
import org.bson.types.*
Expand All @@ -22,11 +23,22 @@ internal class MongoShellConverter(private val context: MongoShellContext, priva

fun toJs(o: Any?): Any? {
return when (o) {
is Iterable<*> -> toJs(o)
is Array<*> -> toJs(o)
is Map<*, *> -> toJs(o)
Unit -> context.eval("undefined")
else -> o
is Iterable<*> -> toJs(o)
is Array<*> -> toJs(o)
is Map<*, *> -> toJs(o)
is Unit -> context.eval("undefined")
is MaxKey -> bsonTypes.maxKey.newInstance()
is MinKey -> bsonTypes.minKey.newInstance()
is Binary -> bsonTypes.binData.newInstance(toJs(o.data), o.type)
is Symbol -> bsonTypes.bsonSymbol.newInstance(o.symbol)
is Decimal128 -> bsonTypes.numberDecimal.newInstance(o.bigDecimalValue().toPlainString())
is Long -> bsonTypes.numberLong.newInstance(o.toString())
is Int -> bsonTypes.numberInt.newInstance(o.toString())
is BsonTimestamp -> bsonTypes.timestamp.newInstance(o.inc, o.time)
is CodeWithScope -> bsonTypes.code.newInstance(o.code, toJs(o.scope))
is Code -> bsonTypes.code.newInstance(o.code)
is DBRef -> bsonTypes.dbRef.newInstance(o.collectionName, toJs(o.id), o.databaseName)
else -> o
}
}

Expand All @@ -47,6 +59,14 @@ internal class MongoShellConverter(private val context: MongoShellContext, priva
return array
}

private fun toJs(array: ByteArray): Value {
val jsArray = context.eval("[]")
array.forEachIndexed { index, v ->
jsArray.setArrayElement(index.toLong(), v)
}
return jsArray
}

private fun toJs(list: Array<*>): Value {
val array = context.eval("[]")
list.forEachIndexed { index, v ->
Expand Down Expand Up @@ -96,25 +116,26 @@ internal class MongoShellConverter(private val context: MongoShellContext, priva
// document with aggregation explain result also has type AggregationCursor, so we need to make sure that value contains cursor
type == "AggregationCursor" && v.hasMember("_cursor") -> CursorResult(Cursor<Any?>(v, this))
type == "InsertOneResult" -> InsertOneResult(v["acknowledged"]!!.asBoolean(), v["insertedId"]!!.asString())
type == "DeleteResult" -> DeleteResult(v["acknowledged"]!!.asBoolean(), v["deletedCount"]!!.asLong())
type == "DeleteResult" -> DeleteResult(v["acknowledged"]!!.asBoolean(), (toJava(v["deletedCount"]!!) as LongResult).value)
type == "UpdateResult" -> {
val res = if (v["acknowledged"]!!.asBoolean()) {
UpdateResult.acknowledged(
v["matchedCount"]!!.asLong(),
v["modifiedCount"]!!.asLong(),
null
(toJava(v["matchedCount"]!!) as LongResult).value,
(toJava(v["modifiedCount"]!!) as LongResult).value,
null
)
} else UpdateResult.unacknowledged()
MongoShellUpdateResult(res)
}
type == "BulkWriteResult" -> BulkWriteResult(
v["acknowledged"]!!.asBoolean(),
v["insertedCount"]!!.asLong(),
v["matchedCount"]!!.asLong(),
v["modifiedCount"]!!.asLong(),
v["deletedCount"]!!.asLong(),
v["upsertedCount"]!!.asLong(),
(toJava(v["upsertedIds"]!!) as ArrayResult).value)
v["acknowledged"]!!.asBoolean(),
(toJava(v["insertedCount"]!!) as IntResult).value,
(toJava(v["matchedCount"]!!) as IntResult).value,
(toJava(v["modifiedCount"]!!) as IntResult).value,
(toJava(v["deletedCount"]!!) as IntResult).value,
(toJava(v["upsertedCount"]!!) as IntResult).value,
(toJava(v["upsertedIds"]!!) as ArrayResult).value
)
type == "InsertManyResult" -> InsertManyResult(v["acknowledged"]!!.asBoolean(), toJava(v["insertedIds"]!!).value as List<String>)
v.instanceOf(context, "RegExp") -> {
val pattern = v["source"]!!.asString()
Expand Down Expand Up @@ -143,7 +164,7 @@ internal class MongoShellConverter(private val context: MongoShellContext, priva
v.instanceOf(context, bsonTypes.dbRef) -> {
val databaseName = v["db"]?.let { if (it.isNull) null else it }?.asString()
val collectionName = v["collection"]!!.asString()
val value = toJava(v["oid"]!!).value
val value = toJava(v["oid"]!!).value!!
DBRefResult(DBRef(databaseName, collectionName, value))
}
v.instanceOf(context, bsonTypes.numberLong) -> LongResult(JsonReader(v.invokeMember("toExtendedJSON").toString()).readInt64())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ class DeleteResult(val acknowledged: Boolean, val deletedCount: Long) : MongoShe
}

class BulkWriteResult(val acknowledged: Boolean,
val insertedCount: Long,
val matchedCount: Long,
val modifiedCount: Long,
val deletedCount: Long,
val upsertedCount: Long,
val insertedCount: Int,
val matchedCount: Int,
val modifiedCount: Int,
val deletedCount: Int,
val upsertedCount: Int,
val upsertedIds: List<Any?>) : MongoShellResult<Map<String, Any>>() {
override val value: Map<String, Any>
get() = mapOf("acknowledged" to acknowledged,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,14 @@ internal class Cursor(private var helper: BaseMongoIterableHelper<*>, private va

@HostAccess.Export
override fun next(): Any? {
/* findOne returns single document as a result.
* Mongosh core will try to defineProperty on it and fail if value is not wrapped in JS object */
val shouldWrap = iterator == null && helper.limit() == 1
val value = getOrCreateIterator().next()
return if (shouldWrap) wrapper.wrap(value) // it's possibly a findOne call
else value
return converter.toJs(value)
}

@HostAccess.Export
override fun tryNext(): Any? {
/* findOne returns single document as a result.
* Mongosh core will try to defineProperty on it and fail if value is not wrapped in JS object */
val shouldWrap = iterator == null && helper.limit() == 1
val value = getOrCreateIterator().tryNext()
return if (shouldWrap) wrapper.wrap(value) // it's possibly a findOne call
else value
return converter.toJs(value)
}

@HostAccess.Export
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{ "acknowledged": true, "insertedId": "UNKNOWN" }
true
[ { "_id": <ObjectID>, "a": 1, "objectId": <ObjectID>, "maxKey": {"$maxKey": 1}, "minKey": {"$minKey": 1}, "binData": {"$binary": {"base64": "MTIzNA==", "subType": "10"}}, "date": {"$date": {"$numberLong": "1355875200000"}}, "isoDate": {"$date": {"$numberLong": "1355875200000"}}, "numberInt": 24, "timestamp": Timestamp{value=429496729600, seconds=100, inc=0}, "undefined": null, "null": null, "uuid": <UUID> } ]
[ { "_id": <ObjectID>, "a": 1, "objectId": <ObjectID>, "maxKey": {"$maxKey": 1}, "minKey": {"$minKey": 1}, "binData": {"$binary": {"base64": "MTIzNA==", "subType": "10"}}, "date": {"$date": {"$numberLong": "1355875200000"}}, "isoDate": {"$date": {"$numberLong": "1355875200000"}}, "numberInt": 24, "timestamp": {"$timestamp": {"t": 100, "i": 0}}, "undefined": null, "null": null, "uuid": <UUID> } ]
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{ "_id": <ObjectID>, "name": "value1", "v": 1 }
{ "_id": <ObjectID>, "name": "value1", "v": 1 }
value1
2 changes: 2 additions & 0 deletions packages/java-shell/src/test/resources/cursor/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ db.coll.insertOne({name: "value2", v: 2});
db.coll.insertOne({name: "value2", v: 3});
// command
db.coll.find().next();
// command
db.coll.find().next().name;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
SymbolResult: a
SymbolResult: b
SymbolResult: b
SymbolResult: c
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// before
db.coll.insertOne({"_id": 1, v: new BSONSymbol('c')});
// command checkResultClass
BSONSymbol('a')
// command checkResultClass
new BSONSymbol('b')
new BSONSymbol('b')
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "10"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "10"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "04"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "04"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "10"}}
8 changes: 7 additions & 1 deletion packages/java-shell/src/test/resources/literal/BinData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// before
db.coll.insertOne({"_id": 1, v: new BinData(16, 'MTIzNA==')})
// command checkResultClass
new BinData(16, 'MTIzNA==')
// command checkResultClass
BinData(16, 'MTIzNA==')
// command checkResultClass
new BinData(4, 'MTIzNA==')
new BinData(4, 'MTIzNA==')
// command checkResultClass
db.coll.find().toArray()[0].v
// clear
db.coll.drop()
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
CodeWithScopeResult: {"$code": "code"}
CodeResult: {"$code": "code"}
CodeResult: {"$code": "code"}
CodeWithScopeResult: {"$code": "code"}
6 changes: 6 additions & 0 deletions packages/java-shell/src/test/resources/literal/Code.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// before
db.coll.insertOne({"_id": 1, v: Code('code', { k: 'v' })});
// command checkResultClass
Code('code', { k: 'v' })
// command checkResultClass
Code('code')
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DBRefResult: { "$ref" : "namespace", "$id" : "oid" }
DBRefResult: { "$ref" : "namespace", "$id" : "oid" }
DBRefResult: { "$ref" : "namespace", "$id" : "oid" }
8 changes: 7 additions & 1 deletion packages/java-shell/src/test/resources/literal/DBRef.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
// before
db.coll.insertOne({"_id": 1, v: new DBRef('namespace', 'oid')});
// command checkResultClass
new DBRef('namespace', 'oid')
new DBRef('namespace', 'oid')
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ DateResult: {"$date": {"$numberLong": "1355896877171"}}
DateResult: {"$date": {"$numberLong": "1355875200000"}}
StringResult
java.lang.IllegalArgumentException: Expected number or string. Got: {} (Document{{}})
LongResult: 1355875200000
LongResult: 1355875200000
DateResult: {"$date": {"$numberLong": "1355875200000"}}
8 changes: 7 additions & 1 deletion packages/java-shell/src/test/resources/literal/Date.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// before
db.coll.insertOne({"_id": 1, v: new Date("2012-12-19")});
// command dontCheckValue
new Date()
// command dontCheckValue
Expand All @@ -19,4 +21,8 @@ Date("2012-12-19")
// command checkResultClass
new Date({})
// command checkResultClass
new Date("2012-12-19").getTime()
new Date("2012-12-19").getTime()
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "10"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "10"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "10"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "10"}}
6 changes: 6 additions & 0 deletions packages/java-shell/src/test/resources/literal/HexData.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// before
db.coll.insertOne({"_id": 1, v: new HexData(16, '31323334')});
// command checkResultClass
new HexData(16, '31323334')
// command checkResultClass
HexData(16, '31323334')
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ DateResult: {"$date": {"$numberLong": "1355875200000"}}
DateResult: {"$date": {"$numberLong": "1355875200000"}}
DateResult: {"$date": {"$numberLong": "1355925905000"}}
DateResult: {"$date": {"$numberLong": "1355925905000"}}
DateResult: {"$date": {"$numberLong": "1355925905000"}}
DateResult: {"$date": {"$numberLong": "1355925905000"}}
DateResult: {"$date": {"$numberLong": "1355925905000"}}
8 changes: 7 additions & 1 deletion packages/java-shell/src/test/resources/literal/ISODate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// before
db.coll.insertOne({"_id": 1, v: new ISODate('2012-12-19T140505')});
// command dontCheckValue
ISODate()
// command dontCheckValue
Expand Down Expand Up @@ -25,4 +27,8 @@ ISODate('20121219T14:05:05')
// command checkResultClass
ISODate('20121219T140505')
// command checkResultClass
ISODate('2012-12-19T140505')
ISODate('2012-12-19T140505')
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "05"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "05"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "05"}}
BinaryResult: {"$binary": {"base64": "MTIzNA==", "subType": "05"}}
8 changes: 7 additions & 1 deletion packages/java-shell/src/test/resources/literal/MD5.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// before
db.coll.insertOne({"_id": 1, v: new MD5('31323334')});
// command checkResultClass
new MD5('31323334')
// command checkResultClass
MD5('31323334')
MD5('31323334')
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MaxKeyResult: {"$maxKey": 1}
MaxKeyResult: {"$maxKey": 1}
MaxKeyResult: {"$maxKey": 1}
MaxKeyResult: {"$maxKey": 1}
8 changes: 7 additions & 1 deletion packages/java-shell/src/test/resources/literal/MaxKey.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// before
db.coll.insertOne({"_id": 1, v: new MaxKey()});
// command checkResultClass
new MaxKey();
// command checkResultClass
MaxKey()
MaxKey()
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MinKeyResult: {"$minKey": 1}
MinKeyResult: {"$minKey": 1}
MinKeyResult: {"$minKey": 1}
MinKeyResult: {"$minKey": 1}
8 changes: 7 additions & 1 deletion packages/java-shell/src/test/resources/literal/MinKey.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// before
db.coll.insertOne({"_id": 1, v: new MinKey()});
// command checkResultClass
new MinKey();
// command checkResultClass
MinKey()
MinKey()
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Decimal128Result: {"$numberDecimal": "24"}
Decimal128Result: {"$numberDecimal": "24"}
Decimal128Result: {"$numberDecimal": "24"}
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
// before
db.coll.insertOne({"_id": 1, v: new NumberDecimal("24")});
// command checkResultClass
new NumberDecimal("24")
new NumberDecimal("24")
// command checkResultClass
db.coll.find().toArray()[0].v;
// clear
db.coll.drop();
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
IntResult: 24
IntResult: 24
IntResult: 24
IntResult: 24
Loading