Просмотр исходного кода

Refs #2165: implement sourceToJson(), toJson() as necessary.

Charles Ezell 13 лет назад
Родитель
Сommit
2fffae183b

+ 7 - 0
lib/pipeline/accumulators/Accumulator.js

@@ -33,6 +33,13 @@ var Accumulator = module.exports = (function(){
 		base.prototype.addOperand.call(this, expr);
 		base.prototype.addOperand.call(this, expr);
 	};
 	};
 
 
+	proto.toJson = function toJson(isExpressionRequired){
+		var rep = {};
+		rep[this.getOpName()] = this.operands[0].toJson(isExpressionRequired);
+
+		return rep;
+	};
+
 	/**
 	/**
 	*   Convenience method for doing this for accumulators.  The pattern
 	*   Convenience method for doing this for accumulators.  The pattern
 	*   is always the same, so a common implementation works, but requires
 	*   is always the same, so a common implementation works, but requires

+ 29 - 1
lib/pipeline/documentSources/GroupDocumentSource.js

@@ -18,7 +18,7 @@ var GroupDocumentSource = module.exports = (function(){
 	 * @constructor
 	 * @constructor
 	 * @param	{ExpressionContext}	
 	 * @param	{ExpressionContext}	
 	**/
 	**/
-	var klass = module.exports = GroupDocumentSource = function GroupDocumentSource(){
+	var klass = module.exports = GroupDocumentSource = function GroupDocumentSource(/*pExpCtx*/){
 
 
 		this.populated = false;
 		this.populated = false;
 		this.idExpression = null;
 		this.idExpression = null;
@@ -44,6 +44,34 @@ var GroupDocumentSource = module.exports = (function(){
 		"$sum": Accumulators.Sum
 		"$sum": Accumulators.Sum
 	};
 	};
 
 
+	/**
+	 * Create an object that represents the document source.  The object
+	 * will have a single field whose name is the source's name.  This
+	 * will be used by the default implementation of addToJsonArray()
+	 * to add this object to a pipeline being represented in JSON.
+	 *
+	 * @method	sourceToJson
+	 * @param	{Object} builder	JSONObjBuilder: a blank object builder to write to
+	 * @param	{Boolean}	explain	create explain output
+	**/
+	proto.sourceToJson = function sourceToJson(builder, explain) {
+		var idExp = this.idExpression,
+			insides = {
+				_id: idExp ? idExp.toJson() : {}
+			},
+			aFac = this.accumulatorFactories,
+			aFacLen = aFac.length;
+
+		for(var i=0; i < aFacLen; ++i) {
+			var acc = new aFac[i](/*pExpCtx*/);
+			acc.addOperand(this.expressions[i]);
+
+			insides[this.fieldNames[i]] = acc.toJson(true);
+		}
+
+		builder[this.getSourceName()] = insides;
+	};
+
     klass.createFromJson = function createFromJson(groupObj) {
     klass.createFromJson = function createFromJson(groupObj) {
 
 
 		if(!(groupObj instanceof Object && groupObj.constructor.name === "Object"))
 		if(!(groupObj instanceof Object && groupObj.constructor.name === "Object"))

+ 7 - 0
lib/pipeline/documentSources/ProjectDocumentSource.js

@@ -85,6 +85,13 @@ var ProjectDocumentSource = module.exports = (function(){
 		this.EO.optimize();
 		this.EO.optimize();
     };
     };
 
 
+	proto.toJSON = function toJSON(){
+		var obj = {};
+		this.sourceToJson(obj);
+
+		return obj;
+	};
+
 	/**
 	/**
 	 * Places a $project key inside the builder object with value of this.EO
 	 * Places a $project key inside the builder object with value of this.EO
 	 *
 	 *

+ 1 - 1
lib/pipeline/expressions/ObjectExpression.js

@@ -289,7 +289,7 @@ var ObjectExpression = module.exports = (function(){
 //TODO: proto.addToBsonArray = ...?
 //TODO: proto.addToBsonArray = ...?
 
 
 //NOTE: in `munge` we're not passing the `Object`s in and allowing `toJson` (was `documentToBson`) to modify it directly and are instead building and returning a new `Object` since that's the way it's actually used
 //NOTE: in `munge` we're not passing the `Object`s in and allowing `toJson` (was `documentToBson`) to modify it directly and are instead building and returning a new `Object` since that's the way it's actually used
-proto.toJson = function toJson(requireExpression){	//TODO: requireExpression doesn't seem to really get used in the mongo code; remove it?
+proto.toJson = function toJson(requireExpression){
 	var o = {};
 	var o = {};
 	if(this.excludeId)
 	if(this.excludeId)
 		o[Document.ID_PROPERTY_NAME] = false;
 		o[Document.ID_PROPERTY_NAME] = false;

+ 16 - 1
test/lib/pipeline/documentSources/GroupDocumentSource.js

@@ -5,6 +5,16 @@ var assert = require("assert"),
 	GroupDocumentSource = require("../../../../lib/pipeline/documentSources/GroupDocumentSource");
 	GroupDocumentSource = require("../../../../lib/pipeline/documentSources/GroupDocumentSource");
 
 
 
 
+/**
+*   Tests if the given spec is the same as what the DocumentSource resolves to as JSON.
+*   MUST CALL WITH A DocumentSource AS THIS (e.g. checkJsonRepresentation.call(this, spec) where this is a DocumentSource and spec is the JSON used to create the source).
+**/
+var checkJsonRepresentation = function checkJsonRepresentation(self, spec) {
+    var rep = {};
+    self.sourceToJson(rep, true);
+    assert.deepEqual(rep, {$group:spec});
+};
+
 /// An assertion for `ObjectExpression` instances based on Mongo's `ExpectedResultBase` class
 /// An assertion for `ObjectExpression` instances based on Mongo's `ExpectedResultBase` class
 function assertExpectedResult(args) {
 function assertExpectedResult(args) {
 	{// check for required args
 	{// check for required args
@@ -23,6 +33,7 @@ function assertExpectedResult(args) {
 		gds.setSource(cds);
 		gds.setSource(cds);
 		var result = gds.getCurrent();
 		var result = gds.getCurrent();
 		assert.deepEqual(result, args.expected);
 		assert.deepEqual(result, args.expected);
+		checkJsonRepresentation(gds, args.spec);
 	}else{
 	}else{
 		if(args.throw)
 		if(args.throw)
 			assert.throws(function(){
 			assert.throws(function(){
@@ -30,8 +41,11 @@ function assertExpectedResult(args) {
 			});
 			});
 		else
 		else
 			assert.doesNotThrow(function(){
 			assert.doesNotThrow(function(){
-				GroupDocumentSource.createFromJson(args.spec);
+				var gds = GroupDocumentSource.createFromJson(args.spec);
+				checkJsonRepresentation(gds, args.spec);
 			});
 			});
+
+
 	}
 	}
 }
 }
 
 
@@ -188,6 +202,7 @@ module.exports = {
 				});
 				});
 			}
 			}
 		}
 		}
+
 	}
 	}
 
 
 };
 };