|
|
@@ -29,6 +29,13 @@ proto.getOpName = function getOpName() {
|
|
|
return klass.opName;
|
|
|
};
|
|
|
|
|
|
+/**
|
|
|
+ * A utility class. Not in the c++ code.
|
|
|
+ * @param v a value that should be tested for being null-ish.
|
|
|
+ * @returns {boolean}
|
|
|
+ */
|
|
|
+klass.isNullish = function(op) {return (op.value == null || op.value == undefined) && !op._fieldPath};
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
* @param expr - I expect this to be the RHS of $cond:{...} or $cond:[,,,]
|
|
|
@@ -41,13 +48,14 @@ klass.parse = function parse(expr, vps) {
|
|
|
|
|
|
// if not an object, return;
|
|
|
// todo I don't understand why we'd do this. shouldn't expr be {}, [], or wrong?
|
|
|
- if (typeof(expr) !== Object || )
|
|
|
+ if (typeof(expr) !== 'object')
|
|
|
return FixedArityExpressionT.parse(expr, vps);
|
|
|
|
|
|
- // ...or expr could be the entirety of $cond:{...} or $cond:[,,,].
|
|
|
- if(!(klass.opName in expr)) {
|
|
|
- throw new Error("Invalid expression. Expected to see '"+klass.opName+"'");
|
|
|
- }
|
|
|
+//NOTE: Deviation from Mongo: The c++ code has a verify( $cond ) structure. The implementation below will never work.
|
|
|
+// // ...or expr could be the entirety of $cond:{...} or $cond:[,,,].
|
|
|
+// if(!(klass.opName in expr)) {
|
|
|
+// throw new Error("Invalid expression. Expected to see '"+klass.opName+"'");
|
|
|
+// }
|
|
|
|
|
|
var ret = new CondExpression();
|
|
|
|
|
|
@@ -57,7 +65,8 @@ klass.parse = function parse(expr, vps) {
|
|
|
// parseOperand (again) without altering the input.)
|
|
|
// var args = Expression.parseOperand(expr, vps);
|
|
|
|
|
|
- var args = expr[getOpName()];
|
|
|
+// var args = expr[klass.opName];
|
|
|
+ var args = expr;
|
|
|
|
|
|
if (typeof args !== 'object') throw new Error("this should not happen");
|
|
|
if (args instanceof Array) {
|
|
|
@@ -82,9 +91,9 @@ klass.parse = function parse(expr, vps) {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- if (!ret.operands[0]) throw new Error("Missing 'if' parameter to $cond; code 17080");
|
|
|
- if (!ret.operands[1]) throw new Error("Missing 'then' parameter to $cond; code 17081");
|
|
|
- if (!ret.operands[2]) throw new Error("Missing 'else' parameter to $cond; code 17082");
|
|
|
+ if (klass.isNullish(ret.operands[0])) throw new Error("Missing 'if' parameter to $cond; code 17080");
|
|
|
+ if (klass.isNullish(ret.operands[1])) throw new Error("Missing 'then' parameter to $cond; code 17081");
|
|
|
+ if (klass.isNullish(ret.operands[2])) throw new Error("Missing 'else' parameter to $cond; code 17082");
|
|
|
|
|
|
return ret;
|
|
|
};
|
|
|
@@ -100,7 +109,7 @@ proto.evaluateInternal = function evaluateInternal(vars) {
|
|
|
var pCond1 = this.operands[0].evaluateInternal(vars);
|
|
|
|
|
|
this.idx = 0;
|
|
|
- if (pCond1.coerceToBool()) {
|
|
|
+ if (Value.coerceToBool(pCond1)) {
|
|
|
this.idx = 1;
|
|
|
} else {
|
|
|
this.idx = 2;
|