Skip to content

Commit

Permalink
add -t/--type flag to go-doudou svc init command
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin48435 committed Oct 8, 2024
1 parent c9626e4 commit 0a80574
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 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`)
}
46 changes: 27 additions & 19 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 @@ -179,25 +183,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 +285,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
10 changes: 9 additions & 1 deletion 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 @@ -180,6 +181,7 @@ func (receiver *Svc) Init() {
Module: receiver.module,
ProtoGenerator: receiver.protoGenerator,
JsonCase: receiver.JsonCase,
ProjectType: receiver.projectType,
})
}

Expand Down Expand Up @@ -268,6 +270,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

0 comments on commit 0a80574

Please sign in to comment.