zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
28.88k stars 3.91k forks source link

shorturl.api 22:3 syntax error: expected '@handler' | '}', got '@server' #3889

Closed duckjiangwei closed 6 months ago

duckjiangwei commented 7 months ago

位置: https://github.com/zeromicro/zero-doc/blob/main/doc/shorturl.md

教程说 api/shorturl.api 文件的代码如下:

type (
  expandReq {
    shorten string `form:"shorten"`
  }

  expandResp {
    url string `json:"url"`
  }
)

type (
  shortenReq {
    url string `form:"url"`
  }

  shortenResp {
    shorten string `json:"shorten"`
  }
)

service shorturl-api {
  @server(
    handler: ShortenHandler
  )
  get /shorten(shortenReq) returns(shortenResp)

  @server(
    handler: ExpandHandler
  )
  get /expand(expandReq) returns(expandResp)
}

执行 goctl api go -api shorturl.api -dir .
报错: shorturl.api 22:3 syntax error: expected '@handler' | '}', got '@server'

@server语句块是否应该放外面

zzZZzzz888 commented 7 months ago

The writing of @server in service is no longer supported. You can modify the code in this way:

service shorturl-api {
-  @server(
-    handler: ShortenHandler
-  )
+  @handler ShortenHandler
   get /shorten(shortenReq) returns(shortenResp)

-  @server(
-    handler: ExpandHandler
-  )
+  @handler ExpandHandler
   get /expand(expandReq) returns(expandResp)
 }

@server is used to define the properties that used in server side. To find its specific style of writing, you can refer to the service grammar block in the file tools/goctl/api/parser/readme.md as below:

eg2:规范写法(推荐)

@server(
  jwt: Auth
  group: foo
  middleware: AuthMiddleware
)
service foo-api{
  @doc "foo"
  @handler: foo
  post /foo/:id (Foo) returns (Bar)
}

service foo-api{
  @handler ping
  get /ping

  @doc "foo"
  @handler: bar
  post /bar/:id (Foo)
}
duckjiangwei commented 6 months ago

close