From e535441154e076cff0a279d4f9640967bacbde15 Mon Sep 17 00:00:00 2001 From: fgy Date: Fri, 3 Nov 2023 14:59:38 +0800 Subject: [PATCH] optimize: slice method for thrift --- cmd/hz/thrift/ast.go | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/cmd/hz/thrift/ast.go b/cmd/hz/thrift/ast.go index 83722358f..981eb7649 100644 --- a/cmd/hz/thrift/ast.go +++ b/cmd/hz/thrift/ast.go @@ -127,7 +127,7 @@ func astToService(ast *parser.Thrift, resolver *Resolver, args *config.Argument) } hmethod, path := httpAnnos[0].method, httpAnnos[0].path - if len(path) != 1 || path[0] == "" { + if len(path) == 0 || path[0] == "" { return nil, fmt.Errorf("invalid api.%s for %s.%s: %s", hmethod, s.Name, m.Name, path) } @@ -199,17 +199,22 @@ func astToService(ast *parser.Thrift, resolver *Resolver, args *config.Argument) methods = append(methods, method) for idx, anno := range httpAnnos { if idx == 0 { + for i := 1; i < len(anno.path); i++ { + newMethod, err := newHTTPMethod(s, m, method, i, anno) + if err != nil { + return nil, err + } + methods = append(methods, newMethod) + } continue } - tmp := *method - hmethod, path := anno.method, anno.path - if len(path) != 1 || path[0] == "" { - return nil, fmt.Errorf("invalid api.%s for %s.%s: %s", hmethod, s.Name, m.Name, path) + for i := 0; i < len(anno.path); i++ { + newMethod, err := newHTTPMethod(s, m, method, i, anno) + if err != nil { + return nil, err + } + methods = append(methods, newMethod) } - tmp.HTTPMethod = hmethod - tmp.Path = path[0] - tmp.GenHandler = false - methods = append(methods, &tmp) } if args.CmdType == meta.CmdClient { clientMethod := &generator.ClientMethod{} @@ -234,6 +239,18 @@ func astToService(ast *parser.Thrift, resolver *Resolver, args *config.Argument) return out, nil } +func newHTTPMethod(s *parser.Service, m *parser.Function, method *generator.HttpMethod, i int, anno httpAnnotation) (*generator.HttpMethod, error) { + newMethod := *method + hmethod, path := anno.method, anno.path + if path[i] == "" { + return nil, fmt.Errorf("invalid api.%s for %s.%s: %s", hmethod, s.Name, m.Name, path[i]) + } + newMethod.HTTPMethod = hmethod + newMethod.Path = path[i] + newMethod.GenHandler = false + return &newMethod, nil +} + func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Type, symbol ResolvedSymbol) error { if p == nil { return fmt.Errorf("get type failed for parse annotatoon to client")