visitType()

in lib/generator.js [828:972]


  visitType(ast, level, env) {
    if (ast.type === 'array') {
      this.emit('List<');
      this.visitType(ast.subType || ast.itemType, level);
      this.emit('>');
    } else if (ast.type === 'map' || ast.fieldType === 'map') {
      if (env && env.castToObject) {
        this.emit('Dictionary<string, object>');
        env.castToObject = false;
        return;
      }
      this.emit('Dictionary<');
      this.visitType(ast.keyType, level);
      this.emit(', ');
      this.visitType(ast.valueType, level);
      this.emit('>');
    } else if (this.predefined && this.predefined['module:' + _name(ast)]) {
      let clientName = (this.imports[_name(ast)] && this.imports[_name(ast)]['client']) || `${_name(ast)}.Client`;
      this.emit(clientName);
    } else if (ast.type === 'model') {
      let namespace = this.namespace;
      let type = 'Models';
      if (ast.moduleName) {
        namespace = this.moduleClass.get(ast.moduleName).namespace;
        var usedEx;
        if (this.usedExternException) {
          usedEx = this.usedExternException.get(ast.moduleName);
        }
        if (usedEx && usedEx.has(ast.name)) {
          type = 'Exceptions';
        }
      } else if (this.predefined[ast.name] && this.predefined[ast.name].isException) {
        type = 'Exceptions';
      }
      let tempName = '';
      let models = _name(ast).split('.');
      models.forEach((item, index) => {
        if (index > 0) {
          this.emit('.');
        }
        if (index === 0) {
          tempName += _upperFirst(this._type(item));
          const realModelName = this.getRealModelName(`${namespace}.${type}`, tempName, type);
          this.emit(realModelName);
        } else {
          tempName += _upperFirst(this._type(item));
          this.emit(tempName);
        }
      });
    } else if (ast.type === 'moduleModel' && ast.path && ast.path.length > 0) {
      const [moduleId, ...rest] = ast.path;
      const { namespace } = this.moduleClass.get(moduleId.lexeme);
      let moduleModelName = '';
      let tempName = '';
      rest.forEach((item, index) => {
        tempName += _upperFirst(item.lexeme);
        if (index < rest.length - 1) {
          moduleModelName += tempName + '.';
        } else {
          moduleModelName += tempName;
        }
      });
      let type = 'Models';
      if (this.usedExternException) {
        usedEx = this.usedExternException.get(moduleId.lexeme);
      }
      if (usedEx && usedEx.has(moduleModelName)) {
        type = 'Exceptions';
      }
      const subModelName = this.getRealModelName(`${namespace}.${type}`, moduleModelName, type);
      this.emit(subModelName);
    } else if (ast.type === 'module_instance') {
      if (_name(ast) in builtinMap) {
        this.emit(`${builtinMap[_name(ast)]}`);
      } else {
        const realClsName = this.getRealClientName(_name(ast));
        this.emit(realClsName);
      }
    } else if (ast.idType === 'module') {
      let moduleName = _name(ast);
      moduleName = this.getRealClientName(moduleName);
      this.emit(`${moduleName || 'Client'}`);
    } else if (ast.idType === 'model') {
      let type = 'Models';
      if (this.predefined[ast.lexeme] && this.predefined[ast.lexeme].isException) {
        type = 'Exceptions';
      }
      const realModelName = this.getRealModelName(`${this.namespace}.${type}`, _upperFirst(ast.lexeme), type);
      this.emit(realModelName);
    } else if (ast.idType === 'builtin_model') {
      const subModelName = ast.lexeme;
      const namespace = this.getType(subModelName);
      const realModelName = this.getRealModelName(namespace, subModelName);
      this.emit(realModelName);
    } else if (ast.type === 'moduleTypedef') {
      const [moduleId, modelName] = ast.path;
      const moduleTypedef = this.moduleTypedef[moduleId.lexeme];
      const typedef = moduleTypedef[modelName.lexeme];
      if (!typedef.import) {
        this.emit(typedef.type);
      } else {
        const typedefName = this.getRealModelName(typedef.import, typedef.type);
        this.emit(typedefName);
      }
    } else if (ast.type === 'typedef' || ast.idType === 'typedef') {
      const typedef = this.typedef[ast.lexeme];
      if (!typedef.import) {
        this.emit(typedef.type);
      } else {
        const typedefName = this.getRealModelName(typedef.import, typedef.type);
        this.emit(typedefName);
      }
    } else if (ast.fieldType === 'array') {
      this.emit('List<');
      this.visitType(ast.fieldItemType, level);
      this.emit('>');
    } else if (ast.type === 'entry') {
      this.emit('KeyValuePair<string, ');
      this.visitType(ast.valueType);
      this.emit('>');
    } else if (ast.type === 'subModel') {
      let tempName = '';
      for (let i = 0; i < ast.path.length; i++) {
        if (i > 0) {
          this.emit('.');
        }
        tempName += _upperFirst(_name(ast.path[i]));
        this.emit(tempName);
      }
    } else if (typeof ast.fieldType === 'string') {
      this.emit(`${this._type(ast.fieldType)}`);
    } else if (this.isIterator(ast)) {
      if (ast.type === 'iterator') {
        this.emit('IEnumerable<');
      } else if (ast.type === 'asyncIterator') {
        this.emit('IAsyncEnumerable<');
      }
      this.visitType(ast.valueType);
      this.emit('>');
    } else if (ast.type === 'basic') {
      this.emit(this._type(_name(ast)));
    } else {
      this.emit(this._type(_name(ast)));
    }
  }