Skip to content

Commit

Permalink
fix retag module path
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee authored Dec 13, 2024
1 parent 24e81c9 commit 9664060
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 296 deletions.
20 changes: 12 additions & 8 deletions cmd/protobuild/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ func Main() *cli.Command {
base += fmt.Sprintf(" -I %s", pluginCfg.Includes[i])
}

reTagOut := ""
reTagOpt := ""
reTagData := ""
for i := range pluginCfg.Plugins {
plg := pluginCfg.Plugins[i]
if plg.SkipRun {
Expand Down Expand Up @@ -269,8 +268,13 @@ func Main() *cli.Command {
}

if name == reTagPluginName {
reTagOut = fmt.Sprintf(" --%s_out=%s", name, out)
reTagOpt = fmt.Sprintf(" --%s_opt=%s", name, strings.Join(opts, ","))
reTagData = fmt.Sprintf(" --%s_out=%s", name, out)
opts = append(opts, "__out="+out)

reTagData += fmt.Sprintf(" --%s_opt=%s", name, strings.Join(opts, ","))
if plg.Path != "" {
reTagData += fmt.Sprintf(" --plugin=protoc-gen-%s=%s", name, plg.Path)
}
continue
}

Expand All @@ -288,11 +292,11 @@ func Main() *cli.Command {
}
data = base + data + " " + filepath.Join(protoPath, "*.proto")
logger.Info().Msg(data)
assert.Must(shutil.Shell(data).Run(), data)
if reTagOut != "" && reTagOpt != "" {
data = base + reTagOut + reTagOpt + " " + filepath.Join(protoPath, "*.proto")
assert.Exit(shutil.Shell(data).Run(), data)
if reTagData != "" {
data = base + reTagData + " " + filepath.Join(protoPath, "*.proto")
logger.Info().Bool(reTagPluginName, true).Msg(data)
assert.Must(shutil.Shell(data).Run(), data)
assert.Exit(shutil.Shell(data).Run(), data)
}
}
doF(pp, protoSourcePath)
Expand Down
16 changes: 15 additions & 1 deletion cmd/protoc-gen-retag/ast/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"go/format"
"go/parser"
"go/token"
"path/filepath"
"strings"

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
Expand Down Expand Up @@ -88,7 +90,8 @@ func (g *Generator) Generate() {
// fix Response will always be generated, so add a new generated file directly.
// content := buf.String()
// file.outerFile.Content = &content
_, err = g.protoGenerator.NewGeneratedFile(file.outerFile.GetName(), "").Write(buf.Bytes())
filePath := filepath.Join(getModule(g.protoGenerator), file.outerFile.GetName())
_, err = g.protoGenerator.NewGeneratedFile(filePath, "").Write(buf.Bytes())
if err != nil {
g.protoGenerator.Error(fmt.Errorf("failed to new generated file to rewrite: %w", err))
continue
Expand All @@ -115,3 +118,14 @@ func PrintComment(file *ast.File) {
lists = append(lists, &list)
comments.List = append(lists, comments.List...)
}

func getModule(gen *protogen.Plugin) string {
params := gen.Request.GetParameter()
for _, p := range strings.Split(params, ",") {
p = strings.TrimSpace(p)
if strings.HasPrefix(p, "module=") {
return strings.TrimPrefix(p, "module=")
}
}
return ""
}
51 changes: 31 additions & 20 deletions cmd/protoc-gen-retag/ast/parse_and_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package ast

import (
"flag"

retagpb "github.com/pubgo/protobuild/pkg/retag"
"github.com/searKing/golang/go/reflect"
strings_ "github.com/searKing/golang/go/strings"
Expand All @@ -13,6 +15,8 @@ import (
"google.golang.org/protobuf/types/descriptorpb"
)

var OutputPkg = flag.String("__out", "", "output pkg")

type FieldInfo struct {
FieldNameInProto string
FieldNameInGo string
Expand Down Expand Up @@ -56,15 +60,17 @@ func WalkDescriptorProto(g *protogen.Plugin, dp *descriptorpb.DescriptorProto, t
}

f := HandleFieldDescriptorProto(field)
if f != nil {
if oneOfS != nil {
oneOfS.FieldInfos = append(oneOfS.FieldInfos, *f)
if len(oneOfS.FieldInfos) > 0 {
ss = append(ss, *oneOfS)
}
} else {
s.FieldInfos = append(s.FieldInfos, *f)
if f == nil {
continue
}

if oneOfS != nil {
oneOfS.FieldInfos = append(oneOfS.FieldInfos, *f)
if len(oneOfS.FieldInfos) > 0 {
ss = append(ss, *oneOfS)
}
} else {
s.FieldInfos = append(s.FieldInfos, *f)
}
}

Expand All @@ -76,18 +82,22 @@ func WalkDescriptorProto(g *protogen.Plugin, dp *descriptorpb.DescriptorProto, t
continue
}

if decl.GetOptions() != nil {
v, ok := proto.GetExtension(decl.GetOptions(), retagpb.E_OneofTags).([]*retagpb.Tag)
if ok {
info := FieldInfo{FieldNameInProto: decl.GetName(), FieldNameInGo: CamelCase(decl.GetName())}
for i := range v {
tag := reflect.StructTag{}
tag.SetName(v[i].Name, v[i].Value)
info.FieldTag = append(info.FieldTag, tag)
}
s.FieldInfos = append(s.FieldInfos, info)
}
if decl.GetOptions() == nil {
continue
}

v, ok := proto.GetExtension(decl.GetOptions(), retagpb.E_OneofTags).([]*retagpb.Tag)
if !ok || len(v) == 0 {
continue
}

info := FieldInfo{FieldNameInProto: decl.GetName(), FieldNameInGo: CamelCase(decl.GetName())}
for i := range v {
tag := reflect.StructTag{}
tag.SetName(v[i].Name, v[i].Value)
info.FieldTag = append(info.FieldTag, tag)
}
s.FieldInfos = append(s.FieldInfos, info)

ss = append(ss, *declS)
}
Expand Down Expand Up @@ -157,7 +167,8 @@ func Rewrite(g *protogen.Plugin) {
//if len(protoFiles) == 0 {
// return
//}
// g.Response() will generate files, so skip this step
//
//// g.Response() will generate files, so skip this step
//if len(g.Response().GetFile()) == 0 {
// return
//}
Expand Down
13 changes: 9 additions & 4 deletions cmd/protoc-gen-retag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
package main

import (
"flag"

"github.com/pubgo/protobuild/cmd/protoc-gen-retag/ast"

gengo "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo"
"google.golang.org/protobuf/compiler/protogen"
)

func main() {
protogen.Options{}.Run(func(gen *protogen.Plugin) error {
protogen.Options{ParamFunc: flag.CommandLine.Set}.Run(func(gen *protogen.Plugin) error {
gen.SupportedFeatures = gengo.SupportedFeatures
var originFiles []*protogen.GeneratedFile

for _, f := range gen.Files {
if f.Generate {
originFiles = append(originFiles, gengo.GenerateFile(gen, f))
if !f.Generate {
continue
}

originFiles = append(originFiles, gengo.GenerateFile(gen, f))
}

ast.Rewrite(gen)

for _, f := range originFiles {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/pubgo/protobuild

go 1.22
go 1.22.6

require (
github.com/a8m/envsubst v1.3.0
Expand All @@ -27,7 +27,7 @@ require (
golang.org/x/mod v0.17.0
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.33.0
google.golang.org/protobuf v1.35.2
gopkg.in/yaml.v3 v3.0.1
gorm.io/gorm v1.24.5
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
98 changes: 20 additions & 78 deletions pkg/cmd/protoc-gen-retag/example/example.pb.go

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

Loading

0 comments on commit 9664060

Please sign in to comment.