aliyun / darabonba

Darabonba 是一种用于 OpenAPI 的 DSL 语言,可以用来生成多语言的 SDK、Code Sample、Test Case 等代码
Other
1.05k stars 93 forks source link

生成sdk 报错 #112

Open jiashiwen opened 2 years ago

jiashiwen commented 2 years ago
import Util;

type @endpoint_host = string
type @protocol = string

model Config {
  endpoint: string,
  protocol: string
}

init(config: Config){
  @endpoint_host = config.endpoint;
  @protocol = config.protocol;
}

model RawGetRequest {
  Key: string,
}

model RawGetResponse{  
    code: number,
    msg: string,
    data: string
}

api rawgetModule(pathname: string, query: RawGetRequest): RawGetResponse {
  __request.protocol = @protocol;
  __request.method = 'POST';
  __request.pathname = `/api/v1/raw/get`;
  __request.headers = {
    host = @endpoint_host,
    content-type = 'application/json; charset=utf-8'
  };
  __request.query = Util.toJSONString(query);  
} returns {
  var result = Util.assertAsMap(Util.readAsJSON(__response.body));

  if (!Util.equalNumber(__response.statusCode, 200)) {
    throw {
      message = `code: ${__response.statusCode}, ${result.code} reason: ${result.msg}`,
      code = `${result.code}`
    };
  }

  if (!Util.assertAsBoolean(result.ok)) {
    throw {
      message = `code: ${result.status}, ${result.code} reason: ${result.msg}`,
      code = `${result.code}`
    };
  }
  return result;
} runtime {
  timeout = 1000 // 10s 的过期时间
}

报错如下

exec error :
SyntaxError: Unexpected token / in JSON at position 110
    at JSON.parse (<anonymous>)
    at CodegenCommand.exec (/Users/jiashiwen/node-v16.13.0-darwin-x64/lib/node_modules/@darabonba/cli/commands/codegen.js:116:22)
    at CommandsApplication.exec (/Users/jiashiwen/node-v16.13.0-darwin-x64/lib/node_modules/@darabonba/cli/lib/app.js:154:13)
    at CommandsApplication.run (/Users/jiashiwen/node-v16.13.0-darwin-x64/lib/node_modules/@darabonba/cli/lib/app.js:34:12)
    at Object.<anonymous> (/Users/jiashiwen/node-v16.13.0-darwin-x64/lib/node_modules/@darabonba/cli/bin/dara.js:45:5)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

不知道是那句语法错了

jiashiwen commented 2 years ago

官方能否给出完整的工程例子?

ZhaoQi99 commented 2 years ago

Try use _request.query = Util.stringify_map_value(query). You can see below examples.