Skip to content

Commit

Permalink
perf: 减少protoc-gen-route import引入判断
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 9, 2024
1 parent c164941 commit 0300f58
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
3 changes: 0 additions & 3 deletions api/bot/v1/counter_bot.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions contrib/protoc-gen-route/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func genConfig() (*Config, error) {
requestType: NewGoIdent(*requestModel),
responseType: NewGoIdent(*responseModel),
extraType: NewGoIdent(*extraDataModel),
extraConstructor: NewGoIdent(*extraDataConstructor),
extraConstructor: NewGoIdent(*extraDataConstructor, identIsFunc()),
}
if cfg.requestType == nil {
return nil, fmt.Errorf("flag request_model must be set")
Expand All @@ -68,8 +68,9 @@ func genConfig() (*Config, error) {
}

type GoIdent struct {
pkg protogen.GoImportPath
ident string
pkg protogen.GoImportPath
ident string
isFunc bool
}

func (g GoIdent) GoIdent() protogen.GoIdent {
Expand All @@ -87,7 +88,7 @@ type Config struct {
extraConstructor *GoIdent
}

func NewGoIdent(s string) *GoIdent {
func NewGoIdent(s string, options ...func(*GoIdent)) *GoIdent {
parts := strings.Split(s, ";")
if len(parts) != 2 {
return nil
Expand All @@ -97,3 +98,9 @@ func NewGoIdent(s string) *GoIdent {
ident: parts[1],
}
}

func identIsFunc() func(*GoIdent) {
return func(g *GoIdent) {
g.isFunc = true
}
}
22 changes: 18 additions & 4 deletions contrib/protoc-gen-route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,24 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
return
}
g.P("var _ = new(", contextPackage.Ident("Context"), ")")
g.P("var _ = new(", conf.requestType.GoIdent(), ")")
g.P("var _ = new(", conf.responseType.GoIdent(), ")")
g.P("var _ = new(", conf.extraType.GoIdent(), ")")
g.P("var _ = ", conf.extraConstructor.GoIdent())
idents := []*GoIdent{
conf.requestType,
conf.responseType,
conf.extraType,
conf.extraConstructor,
}
imported := make(map[string]struct{}, len(idents))
for _, i := range idents {
_, exist := imported[string(i.pkg)]
if !exist {
if i.isFunc {
g.P("var _ = ", i.GoIdent())
} else {
g.P("var _ = new(", i.GoIdent(), ")")
}
imported[string(i.pkg)] = struct{}{}
}
}
g.P()
for _, service := range file.Services {
genService(gen, file, g, service, conf)
Expand Down

0 comments on commit 0300f58

Please sign in to comment.