Skip to content

Commit

Permalink
feat: Optimize template
Browse files Browse the repository at this point in the history
  • Loading branch information
fynntang committed Jul 18, 2023
1 parent 3557898 commit 7e75138
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
28 changes: 13 additions & 15 deletions protoc-gen-go-crud/deliveries/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ type {{.ServiceType}}Service struct {
{{$firstLetter}}c usecase.I{{.ServiceType}}UseCase
}
func New{{.ServiceType}}Service({{$firstLetter}}c usecase.I{{$svrType}}UseCase) {{$packageName}}.{{.ServiceType}}HTTPServer{
return &{{.ServiceType}}Service{
{{$firstLetter}}c: {{$firstLetter}}c,
}
}
func (s *{{.ServiceType}}Service) log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("{{.ServiceType}}Service")
}
{{- range .MethodSets}}
{{$request := .Request}}
{{$reply := .Reply}}
Expand All @@ -29,24 +39,12 @@ type {{.ServiceType}}Service struct {
{{$reply = printf "%s.%s" .PackageName .Reply}}
{{- end}}
func ({{$firstLetter}} {{$svrType}}Service){{.OriginalName}}(c *gin.Context, in *{{$request}})(*{{$reply}},error) {
panic("todo")
func (s *{{$svrType}}Service){{.OriginalName}}(c *gin.Context, in *{{$request}})(*{{$reply}},error) {
//TODO implement me
panic("implement me")
}
{{- end}}
func ({{$firstLetter}} {{.ServiceType}}Service) Log(c *gin.Context) *zap.SugaredLogger {
return global.Logger(c).Named("{{.ServiceType}}Repo")
}
func New{{.ServiceType}}Service({{$firstLetter}}c usecase.I{{$svrType}}UseCase) {{$packageName}}.{{.ServiceType}}HTTPServer{
return &{{.ServiceType}}Service{
{{$firstLetter}}c: {{$firstLetter}}c,
}
}
`

type serviceDesc struct {
Expand Down
43 changes: 24 additions & 19 deletions protoc-gen-go-crud/repositories/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,58 @@ import (
var crudTemplate = `
type I{{.ServiceType}}Repo interface {
log(ctx context.Context) *zap.SugaredLogger
Create{{.ServiceType}}(ctx context.Context, {{.ServiceType}} *entities.{{.ServiceType}}) error
Update{{.ServiceType}}(ctx context.Context, updateFields []string,{{.ServiceType}} *entities.{{.ServiceType}}) error
Get{{.ServiceType}}(ctx context.Context, id entity.ID) (*entities.{{.ServiceType}}, error)
Get{{.ServiceType}}s(ctx context.Context, filter *database.Filter) (res []*entities.{{.ServiceType}}, count int64, err error)
Delete{{.ServiceType}}(ctx context.Context, id entity.ID) error
Log(ctx context.Context) *zap.SugaredLogger
}
{{$firstLetter := (GetFirstLetter .ServiceType)}}
{{$entityName := (GetEntityName .ServiceType)}}
type {{.ServiceType}}Repo struct {
}
type {{.ServiceType}}Repo struct {}
func ({{$firstLetter}} *{{.ServiceType}}Repo) DB(ctx context.Context) *gorm.DB {
return global.DBFromContext(ctx).Model(&entities.{{.ServiceType}}{})
func New{{.ServiceType}}Repo() I{{.ServiceType}}Repo{
return &{{.ServiceType}}Repo{}
}
func ({{$firstLetter}} {{.ServiceType}}Repo) Create{{.ServiceType}}(ctx context.Context, {{$entityName}} *entities.{{.ServiceType}}) error {
panic("todo")
func ({{$firstLetter}} *{{.ServiceType}}Repo) log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("{{.ServiceType}}Repo")
}
func ({{$firstLetter}} {{.ServiceType}}Repo) Update{{.ServiceType}}(ctx context.Context, updateFields []string, {{$entityName}} *entities.{{.ServiceType}}) error {
panic("todo")
func ({{$firstLetter}} *{{.ServiceType}}Repo) DB(ctx context.Context) *gorm.DB {
return global.DBFromContext(ctx).Model(&entities.{{.ServiceType}}{})
}
func ({{$firstLetter}} {{.ServiceType}}Repo) Get{{.ServiceType}}(ctx context.Context, id entity.ID) (*entities.{{.ServiceType}}, error) {
panic("todo")
func (r *{{.ServiceType}}Repo) Create{{.ServiceType}}(ctx context.Context, {{$entityName}} *entities.{{.ServiceType}}) error {
//TODO implement me
panic("implement me")
}
func ({{$firstLetter}} {{.ServiceType}}Repo) Delete{{.ServiceType}}(ctx context.Context, id entity.ID) error {
panic("todo")
func (r *{{.ServiceType}}Repo) Update{{.ServiceType}}(ctx context.Context, updateFields []string, {{$entityName}} *entities.{{.ServiceType}}) error {
//TODO implement me
panic("implement me")
}
func ({{$firstLetter}} {{.ServiceType}}Repo) Get{{.ServiceType}}s(ctx context.Context, filter *database.Filter) (res []*entities.{{.ServiceType}}, count int64, err error) {
panic("todo")
func (r *{{.ServiceType}}Repo) Get{{.ServiceType}}(ctx context.Context, id entity.ID) (*entities.{{.ServiceType}}, error) {
//TODO implement me
panic("implement me")
}
func ({{$firstLetter}} {{.ServiceType}}Repo) Log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("{{.ServiceType}}Repo")
func (r *{{.ServiceType}}Repo) Delete{{.ServiceType}}(ctx context.Context, id entity.ID) error {
//TODO implement me
panic("implement me")
}
func New{{.ServiceType}}Repo() I{{.ServiceType}}Repo{
return &{{.ServiceType}}Repo{}
func (r *{{.ServiceType}}Repo) Get{{.ServiceType}}s(ctx context.Context, filter *database.Filter) (res []*entities.{{.ServiceType}}, count int64, err error) {
//TODO implement me
panic("implement me")
}
`

type serviceDesc struct {
Expand Down
36 changes: 21 additions & 15 deletions protoc-gen-go-crud/usecase/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,51 @@ var crudTemplate = `
type I{{.ServiceType}}UseCase interface {
log(ctx context.Context) *zap.SugaredLogger
Create{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Create{{.ServiceType}}Request) error
Update{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Update{{.ServiceType}}Request) error
Delete{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Delete{{.ServiceType}}Request) error
Get{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Get{{.ServiceType}}Request) (*entities.{{.ServiceType}},error)
List{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.List{{.ServiceType}}Request) ([]*entities.{{.ServiceType}},int64,error)
Log(ctx context.Context) *zap.SugaredLogger
}
type {{.ServiceType}}UseCase struct {
}
type {{.ServiceType}}UseCase struct {}
func New{{.ServiceType}}UseCase() I{{.ServiceType}}UseCase {
return &{{.ServiceType}}UseCase{}
}
func ({{$firstLetter}} *{{.ServiceType}}UseCase) Create{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Create{{.ServiceType}}Request) error {
panic("todo")
func (uc *{{.ServiceType}}UseCase) log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("{{.ServiceType}}UseCase")
}
func ({{$firstLetter}} *{{.ServiceType}}UseCase) Update{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Update{{.ServiceType}}Request) error {
panic("todo")
func (uc *{{.ServiceType}}UseCase) Create{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Create{{.ServiceType}}Request) error {
//TODO implement me
panic("implement me")
}
func ({{$firstLetter}} *{{.ServiceType}}UseCase) Delete{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Delete{{.ServiceType}}Request) error{
panic("todo")
func (uc *{{.ServiceType}}UseCase) Update{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Update{{.ServiceType}}Request) error {
//TODO implement me
panic("implement me")
}
func ({{$firstLetter}} *{{.ServiceType}}UseCase) Get{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Get{{.ServiceType}}Request) (*entities.{{.ServiceType}},error){
panic("todo")
func (uc *{{.ServiceType}}UseCase) Delete{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Delete{{.ServiceType}}Request) error{
//TODO implement me
panic("implement me")
}
func ({{$firstLetter}} *{{.ServiceType}}UseCase) List{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.List{{.ServiceType}}Request) ([]*entities.{{.ServiceType}},int64,error){
panic("todo")
func (uc *{{.ServiceType}}UseCase) Get{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Get{{.ServiceType}}Request) (*entities.{{.ServiceType}},error){
//TODO implement me
panic("implement me")
}
func ({{$firstLetter}} {{.ServiceType}}UseCase) Log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("{{.ServiceType}}Repo")
func (uc *{{.ServiceType}}UseCase) List{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.List{{.ServiceType}}Request) ([]*entities.{{.ServiceType}},int64,error){
//TODO implement me
panic("implement me")
}
`

type serviceDesc struct {
Expand Down

0 comments on commit 7e75138

Please sign in to comment.