Skip to content

Commit

Permalink
optimize: slice method for thrift
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Nov 13, 2023
1 parent e02c162 commit e535441
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions cmd/hz/thrift/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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{}
Expand All @@ -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")
Expand Down

0 comments on commit e535441

Please sign in to comment.