CondExpression.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. /**
  3. * $cond expression; @see evaluate
  4. * @class CondExpression
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var CondExpression = module.exports = function CondExpression(vars) {
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. base.call(this);
  12. }, klass = CondExpression,
  13. base = require("./FixedArityExpressionT")(klass, 3),
  14. proto = klass.prototype = Object.create(base.prototype, {
  15. constructor: {
  16. value: klass
  17. }
  18. });
  19. // DEPENDENCIES
  20. var Value = require("../Value"),
  21. Expression = require("./Expression"),
  22. FixedArityExpressionT = require("./FixedArityExpressionT");
  23. // PROTOTYPE MEMBERS
  24. klass.opName = "$cond";
  25. proto.getOpName = function getOpName() {
  26. return klass.opName;
  27. };
  28. /**
  29. *
  30. * @param expr - I expect this to be the RHS of $cond:{...} or $cond:[,,,]
  31. * @param vps
  32. * @returns {*}
  33. */
  34. klass.parse = function parse(expr, vps) {
  35. // There may only be one argument - an array of 3 items, or a hash containing 3 keys.
  36. //this.checkArgLimit(3);
  37. // if not an object, return;
  38. // todo I don't understand why we'd do this. shouldn't expr be {}, [], or wrong?
  39. if (typeof(expr) !== Object || )
  40. return FixedArityExpressionT.parse(expr, vps);
  41. // ...or expr could be the entirety of $cond:{...} or $cond:[,,,].
  42. if(!(klass.opName in expr)) {
  43. throw new Error("Invalid expression. Expected to see '"+klass.opName+"'");
  44. }
  45. var ret = new CondExpression();
  46. // If this is an Object and not an array, verify all the bits are specified.
  47. // If this is an Object that is an array, verify there are three bits.
  48. // (My issue here is that we got to this parse function when we parsed the $cond:{...} item, and we're calling
  49. // parseOperand (again) without altering the input.)
  50. // var args = Expression.parseOperand(expr, vps);
  51. var args = expr[getOpName()];
  52. if (typeof args !== 'object') throw new Error("this should not happen");
  53. if (args instanceof Array) {
  54. // it's the array form. Convert it to the object form.
  55. if (args.length !== 3) throw new Error("$cond requires exactly three arguments");
  56. args = {if: args[0], then: args[1], else: args[2]};
  57. }
  58. // One way or the other, args is now in object form.
  59. Object.keys(args).forEach(function(arg) {
  60. if (arg === 'if') {
  61. ret.operands[0] = Expression.parseOperand(args['if'], vps);
  62. }
  63. else if (arg === 'then') {
  64. ret.operands[1] = Expression.parseOperand(args['then'], vps);
  65. }
  66. else if (arg === 'else') {
  67. ret.operands[2] = Expression.parseOperand(args['else'], vps);
  68. }
  69. else {
  70. throw new Error("Unrecognized parameter to $cond: '" + arg + "'; code 17083");
  71. }
  72. });
  73. if (!ret.operands[0]) throw new Error("Missing 'if' parameter to $cond; code 17080");
  74. if (!ret.operands[1]) throw new Error("Missing 'then' parameter to $cond; code 17081");
  75. if (!ret.operands[2]) throw new Error("Missing 'else' parameter to $cond; code 17082");
  76. return ret;
  77. };
  78. /**
  79. * Use the $cond operator with the following syntax:
  80. * { $cond: { if: <boolean-expression>, then: <true-case>, else: <false-case-> } }
  81. * -or-
  82. * { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
  83. * @method evaluate
  84. **/
  85. proto.evaluateInternal = function evaluateInternal(vars) {
  86. var pCond1 = this.operands[0].evaluateInternal(vars);
  87. this.idx = 0;
  88. if (pCond1.coerceToBool()) {
  89. this.idx = 1;
  90. } else {
  91. this.idx = 2;
  92. }
  93. return this.operands[this.idx].evaluateInternal(vars);
  94. };
  95. /** Register Expression */
  96. Expression.registerExpression(klass.opName, klass.parse);