Code coverage report for src/helper/jsonify/jsonify.js

Statements: 10.39% (8 / 77)      Branches: 0% (0 / 53)      Functions: 7.14% (1 / 14)      Lines: 10.67% (8 / 75)      Ignored: none     

All files » src/helper/jsonify/ » jsonify.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 1761 1 1 1   1                 1   1                                               1                                                                                                                                                                                                                                                                              
var Parser = require('../../parse/index');
var Compile = require('../../compile/index');
var fs = require('fs');
var macros = Parser.parse(fs.readFileSync(__dirname + '/macros.vm').toString());
 
function getVmText(macro, vmText, third){
  var vm = new Compile(macros);
  return vm.render({
    macro: macro,
    vmText: vmText,
    third: third
  });
}
 
module.exports = function(Velocity, utils, BLOCK_TYPES){
 
  function getPath(ast){
 
    var ret = [ast.id];
 
    utils.forEach(ast.path, function(a){
 
      var isIgnore = a.type === 'method' && a.id === 'size'; 
      var isGet = a.type === 'method' && a.id.indexOf('get') === 0 && a.args === false;
      if (isIgnore) {
        return;
      }
 
      if (a.type === 'index') {
        if (a.id && (a.id.type === 'integer' || a.id.type === 'string')) 
          ret.push(a.id.value);
      } else {
        ret.push(isGet ? a.id.slice(3) : a.id);
      }
 
    });
 
    return ret;
  }
 
  utils.mixin(Velocity.prototype, {
 
    toBasicType: function(astType){
 
      if (astType.ignore) return;
 
      var ast = astType.real;
      if (ast.type === 'foreach') ast = ast.from;
 
      if (astType.foreach === true && astType.type === 'method') {
        var isMaps = true;
      }
 
      this.setRef(ast, this.getLeaf(astType), isMaps);
 
    },
 
    setRef: function(ast, text, isMaps){
 
      var paths = getPath(ast);
      var last  = paths.pop();
      var context = this.context;
      var leafs = this.leafs;
      var len   = leafs.length;
 
      if (isMaps && (last === 'entrySet' || last === 'keySet')) 
        last = paths.pop();
 
      utils.forEach(paths, function(path){
 
        if (!context[path] || typeof context[path] === "string") {
          context[path] = {};
        }
 
        context = context[path];
 
      }, this);
 
      leafs.push(text);
      context[last] = '{@' + len + '@}';
    },
 
    toVTL: function(){
      var leafs = this.leafs;
      var context = JSON.stringify(this.context, false, 2);
 
      utils.forEach(leafs, function(leaf, i){
        var tpl = '"{@' + i + '@}"';
        context = context.replace(tpl, leaf);
      }, this);
 
      return context;
    },
 
    getLeaf: function(leaf){
      var ret = '';
      var real = leaf.real;
 
      if (!leaf.foreach) {
        if (leaf.type === 'method') {
          ret = this.getMethodCall(leaf);
        } else {
          ret = this._callMacro('string', this.getRefText(real));
        }
      } else {
        if (leaf.foreach === true && real.from) {
          ret = this.getEachVTL(leaf);
        } else {
          ret = this.getMethodInEachCall(leaf);
        }
      }
 
      return ret;
    },
 
    getMethodInEachCall: function(leaf){
      return '"function(){}"';
    },
 
    getMethodCall: function(leaf){
      return '"function(){}"';
/*
 *      var ast = leaf.real;
 *      var paths = getPath(ast);
 *      var len = ast.path.length;
 *      var last = ast.path[len - 1];
 *      var args = last.args;
 *
 *      var argText = [];
 *      utils.forEach(args, function(arg){
 *        argText.push('"' + this.getRefText(arg) + '"');
 *      }, this);
 *      argText = argText.join(', ');
 *
 *      var retText = this.getRefText(ast);
 *
 *      //console.log(getVmText('method', argText, retText));
 *      return getVmText('method', argText, retText);
 */
    },
 
    _callMacro: getVmText,
 
    getEachVTL: function(leaf){
      var real = leaf.real;
      var paths = getPath(real.from);
      var last  = paths.pop();
      var macro = 'lists';
      var vmText = this.getRefText(real.from);
 
      if (leaf.type === 'method' && last === 'entrySet') {
        macro = 'maps';
        vmText = vmText.replace(/\.entrySet\(\)$/, '');
      } else if (leaf.type === 'method' && last === 'keySet') {
        macro = 'maps';
        vmText = vmText.replace(/\.keySet\(\)$/, '');
      }
 
      return this._callMacro(macro, vmText);
    },
 
    eval: function(str){
      if (str) {
        var asts = Parser.parse(str);
        if (this instanceof Velocity){
          this._render(asts);
        } else {
          throw new Error('不能改变this指向');
        }
      }
    }
 
  });
 
};