Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimzie(hz): handler path is for package #1215

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/hz/protobuf/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func astToService(ast *descriptorpb.FileDescriptorProto, resolver *Resolver, cmd
if !ok || len(handlerOutDir) == 0 {
handlerOutDir = ""
}
if !util.IsValidPackageName(filepath.Base(handlerOutDir)) {
handlerOutDir = ""
}
if len(handlerOutDir) == 0 {
handlerOutDir = servicePath
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/hz/thrift/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package thrift

import (
"fmt"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -125,6 +126,9 @@ func astToService(ast *parser.Thrift, resolver *Resolver, args *config.Argument)
} else if len(genPaths) > 0 {
return nil, fmt.Errorf("too many 'api.handler_path' for %s", m.Name)
}
if !util.IsValidPackageName(filepath.Base(handlerOutDir)) {
handlerOutDir = ""
}

hmethod, path := httpAnnos[0].method, httpAnnos[0].path
if len(path) == 0 || path[0] == "" {
Expand Down
7 changes: 7 additions & 0 deletions cmd/hz/util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package util

import (
"reflect"
"regexp"
"strings"
"unicode/utf8"
"unsafe"
Expand Down Expand Up @@ -97,3 +98,9 @@ func SnakeString(s string) string {
}
return strings.ToLower(Bytes2Str(data))
}

func IsValidPackageName(name string) bool {
// Regular expression for a valid package name
re := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]*$`)
return re.MatchString(name)
}
Loading