mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
change parameter prefix of type string to prefixes of type slice for command enums (#2662)
This commit is contained in:
@ -21,10 +21,10 @@ import (
|
||||
type (
|
||||
CGenEnums struct{}
|
||||
CGenEnumsInput struct {
|
||||
g.Meta `name:"enums" config:"{CGenEnumsConfig}" brief:"{CGenEnumsBrief}" eg:"{CGenEnumsEg}"`
|
||||
Src string `name:"src" short:"s" dc:"source folder path to be parsed" d:"."`
|
||||
Path string `name:"path" short:"p" dc:"output go file path storing enums content" d:"internal/boot/boot_enums.go"`
|
||||
Prefix string `name:"prefix" short:"x" dc:"only exports packages that starts with specified prefix"`
|
||||
g.Meta `name:"enums" config:"{CGenEnumsConfig}" brief:"{CGenEnumsBrief}" eg:"{CGenEnumsEg}"`
|
||||
Src string `name:"src" short:"s" dc:"source folder path to be parsed" d:"."`
|
||||
Path string `name:"path" short:"p" dc:"output go file path storing enums content" d:"internal/boot/boot_enums.go"`
|
||||
Prefixes []string `name:"prefixes" short:"x" dc:"only exports packages that starts with specified prefixes"`
|
||||
}
|
||||
CGenEnumsOutput struct{}
|
||||
)
|
||||
@ -57,7 +57,7 @@ func (c CGenEnums) Enums(ctx context.Context, in CGenEnumsInput) (out *CGenEnums
|
||||
if err != nil {
|
||||
mlog.Fatal(err)
|
||||
}
|
||||
mlog.Printf(`scanning: %s`, realPath)
|
||||
mlog.Printf(`scanning for enums: %s`, realPath)
|
||||
cfg := &packages.Config{
|
||||
Dir: realPath,
|
||||
Mode: pkgLoadMode,
|
||||
@ -67,7 +67,7 @@ func (c CGenEnums) Enums(ctx context.Context, in CGenEnumsInput) (out *CGenEnums
|
||||
if err != nil {
|
||||
mlog.Fatal(err)
|
||||
}
|
||||
p := NewEnumsParser(in.Prefix)
|
||||
p := NewEnumsParser(in.Prefixes)
|
||||
p.ParsePackages(pkgs)
|
||||
var enumsContent = gstr.ReplaceByMap(consts.TemplateGenEnums, g.MapStrStr{
|
||||
"{PackageName}": gfile.Basename(gfile.Dir(in.Path)),
|
||||
@ -77,7 +77,7 @@ func (c CGenEnums) Enums(ctx context.Context, in CGenEnumsInput) (out *CGenEnums
|
||||
if err = gfile.PutContents(in.Path, enumsContent); err != nil {
|
||||
return
|
||||
}
|
||||
mlog.Printf(`generated: %s`, in.Path)
|
||||
mlog.Printf(`generated enums go file: %s`, in.Path)
|
||||
mlog.Print("done!")
|
||||
return
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ const pkgLoadMode = 0xffffff
|
||||
type EnumsParser struct {
|
||||
enums []EnumItem
|
||||
parsedPkg map[string]struct{}
|
||||
prefix string
|
||||
prefixes []string
|
||||
}
|
||||
|
||||
type EnumItem struct {
|
||||
@ -43,11 +43,11 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
func NewEnumsParser(prefix string) *EnumsParser {
|
||||
func NewEnumsParser(prefixes []string) *EnumsParser {
|
||||
return &EnumsParser{
|
||||
enums: make([]EnumItem, 0),
|
||||
parsedPkg: make(map[string]struct{}),
|
||||
prefix: prefix,
|
||||
prefixes: prefixes,
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,9 +67,16 @@ func (p *EnumsParser) ParsePackage(pkg *packages.Package) {
|
||||
return
|
||||
}
|
||||
p.parsedPkg[pkg.ID] = struct{}{}
|
||||
// Only parse specified prefix.
|
||||
if p.prefix != "" {
|
||||
if !gstr.HasPrefix(pkg.ID, p.prefix) {
|
||||
|
||||
// Only parse specified prefixes.
|
||||
if len(p.prefixes) > 0 {
|
||||
var hasPrefix bool
|
||||
for _, prefix := range p.prefixes {
|
||||
if hasPrefix = gstr.HasPrefix(pkg.ID, prefix); hasPrefix {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasPrefix {
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -108,7 +115,6 @@ func (p *EnumsParser) ParsePackage(pkg *packages.Package) {
|
||||
Type: enumType,
|
||||
Kind: enumKind,
|
||||
})
|
||||
|
||||
}
|
||||
for _, im := range pkg.Imports {
|
||||
p.ParsePackage(im)
|
||||
|
||||
@ -18,7 +18,7 @@ const (
|
||||
// StartServer1 starts Server1: A simple http server for demo.
|
||||
func StartServer1() {
|
||||
s := g.Server(1)
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
s.BindHandler("/*", func(r *ghttp.Request) {
|
||||
r.Response.Write("response from server 1")
|
||||
})
|
||||
s.BindHandler("/user/1", func(r *ghttp.Request) {
|
||||
@ -33,11 +33,17 @@ func StartServer1() {
|
||||
func StartServer2() {
|
||||
s := g.Server(2)
|
||||
u, _ := url.Parse(UpStream)
|
||||
s.BindHandler("/*", func(r *ghttp.Request) {
|
||||
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
|
||||
writer.WriteHeader(http.StatusBadGateway)
|
||||
}
|
||||
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
|
||||
writer.WriteHeader(http.StatusBadGateway)
|
||||
}
|
||||
s.BindHandler("/proxy/*url", func(r *ghttp.Request) {
|
||||
var (
|
||||
originalPath = r.Request.URL.Path
|
||||
proxyToPath = "/" + r.Get("url").String()
|
||||
)
|
||||
r.Request.URL.Path = proxyToPath
|
||||
g.Log().Infof(r.Context(), `server2:"%s" -> server1:"%s"`, originalPath, proxyToPath)
|
||||
proxy.ServeHTTP(r.Response.Writer.RawWriter(), r.Request)
|
||||
})
|
||||
s.SetPort(PortOfServer2)
|
||||
|
||||
Reference in New Issue
Block a user