Skip to content

Commit

Permalink
merge upstream and fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin1989 committed Oct 13, 2024
2 parents c27745d + b6f4ae1 commit 49a2c24
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 68 deletions.
5 changes: 4 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

var modName string
var module bool
var projectType string

// initCmd initializes the service
var initCmd = &cobra.Command{
Expand All @@ -32,7 +33,8 @@ 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), v3.WithProtocCmd(protocCmd))))
options = append(options, svc.WithJsonCase(naming), svc.WithCaseConverter(fn), svc.WithProjectType(projectType),
svc.WithProtoGenerator(v3.NewProtoGenerator(v3.WithFieldNamingFunc(fn), v3.WithProtocCmd(protocCmd))))
s := svc.NewSvc(svcdir, options...)
s.Init()
},
Expand All @@ -46,4 +48,5 @@ func init() {
initCmd.Flags().StringVarP(&docfile, "file", "f", "", `OpenAPI 3.0 or Swagger 2.0 spec json file path or download link`)
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().StringVar(&naming, "case", "lowerCamel", `protobuf message field and json tag case, only support "lowerCamel" and "snake"`)
initCmd.Flags().StringVarP(&projectType, "type", "t", "grpc", `Indicate project type, accept values: grpc or rest`)
}
7 changes: 4 additions & 3 deletions cmd/internal/modular/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func (receiver *Work) Init() {
if err = receiver.runner.Run("go", "work", "use", "main"); err != nil {
panic(err)
}
if err = receiver.runner.Run("go", "work", "sync"); err != nil {
panic(err)
}
// Comment below code due to performance issue
//if err = receiver.runner.Run("go", "work", "sync"); err != nil {
// panic(err)
//}
}
15 changes: 8 additions & 7 deletions cmd/internal/svc/codegen/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ func (b *AbstractBaseGenerator) GenService() {
}

func (b *AbstractBaseGenerator) goModTidy() {
wd, _ := os.Getwd()
os.Chdir(filepath.Join(b.Dir))
err := b.runner.Run("go", "mod", "tidy")
if err != nil {
panic(err)
}
os.Chdir(wd)
// here go mod tidy cause performance issue on some computer
//wd, _ := os.Getwd()
//os.Chdir(filepath.Join(b.Dir))
//err := b.runner.Run("go", "mod", "tidy")
//if err != nil {
// panic(err)
//}
//os.Chdir(wd)
}
1 change: 0 additions & 1 deletion cmd/internal/svc/codegen/httphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
"github.com/unionj-cloud/go-doudou/v2/framework"
"net/http"
"os"
)
type {{.Meta.Name}}Handler interface {
Expand Down
12 changes: 5 additions & 7 deletions cmd/internal/svc/codegen/httphandlerimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,10 @@ var appendHttpHandlerImplTmpl = `
var importTmpl = `
"context"
"github.com/bytedance/sonic"
"fmt"
"github.com/sirupsen/logrus"
v3 "github.com/unionj-cloud/toolkit/openapi/v3"
"github.com/unionj-cloud/go-doudou/v2/framework/rest"
"github.com/unionj-cloud/go-doudou/v2/framework/rest/httprouter"
"github.com/unionj-cloud/toolkit/cast"
{{.ServiceAlias}} "{{.ServicePackage}}"
"{{.DtoPackage}}"
"net/http"
"github.com/pkg/errors"
`

var initHttpHandlerImplTmpl = templates.EditableHeaderTmpl + `package httpsrv
Expand Down Expand Up @@ -521,15 +516,18 @@ func GenHttpHandlerImpl(dir string, ic astutils.InterfaceCollector, config GenHt
}

original = append(original, buf.Bytes()...)
if tpl, err = template.New("himportimpl.go.tmpl").Parse(importTmpl); err != nil {
if tpl, err = template.New(importTmpl).Parse(importTmpl); err != nil {
panic(err)
}
dtoPkg := astutils.GetPkgPath(filepath.Join(dir, "dto"))
if err = tpl.Execute(&importBuf, struct {
ServicePackage string
ServiceAlias string
DtoPackage string
}{
ServicePackage: servicePkg,
ServiceAlias: ic.Package.Name,
DtoPackage: dtoPkg,
}); err != nil {
panic(err)
}
Expand Down
62 changes: 37 additions & 25 deletions cmd/internal/svc/codegen/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import (
"{{.DtoPackage}}"
)
{{- if eq .ProjectType "rest" }}
//go:generate go-doudou svc http --case {{ .JsonCase }}
{{- else }}
//go:generate go-doudou svc grpc --http2grpc --case {{ .JsonCase }}
{{- end }}
type {{.SvcName}} interface {
// You can define your service methods as your need. Below is an example.
Expand Down Expand Up @@ -115,7 +119,6 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/gorilla/handlers v1.5.1
github.com/iancoleman/strcase v0.1.3
github.com/jmoiron/sqlx v1.3.1
github.com/opentracing-contrib/go-stdlib v1.0.0
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -179,25 +182,27 @@ type InitProjConfig struct {
ProtoGenerator v3.ProtoGenerator
JsonCase string
DocPath string
ProjectType string
}

// InitProj inits a service project
// dir is root path
// modName is module name
func InitProj(conf InitProjConfig) {
var (
err error
svcName string
svcfile string
dtodir string
dtofile string
goVersion string
f *os.File
tpl *template.Template
envfile string
docPath string
err error
svcName string
svcfile string
dtodir string
dtofile string
goVersion string
f *os.File
tpl *template.Template
envfile string
docPath string
projectType string
)
dir, modName, runner, module, jsonCase, docPath := conf.Dir, conf.ModName, conf.Runner, conf.Module, conf.JsonCase, conf.DocPath
dir, modName, runner, module, jsonCase, docPath, projectType := conf.Dir, conf.ModName, conf.Runner, conf.Module, conf.JsonCase, conf.DocPath, conf.ProjectType
if stringutils.IsEmpty(dir) {
dir, _ = os.Getwd()
}
Expand Down Expand Up @@ -279,15 +284,17 @@ func InitProj(conf InitProjConfig) {

tpl, _ = template.New(svcTmpl).Parse(svcTmpl)
_ = tpl.Execute(f, struct {
DtoPackage string
SvcName string
Version string
JsonCase string
DtoPackage string
SvcName string
Version string
JsonCase string
ProjectType string
}{
DtoPackage: strings.ReplaceAll(filepath.Join(modName, "dto"), string(os.PathSeparator), "/"),
SvcName: svcName,
Version: version.Release,
JsonCase: jsonCase,
DtoPackage: strings.ReplaceAll(filepath.Join(modName, "dto"), string(os.PathSeparator), "/"),
SvcName: svcName,
Version: version.Release,
JsonCase: jsonCase,
ProjectType: projectType,
})
}

Expand Down Expand Up @@ -319,8 +326,12 @@ func InitProj(conf InitProjConfig) {
if module {
parser.ParseDto(dir, parser.DEFAULT_DTO_PKGS...)
ic := astutils.BuildInterfaceCollector(filepath.Join(dir, "svc.go"), astutils.ExprString)
genPlugin(dir, ic)
genMainModule(dir)
genPlugin(dir, ic, CodeGenConfig{
ProjectType: projectType,
})
genMain(dir, CodeGenConfig{
ProjectType: projectType,
})
mainMainFile := filepath.Join(filepath.Dir(dir), "main", "cmd", "main.go")
fileContent, err := ioutil.ReadFile(mainMainFile)
if err != nil {
Expand All @@ -329,9 +340,10 @@ func InitProj(conf InitProjConfig) {
pluginPkg := astutils.GetPkgPath(filepath.Join(dir, "plugin"))
original := astutils.AppendImportStatements(fileContent, []byte(fmt.Sprintf(`_ "%s"`, pluginPkg)))
astutils.FixImport(original, mainMainFile)
if err = runner.Run("go", "work", "sync"); err != nil {
panic(err)
}
// Comment below code due to performance issue
//if err = runner.Run("go", "work", "sync"); err != nil {
// panic(err)
//}
}
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/internal/svc/codegen/mainmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/unionj-cloud/go-doudou/v2/version"
)

func genMainModule(dir string) {
func genMain(dir string, conf CodeGenConfig) {
var (
err error
mainfile string
Expand All @@ -31,16 +31,18 @@ func genMainModule(dir string) {
}
defer f.Close()

if tpl, err = template.New(templates.MainModuleTmpl).Parse(templates.MainModuleTmpl); err != nil {
if tpl, err = template.New(templates.MainTmpl).Parse(templates.MainTmpl); err != nil {
panic(err)
}
pluginPkg := astutils.GetPkgPath(filepath.Join(dir, "plugin"))
if err = tpl.Execute(&buf, struct {
CodeGenConfig
PluginPackage string
Version string
}{
PluginPackage: pluginPkg,
Version: version.Release,
CodeGenConfig: conf,
}); err != nil {
panic(err)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/internal/svc/codegen/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import (
"github.com/unionj-cloud/go-doudou/v2/version"
)

func genPlugin(dir string, ic astutils.InterfaceCollector) {
type CodeGenConfig struct {
ProjectType string
}

func genPlugin(dir string, ic astutils.InterfaceCollector, conf CodeGenConfig) {
var (
err error
pluginFile string
Expand Down Expand Up @@ -44,6 +48,7 @@ func genPlugin(dir string, ic astutils.InterfaceCollector) {
svcName := ic.Interfaces[0].Name
alias := ic.Package.Name
if err = tpl.Execute(&buf, struct {
CodeGenConfig
ServicePackage string
ConfigPackage string
TransportGrpcPackage string
Expand All @@ -59,6 +64,7 @@ func genPlugin(dir string, ic astutils.InterfaceCollector) {
ServiceAlias: alias,
SvcName: svcName,
Version: version.Release,
CodeGenConfig: conf,
}); err != nil {
panic(err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/internal/svc/codegen/svcimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var svcimportTmpl = `
"context"
"{{.ConfigPackage}}"
"github.com/jmoiron/sqlx"
"{{.DtoPackage}}"
"github.com/brianvoe/gofakeit/v6"
`

Expand Down Expand Up @@ -66,6 +66,7 @@ var svcimportTmplGrpc = `
"context"
"{{.ConfigPackage}}"
pb "{{.PbPackage}}"
"google.golang.org/protobuf/types/known/emptypb"
`

var appendPartGrpc = `{{- range $m := .GrpcSvc.Rpcs }}
Expand Down Expand Up @@ -192,13 +193,16 @@ func GenSvcImpl(dir string, ic astutils.InterfaceCollector) {
}

original = append(original, buf.Bytes()...)
dtoPkg := astutils.GetPkgPath(filepath.Join(dir, "dto"))
if tpl, err = template.New(svcimportTmpl).Parse(svcimportTmpl); err != nil {
panic(err)
}
if err = tpl.Execute(&importBuf, struct {
ConfigPackage string
DtoPackage string
}{
ConfigPackage: cfgPkg,
DtoPackage: dtoPkg,
}); err != nil {
panic(err)
}
Expand Down
32 changes: 21 additions & 11 deletions cmd/internal/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ type Svc struct {
JsonCase string
CaseConverter func(string) string

http2grpc bool
http2grpc bool
projectType string
}

type DbConfig struct {
Expand Down Expand Up @@ -164,11 +165,12 @@ func (receiver *Svc) Http() {
RoutePatternStrategy: receiver.RoutePatternStrategy,
AllowGetWithReqBody: receiver.AllowGetWithReqBody,
})
runner := receiver.runner
if runner == nil {
runner = executils.CmdRunner{}
}
runner.Run("go", "mod", "tidy")
// here go mod tidy cause performance issue on some computer
//runner := receiver.runner
//if runner == nil {
// runner = executils.CmdRunner{}
//}
//runner.Run("go", "mod", "tidy")
}

// Init inits a project
Expand All @@ -180,6 +182,7 @@ func (receiver *Svc) Init() {
Module: receiver.module,
ProtoGenerator: receiver.protoGenerator,
JsonCase: receiver.JsonCase,
ProjectType: receiver.projectType,
})
}

Expand Down Expand Up @@ -268,6 +271,12 @@ func WithJsonCase(jsonCase string) SvcOption {
}
}

func WithProjectType(projectType string) SvcOption {
return func(svc *Svc) {
svc.projectType = projectType
}
}

func WithProtoGenerator(protoGenerator v3.ProtoGenerator) SvcOption {
return func(svc *Svc) {
svc.protoGenerator = protoGenerator
Expand Down Expand Up @@ -544,9 +553,10 @@ func (receiver *Svc) Grpc() {
}
codegen.FixModGrpc(dir)
codegen.GenMethodAnnotationStore(dir, ic)
runner := receiver.runner
if runner == nil {
runner = executils.CmdRunner{}
}
runner.Run("go", "mod", "tidy")
// here go mod tidy cause performance issue on some computer
//runner := receiver.runner
//if runner == nil {
// runner = executils.CmdRunner{}
//}
//runner.Run("go", "mod", "tidy")
}
Loading

0 comments on commit 49a2c24

Please sign in to comment.