eachAPI()

in lib/generator.js [3791:3867]


  eachAPI(ast, level, predefined) {
    // if (ast.annotation) {
    //   this.emit(`${_anno(ast.annotation.value)}\n`, level);
    // }
    const env = {
      // params, paramMap, returnType,
      predefined,
      returnType: ast.returnType,
      runtimeBody: ast.runtimeBody,
      local: new Map(),
      hasThrow: true,
      yieldFunc: _isIterator(ast.returnType),
      nestFuncParamName: new Map(),
      nestFuncParamNameSubscript: { 'count': 0 },
    };

    const apiName = _name(ast.apiName);
    this.visitAnnotation(ast.annotation, level);
    let comments = DSL.comment.getFrontComments(this.comments, ast.tokenRange[0]);
    this.visitComments(comments, level);
    // func (b *Buffer) Next(n int) []byte {
    this.emit(`func (client *${this.structName}) ${_format(apiName)}`, level);
    this.visitParams(ast.params, level, env);
    this.visitReturnType(ast, level, env);
    // this.emit(` (*map[string]interface{}, error) {\n`);
    let baseLevel = ast.runtimeBody ? level + 1 : level;
    if(env.yieldFunc) {
      this.emit('defer close(_yield)\n', level + 1);
      if(env.hasThrow) {
        this.emit('defer close(_yieldErr)\n', level + 1);
      }
    }
    // api level
    if (ast.runtimeBody) {
      this.visitRuntimeBefore(ast.runtimeBody, level + 1, env);
    }

    // temp level
    this.visitAPIBody(ast.apiBody, baseLevel + 1, env);

    // if (ast.runtimeBody) {
    //   this.emit(`_lastRequest = ${REQUEST}\n`, baseLevel + 1);
    // }

    this.emit(`${RESPONSE}, _err := dara.DoRequest(${REQUEST}`, baseLevel + 1);

    if (ast.runtimeBody) {
      this.emit(`, _runtime`);
    } else {
      this.emit(`, nil`);
    }
    this.emit(`)\n`);

    this.visitAPIErrCatch(baseLevel + 1, env);

    if (ast.returns) {
      this.visitReturnBody(ast.returns, apiName, baseLevel + 1, env);
    } else {
      this.visitDefaultReturnBody(baseLevel + 1, env);
    }

    if (ast.runtimeBody) {
      this.emit('}\n', level + 1);
      if(env.yieldFunc) {
        this.emit(`_yieldErr <- _resultErr\n`, level + 1);
        this.emit(`return\n`, level + 1);
      } else {
        if(!this.noCompatible) {
          this.emit(`if dara.BoolValue(client.DisableSDKError) != true {\n`, level + 1);
          this.emit(`_resultErr = dara.TeaSDKError(_resultErr)\n`, level + 2);
          this.emit(`}\n`, level + 1);
        }
        this.emit(`return _result, _resultErr\n`, level + 1);
      }
    }
    
    this.emit(`}\n\n`, level);