Co-authored-by: anqiansong <anqiansong@bytedance.com>
master
anqiansong 3 years ago committed by GitHub
parent f1102fb262
commit cdf7ec213c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ import (
) )
const prefixKey = "prefix" const prefixKey = "prefix"
const groupKey = "group"
// Api describes syntax for api // Api describes syntax for api
type Api struct { type Api struct {
@ -52,12 +53,16 @@ func (v *ApiVisitor) acceptService(root, final *Api) {
} }
v.duplicateServerItemCheck(service) v.duplicateServerItemCheck(service)
var prefix string var prefix, group string
if service.AtServer != nil { if service.AtServer != nil {
p := service.AtServer.Kv.Get(prefixKey) p := service.AtServer.Kv.Get(prefixKey)
if p != nil { if p != nil {
prefix = p.Text() prefix = p.Text()
} }
g := service.AtServer.Kv.Get(groupKey)
if g != nil {
group = g.Text()
}
} }
for _, route := range service.ServiceApi.ServiceRoute { for _, route := range service.ServiceApi.ServiceRoute {
uniqueRoute := fmt.Sprintf("%s %s", route.Route.Method.Text(), path.Join(prefix, route.Route.Path.Text())) uniqueRoute := fmt.Sprintf("%s %s", route.Route.Method.Text(), path.Join(prefix, route.Route.Path.Text()))
@ -92,10 +97,14 @@ func (v *ApiVisitor) acceptService(root, final *Api) {
v.panic(handlerExpr, "mismatched handler") v.panic(handlerExpr, "mismatched handler")
} }
if _, ok := final.handlerM[handlerExpr.Text()]; ok { handlerKey := handlerExpr.Text()
if len(group) > 0 {
handlerKey = fmt.Sprintf("%s/%s", group, handlerExpr.Text())
}
if _, ok := final.handlerM[handlerKey]; ok {
v.panic(handlerExpr, fmt.Sprintf("duplicate handler '%s'", handlerExpr.Text())) v.panic(handlerExpr, fmt.Sprintf("duplicate handler '%s'", handlerExpr.Text()))
} }
final.handlerM[handlerExpr.Text()] = Holder final.handlerM[handlerKey] = Holder
} }
final.Service = append(final.Service, service) final.Service = append(final.Service, service)
} }

@ -240,12 +240,16 @@ 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 var prefix, group string
if each.AtServer != nil { if each.AtServer != nil {
p := each.AtServer.Kv.Get(prefixKey) p := each.AtServer.Kv.Get(prefixKey)
if p != nil { if p != nil {
prefix = p.Text() prefix = p.Text()
} }
g := each.AtServer.Kv.Get(groupKey)
if p != nil {
group = g.Text()
}
} }
for _, r := range each.ServiceApi.ServiceRoute { for _, r := range each.ServiceApi.ServiceRoute {
handler := r.GetHandler() handler := r.GetHandler()
@ -253,9 +257,13 @@ func (p *Parser) duplicateRouteCheck(nestedApi *Api, mainHandlerMap, mainRouteMa
return fmt.Errorf("%s handler not exist near line %d", nestedApi.LinePrefix, r.Route.Method.Line()) return fmt.Errorf("%s handler not exist near line %d", nestedApi.LinePrefix, r.Route.Method.Line())
} }
if _, ok := mainHandlerMap[handler.Text()]; ok { handlerKey := handler.Text()
if len(group) > 0 {
handlerKey = fmt.Sprintf("%s/%s", group, handler.Text())
}
if _, ok := mainHandlerMap[handlerKey]; ok {
return fmt.Errorf("%s line %d:%d duplicate handler '%s'", return fmt.Errorf("%s line %d:%d duplicate handler '%s'",
nestedApi.LinePrefix, handler.Line(), handler.Column(), handler.Text()) nestedApi.LinePrefix, handler.Line(), handler.Column(), handlerKey)
} }
p := fmt.Sprintf("%s://%s", r.Route.Method.Text(), path.Join(prefix, r.Route.Path.Text())) p := fmt.Sprintf("%s://%s", r.Route.Method.Text(), path.Join(prefix, r.Route.Path.Text()))

Loading…
Cancel
Save