LetExpression.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. var LetExpression = module.exports = function LetExpression(vars, subExpression){
  3. //if (arguments.length !== 2) throw new Error("Two args expected");
  4. this._variables = vars;
  5. this._subExpression = subExpression;
  6. }, klass = LetExpression, Expression = require("./Expression"), base = Expression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  7. Expression.registerExpression("$let", LetExpression.parse);
  8. // DEPENDENCIES
  9. var Variables = require("./Variables"),
  10. VariablesParseState = require("./VariablesParseState");
  11. // PROTOTYPE MEMBERS
  12. proto.parse = function parse(expr, vpsIn){
  13. if(!("$let" in expr)) {
  14. throw new Error("Tried to create a $let with something other than let. Looks like your parse map went all funny.");
  15. }
  16. if(typeof(expr.$let) !== 'object' || (expr.$let instanceof Array)) {
  17. throw new Error("$let only supports an object as its argument: 16874");
  18. }
  19. var args = expr.$let,
  20. varsElem = args.vars,
  21. inElem = args['in']; // args.in; ??
  22. //NOTE: DEVIATION FROM MONGO: 1. These if statements are in a loop in the c++ version,
  23. // 2. 'vars' and 'in' are each mandatory here. in the c++ code you only need one of the two.
  24. // 3. Below, we croak if there are more than 2 arguments. The original does not have this limitation, specifically.
  25. // Upon further review, I think our code is more accurate. The c++ code will accept if there are multiple 'in'
  26. // or 'var' values. The previous ones will be overwritten by newer ones.
  27. //
  28. // Final note - I think this code is fine.
  29. //
  30. if(!varsElem) {
  31. throw new Error("Missing 'vars' parameter to $let: 16876");
  32. }
  33. if(!inElem) {
  34. throw new Error("Missing 'in' parameter to $let: 16877");
  35. }
  36. // Should this be !== 2? Why would we have fewer than 2 arguments? Why do we even care what the length of the
  37. // array is? It may be an optimization of sorts. But what we're really wanting here is, 'If any keys are not "in"
  38. // or "vars" then we need to bugcheck.'
  39. if(Object.keys(args).length > 2) {
  40. var bogus = Object.keys(args).filter(function(x) {return !(x === 'in' || x === 'vars');});
  41. throw new Error("Unrecognized parameter to $let: " + bogus.join(",") + "- 16875");
  42. }
  43. var vpsSub = new VariablesParseState(vpsIn),
  44. vars = {};
  45. for(var varName in varsElem) {
  46. Variables.uassertValidNameForUserWrite(varName);
  47. var id = vpsSub.defineVariable(varName);
  48. vars[id] = {};
  49. vars[id][varName] = Expression.parseOperand(varsElem, vpsIn);
  50. }
  51. var subExpression = Expression.parseOperand(inElem, vpsSub);
  52. return new LetExpression(vars, subExpression);
  53. };
  54. proto.optimize = function optimize() {
  55. if(this._variables.empty()) {
  56. return this._subExpression.optimize();
  57. }
  58. for(var id in this._variables){
  59. for(var name in this._variables[id]) {
  60. //NOTE: DEVIATION FROM MONGO: This is actually ok. The c++ code does this with a single map. The js structure
  61. // is nested objects.
  62. this._variables[id][name] = this._variables[id][name].optimize();
  63. }
  64. }
  65. this._subExpression = this._subExpression.optimize();
  66. return this;
  67. };
  68. proto.serialize = function serialize(explain) {
  69. var vars = {};
  70. for(var id in this._variables) {
  71. for(var name in this._variables[id]) {
  72. vars[name] = this._variables[id][name];
  73. }
  74. }
  75. return {$let: {vars:vars, 'in':this._subExpression.serialize(explain)}};
  76. };
  77. proto.evaluateInternal = function evaluateInternal(vars) {
  78. for(var id in this._variables) {
  79. for(var name in this._variables[id]) {
  80. vars.setValue(id, this._variables[id][name]);
  81. }
  82. }
  83. return this._subExpression.evaluateInternal(vars);
  84. };
  85. proto.addDependencies = function addDependencies(deps, path){
  86. for(var id in this._variables) {
  87. for(var name in this._variables[id]) {
  88. this._variables[id][name].addDependencies(deps);
  89. }
  90. }
  91. this._subExpression.addDependencies(deps, path);
  92. return deps; //NOTE: DEVIATION FROM MONGO: The c++ version does not return a value. We seem to use the returned value
  93. // (or something from a different method named
  94. // addDependencies) in many places.
  95. };