Skip to content

Commit

Permalink
add grpc_gen_cmd flag to go-doudou svc init and go-doudou svc grpc co…
Browse files Browse the repository at this point in the history
…mmands to support custom protoc command
  • Loading branch information
wubin48435 committed Sep 28, 2024
1 parent 0f58ae7 commit 440d711
Show file tree
Hide file tree
Showing 11 changed files with 1,058 additions and 31 deletions.
8 changes: 5 additions & 3 deletions cmd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var naming string
var http2grpc bool
var annotatedOnly bool
var protocCmd string

var grpcCmd = &cobra.Command{
Use: "grpc",
Expand All @@ -23,7 +24,7 @@ var grpcCmd = &cobra.Command{
fn = strcase.ToSnake
}
s := svc.NewSvc("",
svc.WithProtoGenerator(v3.NewProtoGenerator(v3.WithFieldNamingFunc(fn), v3.WithAnnotatedOnly(annotatedOnly))),
svc.WithProtoGenerator(v3.NewProtoGenerator(v3.WithFieldNamingFunc(fn), v3.WithAnnotatedOnly(annotatedOnly), v3.WithProtocCmd(protocCmd))),
svc.WithHttp2Grpc(http2grpc),
svc.WithAllowGetWithReqBody(allowGetWithReqBody),
svc.WithCaseConverter(fn),
Expand All @@ -37,7 +38,8 @@ func init() {
svcCmd.AddCommand(grpcCmd)
grpcCmd.Flags().BoolVarP(&omitempty, "omitempty", "o", false, `if true, ",omitempty" will be appended to json tag of fields in every generated anonymous struct in handlers`)
grpcCmd.Flags().StringVar(&naming, "case", "lowerCamel", `protobuf message field naming strategy, only support "lowerCamel" and "snake"`)
grpcCmd.Flags().StringVar(&protocCmd, "grpc_gen_cmd", "protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --go-json_out=. --go-json_opt=paths=source_relative", `command to generate grpc service and message code`)
grpcCmd.Flags().BoolVar(&http2grpc, "http2grpc", false, `whether need RESTful api for your grpc service`)
grpcCmd.Flags().BoolVar(&allowGetWithReqBody, "allowGetWithReqBody", false, "Whether allow get http request with request body.")
grpcCmd.Flags().BoolVar(&annotatedOnly, "annotatedOnly", false, "Whether generate grpc api only for method annotated with @grpc or not")
grpcCmd.Flags().BoolVar(&allowGetWithReqBody, "allow_get_body", false, "Whether allow get http request with request body.")
grpcCmd.Flags().BoolVar(&annotatedOnly, "annotated_only", false, "Whether generate grpc api only for method annotated with @grpc or not")
}
3 changes: 2 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var initCmd = &cobra.Command{
case "snake":
fn = strcase.ToSnake
}
options = append(options, svc.WithJsonCase(naming), svc.WithCaseConverter(fn), svc.WithProtoGenerator(v3.NewProtoGenerator(v3.WithFieldNamingFunc(fn))))
options = append(options, svc.WithJsonCase(naming), svc.WithCaseConverter(fn), svc.WithProtoGenerator(v3.NewProtoGenerator(v3.WithFieldNamingFunc(fn), v3.WithProtocCmd(protocCmd))))
s := svc.NewSvc(svcdir, options...)
s.Init()
},
Expand All @@ -77,6 +77,7 @@ func init() {
initCmd.Flags().StringVar(&dbDsn, "db_dsn", "", `Specify database connection url`)
initCmd.Flags().StringVar(&dbSoft, "db_soft", "deleted_at", `Specify database soft delete column name`)
initCmd.Flags().BoolVar(&dbGrpc, "db_grpc", false, `If true, grpc code will also be generated`)
initCmd.Flags().StringVar(&protocCmd, "grpc_gen_cmd", "protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --go-json_out=. --go-json_opt=paths=source_relative", `command to generate grpc service and message code`)
initCmd.Flags().BoolVar(&dbService, "db_service", false, `If false, service will not be generated, and db_grpc will be ignored. Only dao layer code will be generated.`)
initCmd.Flags().BoolVar(&dbGenGenGo, "db_gen_gen", false, `whether generate gen.go file`)
initCmd.Flags().StringVar(&dbTablePrefix, "db_table_prefix", "", `table prefix or schema name for pg`)
Expand Down
15 changes: 5 additions & 10 deletions cmd/internal/svc/codegen/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type OrmGeneratorConfig struct {
Dir string
Soft string
Grpc bool
ProtoGenerator v3.ProtoGenerator
Omitempty bool
}

Expand Down Expand Up @@ -71,6 +72,7 @@ type AbstractBaseGenerator struct {
Dir string
g *gormgen.Generator
CaseConverter func(string) string
ProtoGenerator v3.ProtoGenerator
Omitempty bool
AllowGetWithReqBody bool
Client bool
Expand Down Expand Up @@ -174,19 +176,12 @@ func (b *AbstractBaseGenerator) GenService() {
})

if b.Grpc {
p := v3.NewProtoGenerator(v3.WithFieldNamingFunc(b.CaseConverter))
parser.ParseDtoGrpc(b.Dir, p, "dto")
grpcSvc, protoFile := codegen.GenGrpcProto(b.Dir, ic, p)
parser.ParseDtoGrpc(b.Dir, b.ProtoGenerator, "dto")
grpcSvc, protoFile := codegen.GenGrpcProto(b.Dir, ic, b.ProtoGenerator)
protoFile, _ = filepath.Rel(b.Dir, protoFile)
wd, _ := os.Getwd()
os.Chdir(filepath.Join(b.Dir))
// protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative transport/grpc/helloworld.proto
if err = b.runner.Run("protoc", "--proto_path=.",
"--go_out=.",
"--go_opt=paths=source_relative",
"--go-grpc_out=.",
"--go-grpc_opt=paths=source_relative",
protoFile); err != nil {
if err := b.ProtoGenerator.Generate(protoFile, b.runner); err != nil {
panic(err)
}
os.Chdir(wd)
Expand Down
1 change: 1 addition & 0 deletions cmd/internal/svc/codegen/database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (gg *GormGenerator) Initialize(conf OrmGeneratorConfig) {
gg.TableExcludeGlob = conf.TableExcludeGlob
gg.GenGenGo = conf.GenGenGo
gg.CaseConverter = conf.CaseConverter
gg.ProtoGenerator = conf.ProtoGenerator
var db *gorm.DB
var err error
switch gg.Driver {
Expand Down
8 changes: 1 addition & 7 deletions cmd/internal/svc/codegen/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ func genGrpc(dir string, ic astutils.InterfaceCollector, runner executils.Runner
oldWd, _ := os.Getwd()
os.Chdir(dir)
protoFile = strings.TrimPrefix(protoFile, dir+string(filepath.Separator))
// protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative transport/grpc/helloworld.proto
if err := runner.Run("protoc", "--proto_path=.",
"--go_out=.",
"--go_opt=paths=source_relative",
"--go-grpc_out=.",
"--go-grpc_opt=paths=source_relative",
protoFile); err != nil {
if err := protoGenerator.Generate(protoFile, runner); err != nil {
panic(err)
}
os.Chdir(oldWd)
Expand Down
9 changes: 2 additions & 7 deletions cmd/internal/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func (receiver *Svc) Init() {
Dir: receiver.dir,
Soft: receiver.DbConfig.Soft,
Grpc: receiver.DbConfig.Grpc,
ProtoGenerator: receiver.protoGenerator,
Omitempty: receiver.DbConfig.Omitempty,
})
if receiver.DbConfig.Service {
Expand Down Expand Up @@ -536,13 +537,7 @@ func (receiver *Svc) Grpc() {
codegen.GenConfig(dir, ic)
parser.ParseDtoGrpc(dir, receiver.protoGenerator, "dto")
grpcSvc, protoFile := codegen.GenGrpcProto(dir, ic, receiver.protoGenerator)
// protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative transport/grpc/helloworld.proto
if err := receiver.runner.Run("protoc", "--proto_path=.",
"--go_out=.",
"--go_opt=paths=source_relative",
"--go-grpc_out=.",
"--go-grpc_opt=paths=source_relative",
protoFile); err != nil {
if err := receiver.protoGenerator.Generate(protoFile, receiver.runner); err != nil {
panic(err)
}
codegen.GenSvcImplGrpc(dir, ic, grpcSvc)
Expand Down
Loading

0 comments on commit 440d711

Please sign in to comment.