emitModelBody()

in lib/model_generator.js [305:451]


  emitModelBody(ast, modelName, level) {
    assert.equal(ast.type, 'modelBody');
    let node;
    for (let i = 0; i < ast.nodes.length; i++) {
      node = ast.nodes[i];
      let comments = DSL.comment.getFrontComments(this.ctx.comments, node.tokenRange[0]);
      this.visitComments(comments, level);
      const value = node.fieldValue;
      const realName = getAttr(node, 'name') || _name(node.fieldName);
      const description = getAttr(node, 'description');
      const position = getAttr(node, 'position');
      const example = getAttr(node, 'example');
      const checkBlank = getAttr(node, 'checkBlank');
      const nullable = getAttr(node, 'nullable');
      const sensitive = getAttr(node, 'sensitive');
      const pattern = getAttr(node, 'pattern') || '';
      const maxLength = getAttr(node, 'maxLength') || 0;
      const minLength = getAttr(node, 'minLength') || 0;
      const maximum = getAttr(node, 'maximum') || 0;
      const minimum = getAttr(node, 'minimum') || 0;
      const required = node.required || false;
      const deprecated = getAttr(node, 'deprecated');
      const parentIgnore = getAttr(node, 'parentIgnore');
      let hasNextSection = false;
      let str = '';
      if (description) {
        const descriptions = md2Html(description).trimEnd().split('\n');
        for (let j = 0; j < descriptions.length; j++) {
          str += ` * ${descriptions[j]}\n`;
        }
        hasNextSection = true;
      }
      if (example) {
        if (hasNextSection) {
          str += ' * \n';
        }
        const examples = md2Html(example).trimEnd().split('\n');
        str += ' * <strong>example:</strong>\n';
        for (let j = 0; j < examples.length; j++) {
          str += ` * ${examples[j]}\n`;
        }
        hasNextSection = true;
      }
      if (typeof checkBlank !== 'undefined') {
        if (hasNextSection) {
          str += ' * \n';
        }
        str += ' * <strong>check if is blank:</strong>\n';
        str += ` * <p>${checkBlank}</p>\n`;
        hasNextSection = true;
      }
      if (typeof nullable !== 'undefined') {
        if (hasNextSection) {
          str += ' * \n';
        }
        str += ' * <strong>if can be null:</strong>\n';
        str += ` * <p>${nullable}</p>\n`;
        hasNextSection = true;
      }
      if (typeof sensitive !== 'undefined') {
        if (hasNextSection) {
          str += ' * \n';
        }
        str += ' * <strong>if sensitive:</strong>\n';
        str += ` * <p>${sensitive}</p>\n`;
      }
      if (description) {
        this.ctx.descriptionMap[realName] = str.trimEnd().split('\n');
      }
      if (position) {
        let pos = position.split(',');
        for (let i = 0; i < pos.length; i++) {
          this.emitln(`@com.aliyun.core.annotation.${pos[i]}`, level);
        }
      }
      if (parentIgnore) {
        this.emitln(`@com.aliyun.core.annotation.ParentIgnore("${parentIgnore}")`, level);
      }
      this.emitln(`@com.aliyun.core.annotation.NameInMap("${realName}")`, level);
      if (deprecated === 'true') {
        this.emitln(`@Deprecated`, level);
      }
      if (required || maxLength > 0 || maximum > 0 || pattern !== '') {
        var validationAnnotation = '@com.aliyun.core.annotation.Validation(';
        if (required) {
          validationAnnotation += `required = ${required}`;
        }
        if (pattern !== '') {
          if (!validationAnnotation.endsWith('(')) {
            validationAnnotation += ', ';
          }
          validationAnnotation += `pattern = "${pattern}"`;
        }
        // 不能超过Java中Integer最大值
        if (maxLength > 0 && maxLength <= 2147483647) {
          if (!validationAnnotation.endsWith('(')) {
            validationAnnotation += ', ';
          }
          validationAnnotation += `maxLength = ${maxLength}`;
        }
        // 不能超过Java中Integer最大值
        if (minLength > 0 && minLength <= 2147483647) {
          if (!validationAnnotation.endsWith('(')) {
            validationAnnotation += ', ';
          }
          validationAnnotation += `minLength = ${minLength}`;
        }
        // 不能超过JS中最大安全整数
        if (maximum > 0 && maximum <= Number.MAX_SAFE_INTEGER) {
          if (!validationAnnotation.endsWith('(')) {
            validationAnnotation += ', ';
          }
          validationAnnotation += `maximum = ${maximum}`;
          if (maximum > 2147483647) {
            validationAnnotation += 'D';
          }
        }
        // 不能超过JS中最大安全整数
        if (minimum > 0 && minimum <= Number.MAX_SAFE_INTEGER) {
          if (!validationAnnotation.endsWith('(')) {
            validationAnnotation += ', ';
          }
          validationAnnotation += `minimum = ${minimum}`;
          if (minimum > 2147483647) {
            validationAnnotation += 'D';
          }
        }
        this.emit(validationAnnotation, level);
        this.emitln(')');
      }
      this.emit('private ', level);
      this.visitFieldType(value, node, modelName);
      this.emitln(`${avoidReserveName(_name(node.fieldName))};`);
      this.emitln();
    }
    if (node) {
      //find the last node's back comment
      let comments = DSL.comment.getBetweenComments(this.ctx.comments, node.tokenRange[0], ast.tokenRange[1]);
      this.visitComments(comments, level);
    }

    if (ast.nodes.length === 0) {
      //empty block's comment
      let comments = DSL.comment.getBetweenComments(this.ctx.comments, ast.tokenRange[0], ast.tokenRange[1]);
      this.visitComments(comments, level);
    }
  }