go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

I hope to support more parameter passing to achieve more flexible automatic generation.

Open cnbattle opened this issue 1 year ago β€’ 5 comments

this place

https://github.com/zeromicro/go-zero/blob/b61e36445851aefecb65138fd547ec83926c7e97/tools/goctl/api/gogen/genhandlers.go#L34-L55

user.api

@server(
    group: user
    prefix: api/user
)
service aif-api {
    @doc(
        summary: "Push SMS"
        diy1: "123"
    )
    @handler SmsPushHandler
    post /sms (SmsReq)

    @doc(
        summary: "User Login"
    )
    @handler LoginHandler
    post /login (UserLoginReq) returns (UserLoginRes)
}

My handler.tpl

package {{.PkgName}}

import (
	"net/http"

	"aif/internal/ai"
	{{.ImportPackages}}

	"github.com/zeromicro/go-zero/rest/httpx"
)

// {{.HandlerName}}
// @Summary      User login
// @Description  
// @Tags         {{.PkgName}}
// @Accept       json
// @Produce      json
{{if .HasRequest}}// @Param        params query types.{{.RequestType}}  true  "params" {{end}}
{{if .HasResp}}// @Success      200  {object}  ai.H{data=ai.ListData{rows=[]types.Attribute}}{{else}}// @Success      200  {object}  ai.H{{end}}
// @Router       /api/{{.PkgName}}/{{.HandlerName}} [get]
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		{{if .HasRequest}}var req types.{{.RequestType}}
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
		{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
		if err != nil {
			httpx.OkJsonCtx(r.Context(), w, ai.NewE(err))
		} else {
			{{if .HasResp}}httpx.OkJsonCtx(r.Context(), w, ai.Data(resp)){{else}}httpx.OkJsonCtx(r.Context(), w, ai.Success()){{end}}
		}
	}
}

I hope to generate Swagger annotation documentation here, but there is a lack of route @doc related data.

cnbattle avatar Mar 15 '23 02:03 cnbattle

@kevwan

cnbattle avatar Mar 15 '23 02:03 cnbattle

Bot detected the issue body's language is not English, translate it automatically. πŸ‘―πŸ‘­πŸ»πŸ§‘β€πŸ€β€πŸ§‘πŸ‘«πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸ»πŸ‘©πŸΎβ€πŸ€β€πŸ‘¨πŸΏπŸ‘¬πŸΏ


@kevwan

Issues-translate-bot avatar Mar 15 '23 02:03 Issues-translate-bot

At present, the api file parses with annotation information, which I can inject into the template variable, and you can use this variable by customizing the template

kesonan avatar Mar 27 '23 11:03 kesonan

At present, the api file parses with annotation information, which I can inject into the template variable, and you can use this variable by customizing the template

@anqiansong Thank you for your reply and hope to support this feature as soon as possible.

cnbattle avatar Mar 27 '23 12:03 cnbattle

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Mar 31 '24 01:03 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jul 03 '24 01:07 github-actions[bot]