|
@@ -1,64 +1,60 @@
|
|
|
"use strict";
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
+var Value = require("../Value"),
|
|
|
|
|
+ Expression = require("./Expression");
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Internal expression for constant values
|
|
* Internal expression for constant values
|
|
|
* @class ConstantExpression
|
|
* @class ConstantExpression
|
|
|
* @namespace mungedb-aggregate.pipeline.expressions
|
|
* @namespace mungedb-aggregate.pipeline.expressions
|
|
|
* @module mungedb-aggregate
|
|
* @module mungedb-aggregate
|
|
|
* @constructor
|
|
* @constructor
|
|
|
- **/
|
|
|
|
|
|
|
+ */
|
|
|
var ConstantExpression = module.exports = function ConstantExpression(value){
|
|
var ConstantExpression = module.exports = function ConstantExpression(value){
|
|
|
- if (arguments.length !== 1) throw new Error("args expected: value");
|
|
|
|
|
- this.value = value; //TODO: actually make read-only in terms of JS?
|
|
|
|
|
|
|
+ if (arguments.length !== 1) throw new Error(klass.name + ": args expected: value");
|
|
|
|
|
+ this.value = value;
|
|
|
base.call(this);
|
|
base.call(this);
|
|
|
-}, klass = ConstantExpression, base = require("./Expression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-// DEPENDENCIES
|
|
|
|
|
-var Value = require("../Value"),
|
|
|
|
|
- Expression = require("./Expression");
|
|
|
|
|
|
|
+}, klass = ConstantExpression, base = require("./FixedArityExpressionT")(klass,1), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
|
|
|
|
|
|
|
|
-// PROTOTYPE MEMBERS
|
|
|
|
|
-proto.getOpName = function getOpName(){
|
|
|
|
|
- return "$const";
|
|
|
|
|
|
|
+klass.parse = function parse(exprElement, vps) {
|
|
|
|
|
+ return new ConstantExpression(exprElement);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Get the constant value represented by this Expression.
|
|
|
|
|
- * @method getValue
|
|
|
|
|
- * @returns the value
|
|
|
|
|
- **/
|
|
|
|
|
-proto.getValue = function getValue(){ //TODO: convert this to an instance field rather than a property
|
|
|
|
|
- return this.value;
|
|
|
|
|
|
|
+klass.create = function create(value) {
|
|
|
|
|
+ var constExpr = new ConstantExpression(value);
|
|
|
|
|
+ return constExpr;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-proto.addDependencies = function addDependencies(deps, path) {
|
|
|
|
|
|
|
+proto.optimize = function optimize() {
|
|
|
// nothing to do
|
|
// nothing to do
|
|
|
|
|
+ return this;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-klass.parse = function parse(expr, vps){
|
|
|
|
|
- return new ConstantExpression(expr);
|
|
|
|
|
|
|
+proto.addDependencies = function addDependencies(deps, path) {
|
|
|
|
|
+ // nothing to do
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Get the constant value represented by this Expression.
|
|
* Get the constant value represented by this Expression.
|
|
|
* @method evaluate
|
|
* @method evaluate
|
|
|
- **/
|
|
|
|
|
-proto.evaluateInternal = function evaluateInternal(vars){
|
|
|
|
|
|
|
+ */
|
|
|
|
|
+proto.evaluateInternal = function evaluateInternal(vars) {
|
|
|
return this.value;
|
|
return this.value;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-proto.optimize = function optimize() {
|
|
|
|
|
- return this; // nothing to do
|
|
|
|
|
-};
|
|
|
|
|
|
|
+/// Helper function to easily wrap constants with $const.
|
|
|
|
|
+function serializeConstant(val) {
|
|
|
|
|
+ return {$const: val};
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
-proto.serialize = function(rawValue){
|
|
|
|
|
- return rawValue ? {$const: this.value} : this.value;
|
|
|
|
|
|
|
+proto.serialize = function serialize(explain) {
|
|
|
|
|
+ return serializeConstant(this.value);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-//TODO: proto.addToBsonObj --- may be required for $project to work -- my hope is that we can implement toJSON methods all around and use that instead
|
|
|
|
|
-//TODO: proto.addToBsonArray
|
|
|
|
|
|
|
+Expression.registerExpression("$const", klass.parse);
|
|
|
|
|
+
|
|
|
|
|
+Expression.registerExpression("$literal", klass.parse); // alias
|
|
|
|
|
|
|
|
-/** Register Expression */
|
|
|
|
|
-Expression.registerExpression("$const",klass.parse(ConstantExpression));
|
|
|
|
|
-Expression.registerExpression("$literal", klass.parse(ConstantExpression)); // alias
|
|
|
|
|
|
|
+proto.getOpName = function getOpName() {
|
|
|
|
|
+ return "$const";
|
|
|
|
|
+};
|