visitModelBody()

in lib/generator.js [1505:1624]


  visitModelBody(ast, nodes, lastName, level) {
    assert.equal(ast.type, 'modelBody');
    var fields = [];
    const structMap = [];
    let node;
    for (let i = 0; i < nodes.length; i++) {
      node = nodes[i];
      let comments = DSL.comment.getFrontComments(this.comments, node.tokenRange[0]);
      this.visitComments(comments, level);
      var fieldName = _name(node.fieldName);
      const structName = lastName + _format(fieldName);
      const description = getAttr(node, 'description');
      const example = getAttr(node, 'example');
      const checkBlank = getAttr(node, 'checkBlank');
      const nullable = getAttr(node, 'nullable');
      const sensitive = getAttr(node, 'sensitive');
      const deprecated = getAttr(node, 'deprecated');
      let hasNextSection = false;
      if (deprecated === 'true') {
        this.emit(`// Deprecated\n`, level);
        hasNextSection = true;
      }
      if (description || example || typeof checkBlank !== 'undefined' || typeof nullable !== 'undefined' || typeof sensitive !== 'undefined') {
        if (description) {
          if (hasNextSection) {
            this.emit(`// \n`, level);
          }
          const descriptions = _escape(description).split('\n');
          for (let j = 0; j < descriptions.length; j++) {
            if (descriptions[j] === '') {
              this.emit(`// \n`, level);
            }
            else {
              this.emit(`// ${descriptions[j]}\n`, level);
              if (j < descriptions.length - 1 && descriptions[j + 1] !== '') {
                this.emit(`// \n`, level);
              }
            }
          }
          hasNextSection = true;
        }
        if (typeof checkBlank !== 'undefined') {
          if (hasNextSection) {
            this.emit(`// \n`, level);
          }
          this.emit('// check if is blank:\n', level);
          this.emit(`// ${checkBlank}\n`, level);
          hasNextSection = true;
        }
        if (typeof nullable !== 'undefined') {
          if (hasNextSection) {
            this.emit(`// \n`, level);
          }
          this.emit('// if can be null:\n', level);
          this.emit(`// ${nullable}\n`, level);
          hasNextSection = true;
        }
        if (typeof sensitive !== 'undefined') {
          if (hasNextSection) {
            this.emit(`// \n`, level);
          }
          this.emit('// if sensitive:\n', level);
          this.emit(`// ${sensitive}\n`, level);
          hasNextSection = true;
        }
        if (example) {
          if (hasNextSection) {
            this.emit(`// \n`, level);
          }
          const examples = _escape(example).split('\n');
          this.emit('// example:\n', level);
          this.emit(`// \n`, level);
          for (let j = 0; j < examples.length; j++) {
            if (examples[j] === '') {
              this.emit(`// \n`, level);
            } else {
              this.emit(`// ${examples[j]}\n`, level);
              if (j < examples.length - 1 && examples[j + 1] !== '') {
                this.emit(`// \n`, level);
              }
            }
          }
        }
      }
      this.emit(`${_format(fieldName)} `, level);
      let { type, omitemptyEnable } = this.visitFieldType(node, structName, fields, structMap, level);
      var realName = _getAttr(node, 'name');
      if (!realName) {
        realName = fieldName;
      }
      var tag = `json:"${realName}${omitemptyEnable ? ',omitempty' : ''}" xml:"${realName}${omitemptyEnable ? ',omitempty' : ''}"`;
      const anno = this.parseAnnotation(node, {
        'signed': 'string', 'encode': 'string'
        , 'pattern': 'string', 'maxLength': 'value', 'minLength': 'value',
        'maximum': 'value', 'minimum': 'value'
      });
      if (node.required) {
        tag = tag + ` require:"true"`;
      }
      if (anno !== '') {
        tag = tag + anno;
      }
      if (type !== '') {
        tag = tag + ` ${type}`;
      }
      this.emit(`\`${tag}\``);
      this.emit(`\n`);
    }
    if (node) {
      //find the last node's back comment
      let comments = DSL.comment.getBetweenComments(this.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.comments, ast.tokenRange[0], ast.tokenRange[1]);
      this.visitComments(comments, level);
    }
    this.emit(`}\n`);