Cli (#1272)
* Fix issue 1260 (#1262) * Fix #1238 (#1266) * docs: update readme to use goctl@cli (#1255) * chore: update goctl version * style: coding style * docs: update readme to use goctl@cli * fix #1238 * format code * format code Co-authored-by: Kevin Wan <wanjunfeng@gmail.com> Co-authored-by: anqiansong <anqiansong@bytedance.com> Co-authored-by: anqiansong <anqiansong@gmail.com> Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
@@ -39,12 +39,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
var mapping = map[string]string{
|
var mapping = map[string]string{
|
||||||
"delete": "http.MethodDelete",
|
"delete": "http.MethodDelete",
|
||||||
"get": "http.MethodGet",
|
"get": "http.MethodGet",
|
||||||
"head": "http.MethodHead",
|
"head": "http.MethodHead",
|
||||||
"post": "http.MethodPost",
|
"post": "http.MethodPost",
|
||||||
"put": "http.MethodPut",
|
"put": "http.MethodPut",
|
||||||
"patch": "http.MethodPatch",
|
"patch": "http.MethodPatch",
|
||||||
|
"connect": "http.MethodConnect",
|
||||||
|
"options": "http.MethodOptions",
|
||||||
|
"trace": "http.MethodTrace",
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ package ast
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const prefixKey = "prefix"
|
||||||
|
|
||||||
// Api describes syntax for api
|
// Api describes syntax for api
|
||||||
type Api struct {
|
type Api struct {
|
||||||
LinePrefix string
|
LinePrefix string
|
||||||
@@ -49,8 +52,15 @@ func (v *ApiVisitor) acceptService(root, final *Api) {
|
|||||||
}
|
}
|
||||||
v.duplicateServerItemCheck(service)
|
v.duplicateServerItemCheck(service)
|
||||||
|
|
||||||
|
var prefix string
|
||||||
|
if service.AtServer != nil {
|
||||||
|
p := service.AtServer.Kv.Get(prefixKey)
|
||||||
|
if p != nil {
|
||||||
|
prefix = p.Text()
|
||||||
|
}
|
||||||
|
}
|
||||||
for _, route := range service.ServiceApi.ServiceRoute {
|
for _, route := range service.ServiceApi.ServiceRoute {
|
||||||
uniqueRoute := fmt.Sprintf("%s %s", route.Route.Method.Text(), route.Route.Path.Text())
|
uniqueRoute := fmt.Sprintf("%s %s", route.Route.Method.Text(), path.Join(prefix, route.Route.Path.Text()))
|
||||||
if _, ok := final.routeM[uniqueRoute]; ok {
|
if _, ok := final.routeM[uniqueRoute]; ok {
|
||||||
v.panic(route.Route.Method, fmt.Sprintf("duplicate route '%s'", uniqueRoute))
|
v.panic(route.Route.Method, fmt.Sprintf("duplicate route '%s'", uniqueRoute))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package ast
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -113,13 +114,13 @@ func (p *Parser) parse(filename, content string) (*Api, error) {
|
|||||||
apiAstList = append(apiAstList, root)
|
apiAstList = append(apiAstList, root)
|
||||||
for _, imp := range root.Import {
|
for _, imp := range root.Import {
|
||||||
dir := filepath.Dir(p.src)
|
dir := filepath.Dir(p.src)
|
||||||
path := filepath.Join(dir, imp.Value.Text())
|
imp := filepath.Join(dir, imp.Value.Text())
|
||||||
data, err := p.readContent(path)
|
data, err := p.readContent(imp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
nestedApi, err := p.invoke(path, data)
|
nestedApi, err := p.invoke(imp, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -196,8 +197,8 @@ func (p *Parser) valid(mainApi, nestedApi *Api) error {
|
|||||||
if handler.IsNotNil() {
|
if handler.IsNotNil() {
|
||||||
handlerName := handler.Text()
|
handlerName := handler.Text()
|
||||||
handlerMap[handlerName] = Holder
|
handlerMap[handlerName] = Holder
|
||||||
path := fmt.Sprintf("%s://%s", g.Route.Method.Text(), g.Route.Path.Text())
|
route := fmt.Sprintf("%s://%s", g.Route.Method.Text(), g.Route.Path.Text())
|
||||||
routeMap[path] = Holder
|
routeMap[route] = Holder
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,6 +240,13 @@ func (p *Parser) valid(mainApi, nestedApi *Api) error {
|
|||||||
|
|
||||||
func (p *Parser) duplicateRouteCheck(nestedApi *Api, mainHandlerMap, mainRouteMap map[string]PlaceHolder) error {
|
func (p *Parser) duplicateRouteCheck(nestedApi *Api, mainHandlerMap, mainRouteMap map[string]PlaceHolder) error {
|
||||||
for _, each := range nestedApi.Service {
|
for _, each := range nestedApi.Service {
|
||||||
|
var prefix string
|
||||||
|
if each.AtServer != nil {
|
||||||
|
p := each.AtServer.Kv.Get(prefixKey)
|
||||||
|
if p != nil {
|
||||||
|
prefix = p.Text()
|
||||||
|
}
|
||||||
|
}
|
||||||
for _, r := range each.ServiceApi.ServiceRoute {
|
for _, r := range each.ServiceApi.ServiceRoute {
|
||||||
handler := r.GetHandler()
|
handler := r.GetHandler()
|
||||||
if !handler.IsNotNil() {
|
if !handler.IsNotNil() {
|
||||||
@@ -250,8 +258,8 @@ func (p *Parser) duplicateRouteCheck(nestedApi *Api, mainHandlerMap, mainRouteMa
|
|||||||
nestedApi.LinePrefix, handler.Line(), handler.Column(), handler.Text())
|
nestedApi.LinePrefix, handler.Line(), handler.Column(), handler.Text())
|
||||||
}
|
}
|
||||||
|
|
||||||
path := fmt.Sprintf("%s://%s", r.Route.Method.Text(), r.Route.Path.Text())
|
p := fmt.Sprintf("%s://%s", r.Route.Method.Text(), path.Join(prefix, r.Route.Path.Text()))
|
||||||
if _, ok := mainRouteMap[path]; ok {
|
if _, ok := mainRouteMap[p]; ok {
|
||||||
return fmt.Errorf("%s line %d:%d duplicate route '%s'",
|
return fmt.Errorf("%s line %d:%d duplicate route '%s'",
|
||||||
nestedApi.LinePrefix, r.Route.Method.Line(), r.Route.Method.Column(), r.Route.Method.Text()+" "+r.Route.Path.Text())
|
nestedApi.LinePrefix, r.Route.Method.Line(), r.Route.Method.Column(), r.Route.Method.Text()+" "+r.Route.Path.Text())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user