Skip to content

Commit

Permalink
feat:补充log定义
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiaosu committed Jun 19, 2023
1 parent ce9ef06 commit 77f6432
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 39 deletions.
13 changes: 11 additions & 2 deletions protoc-gen-go-crud/deliveries/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var crudTemplate = `
type {{.ServiceType}}Service struct {
}
{{$firstLetter := (GetFirstLetter .ServiceType)}}
{{$svrType := .ServiceType}}
{{$svrName := .ServiceName}}
{{- range .MethodSets}}
Expand All @@ -25,12 +26,15 @@ type {{.ServiceType}}Service struct {
{{$reply = printf "%s.%s" .PackageName .Reply}}
{{- end}}
func (a {{$svrType}}Service){{.OriginalName}}(*gin.Context, *{{$request}})(*{{$reply}},error) {
func ({{$firstLetter}} {{$svrType}}Service){{.OriginalName}}(c *gin.Context, in *{{$request}})(*{{$reply}},error) {
panic("todo")
}
{{- end}}
func ({{$firstLetter}} {{.ServiceType}}Service) Log(c *gin.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("{{.ServiceType}}Repo")
}
{{- end}}
`

type serviceDesc struct {
Expand Down Expand Up @@ -66,6 +70,7 @@ func (s *serviceDesc) execute() string {
buf := new(bytes.Buffer)
tmpl, err := template.New("crud").Funcs(template.FuncMap{
"IsHasPackagePrefix": IsHasPackagePrefix,
"GetFirstLetter": GetFirstLetter,
}).Parse(strings.TrimSpace(crudTemplate))
if err != nil {
panic(err)
Expand All @@ -79,3 +84,7 @@ func (s *serviceDesc) execute() string {
func IsHasPackagePrefix(s string) bool {
return strings.Contains(s, ".")
}

func GetFirstLetter(s string) string {
return strings.ToLower(s[0:1])
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
type AuthService struct {
}

func (a AuthService) ListMenus(*gin.Context, *empty.Empty) (*authorizationV1.Menus, error) {
func (a AuthService) ListMenus(c *gin.Context, in *empty.Empty) (*authorizationV1.Menus, error) {
panic("todo")
}

func (a AuthService) Log(c *gin.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("AuthRepo")
}
14 changes: 9 additions & 5 deletions protoc-gen-go-crud/internal/authorization/deliveries/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ import (
type RoleService struct {
}

func (a RoleService) CreateRole(*gin.Context, *authorizationV1.CreateRoleRequest) (*empty.Empty, error) {
func (r RoleService) CreateRole(c *gin.Context, in *authorizationV1.CreateRoleRequest) (*empty.Empty, error) {
panic("todo")
}

func (a RoleService) EnableRole(*gin.Context, *authorizationV1.EnableRoleRequest) (*empty.Empty, error) {
func (r RoleService) EnableRole(c *gin.Context, in *authorizationV1.EnableRoleRequest) (*empty.Empty, error) {
panic("todo")
}

func (a RoleService) GetRole(*gin.Context, *authorizationV1.GetRoleRequest) (*components.Role, error) {
func (r RoleService) GetRole(c *gin.Context, in *authorizationV1.GetRoleRequest) (*components.Role, error) {
panic("todo")
}

func (a RoleService) ListRoles(*gin.Context, *authorizationV1.ListRolesRequest) (*authorizationV1.Roles, error) {
func (r RoleService) ListRoles(c *gin.Context, in *authorizationV1.ListRolesRequest) (*authorizationV1.Roles, error) {
panic("todo")
}

func (a RoleService) UpdateRole(*gin.Context, *authorizationV1.UpdateRoleRequest) (*empty.Empty, error) {
func (r RoleService) UpdateRole(c *gin.Context, in *authorizationV1.UpdateRoleRequest) (*empty.Empty, error) {
panic("todo")
}

func (r RoleService) Log(c *gin.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("RoleRepo")
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
package repositories

import (
"context"
"github.com/fynntang/protobuf-gen-go/protoc-gen-go-crud/internal/authorization/entities"
"github.com/fynntang/protobuf-gen-go/protoc-gen-go-crud/pkg/database"
"github.com/fynntang/protobuf-gen-go/protoc-gen-go-crud/pkg/database/entity"
"github.com/fynntang/protobuf-gen-go/protoc-gen-go-crud/pkg/global"
)

type IAuthRepo interface {
CreateAuth(ctx context.Context, Auth *entities.Auth) error
UpdateAuth(ctx context.Context, updateFields []string, Auth *entities.Auth) error
GetAuth(ctx context.Context, id entity.ID) (*entities.Auth, error)
GetAuths(ctx context.Context, filter *database.Filter) (res []*entities.User, count int64, err error)
DeleteAuth(ctx context.Context, id entity.ID) error
Log(ctx context.Context) *zap.SugaredLogger
}

type AuthRepo struct {
}

func (m *AuthRepo) DB(ctx context.Context) *gorm.DB {
func (a *AuthRepo) DB(ctx context.Context) *gorm.DB {
return global.DBFromContext(ctx).Model(&entities.Auth{})
}

func (m AuthRepo) CreateAuth(ctx context.Context, Auth *entities.Auth) error {
func (a AuthRepo) CreateAuth(ctx context.Context, auth *entities.Auth) error {
panic("todo")
}

func (m AuthRepo) UpdateAuth(ctx context.Context, updateFields []string, Auth *entities.Auth) error {
func (a AuthRepo) UpdateAuth(ctx context.Context, updateFields []string, auth *entities.Auth) error {
panic("todo")
}

func (m AuthRepo) GetAuth(ctx context.Context, id entity.ID) (*entities.Auth, error) {
func (a AuthRepo) GetAuth(ctx context.Context, id entity.ID) (*entities.Auth, error) {
panic("todo")
}

func (m AuthRepo) DeleteAuth(ctx context.Context, id entity.ID) error {
func (a AuthRepo) DeleteAuth(ctx context.Context, id entity.ID) error {
panic("todo")
}

func (m AuthRepo) GetAuths(ctx context.Context, filter *database.Filter) (res []*entities.Auth, count int64, err error) {
func (a AuthRepo) GetAuths(ctx context.Context, filter *database.Filter) (res []*entities.Auth, count int64, err error) {
panic("todo")
}

func (a AuthRepo) Log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("AuthRepo")
}
17 changes: 11 additions & 6 deletions protoc-gen-go-crud/internal/authorization/repositories/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,36 @@ type IRoleRepo interface {
GetRole(ctx context.Context, id entity.ID) (*entities.Role, error)
GetRoles(ctx context.Context, filter *database.Filter) (res []*entities.User, count int64, err error)
DeleteRole(ctx context.Context, id entity.ID) error
Log(ctx context.Context) *zap.SugaredLogger
}

type RoleRepo struct {
}

func (m *RoleRepo) DB(ctx context.Context) *gorm.DB {
func (r *RoleRepo) DB(ctx context.Context) *gorm.DB {
return global.DBFromContext(ctx).Model(&entities.Role{})
}

func (m RoleRepo) CreateRole(ctx context.Context, Role *entities.Role) error {
func (r RoleRepo) CreateRole(ctx context.Context, role *entities.Role) error {
panic("todo")
}

func (m RoleRepo) UpdateRole(ctx context.Context, updateFields []string, Role *entities.Role) error {
func (r RoleRepo) UpdateRole(ctx context.Context, updateFields []string, role *entities.Role) error {
panic("todo")
}

func (m RoleRepo) GetRole(ctx context.Context, id entity.ID) (*entities.Role, error) {
func (r RoleRepo) GetRole(ctx context.Context, id entity.ID) (*entities.Role, error) {
panic("todo")
}

func (m RoleRepo) DeleteRole(ctx context.Context, id entity.ID) error {
func (r RoleRepo) DeleteRole(ctx context.Context, id entity.ID) error {
panic("todo")
}

func (m RoleRepo) GetRoles(ctx context.Context, filter *database.Filter) (res []*entities.Role, count int64, err error) {
func (r RoleRepo) GetRoles(ctx context.Context, filter *database.Filter) (res []*entities.Role, count int64, err error) {
panic("todo")
}

func (r RoleRepo) Log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("RoleRepo")
}
15 changes: 10 additions & 5 deletions protoc-gen-go-crud/internal/authorization/usecase/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type IAuthUsecase interface {
DeleteAuth(ctx context.Context, in *authorizationV1.DeleteAuthRequest) error
GetAuth(ctx context.Context, in *authorizationV1.GetAuthRequest) (*entities.Auth, error)
ListAuth(ctx context.Context, in *authorizationV1.ListAuthRequest) ([]*entities.Auth, int64, error)
Log(ctx context.Context) *zap.SugaredLogger
}

type AuthUseCase struct {
Expand All @@ -15,22 +16,26 @@ func NewAuthUseCase() IAuthUsecase {
return &AuthUseCase{}
}

func (r *AuthUseCase) CreateAuth(ctx context.Context, in *authorizationV1.CreateAuthRequest) error {
func (a *AuthUseCase) CreateAuth(ctx context.Context, in *authorizationV1.CreateAuthRequest) error {
panic("todo")
}

func (r *AuthUseCase) UpdateAuth(ctx context.Context, in *authorizationV1.UpdateAuthRequest) error {
func (a *AuthUseCase) UpdateAuth(ctx context.Context, in *authorizationV1.UpdateAuthRequest) error {
panic("todo")
}

func (r *AuthUseCase) DeleteAuth(ctx context.Context, in *authorizationV1.DeleteAuthRequest) error {
func (a *AuthUseCase) DeleteAuth(ctx context.Context, in *authorizationV1.DeleteAuthRequest) error {
panic("todo")
}

func (r *AuthUseCase) GetAuth(ctx context.Context, in *authorizationV1.GetAuthRequest) (*entities.Auth, error) {
func (a *AuthUseCase) GetAuth(ctx context.Context, in *authorizationV1.GetAuthRequest) (*entities.Auth, error) {
panic("todo")
}

func (r *AuthUseCase) ListAuth(ctx context.Context, in *authorizationV1.ListAuthRequest) ([]*entities.Auth, int64, error) {
func (a *AuthUseCase) ListAuth(ctx context.Context, in *authorizationV1.ListAuthRequest) ([]*entities.Auth, int64, error) {
panic("todo")
}

func (a AuthUseCase) Log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("AuthRepo")
}
5 changes: 5 additions & 0 deletions protoc-gen-go-crud/internal/authorization/usecase/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type IRoleUsecase interface {
DeleteRole(ctx context.Context, in *authorizationV1.DeleteRoleRequest) error
GetRole(ctx context.Context, in *authorizationV1.GetRoleRequest) (*entities.Role, error)
ListRole(ctx context.Context, in *authorizationV1.ListRoleRequest) ([]*entities.Role, int64, error)
Log(ctx context.Context) *zap.SugaredLogger
}

type RoleUseCase struct {
Expand Down Expand Up @@ -34,3 +35,7 @@ func (r *RoleUseCase) GetRole(ctx context.Context, in *authorizationV1.GetRoleRe
func (r *RoleUseCase) ListRole(ctx context.Context, in *authorizationV1.ListRoleRequest) ([]*entities.Role, int64, error) {
panic("todo")
}

func (r RoleUseCase) Log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("RoleRepo")
}
Binary file modified protoc-gen-go-crud/protoc-gen-go-crud
Binary file not shown.
6 changes: 6 additions & 0 deletions protoc-gen-go-crud/repositories/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type I{{.ServiceType}}Repo interface {
Get{{.ServiceType}}(ctx context.Context, id entity.ID) (*entities.{{.ServiceType}}, error)
Get{{.ServiceType}}s(ctx context.Context, filter *database.Filter) (res []*entities.User, count int64, err error)
Delete{{.ServiceType}}(ctx context.Context, id entity.ID) error
Log(ctx context.Context) *zap.SugaredLogger
}
Expand Down Expand Up @@ -48,6 +49,11 @@ func ({{$firstLetter}} {{.ServiceType}}Repo) Get{{.ServiceType}}s(ctx context.Co
panic("todo")
}
func ({{$firstLetter}} {{.ServiceType}}Repo) Log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("{{.ServiceType}}Repo")
}
`

type serviceDesc struct {
Expand Down
26 changes: 20 additions & 6 deletions protoc-gen-go-crud/usecase/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (

var crudTemplate = `
{{$firstLetter := (GetFirstLetter .ServiceType)}}
type I{{.ServiceType}}Usecase interface {
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)
List{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.List{{.ServiceType}}Request) ([]*entities.{{.ServiceType}},int64,error)
Log(ctx context.Context) *zap.SugaredLogger
}
type {{.ServiceType}}UseCase struct {
Expand All @@ -23,25 +27,30 @@ func New{{.ServiceType}}UseCase() I{{.ServiceType}}Usecase {
return &{{.ServiceType}}UseCase{}
}
func (r *{{.ServiceType}}UseCase) Create{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Create{{.ServiceType}}Request) error {
func ({{$firstLetter}} *{{.ServiceType}}UseCase) Create{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Create{{.ServiceType}}Request) error {
panic("todo")
}
func (r *{{.ServiceType}}UseCase) Update{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Update{{.ServiceType}}Request) error {
func ({{$firstLetter}} *{{.ServiceType}}UseCase) Update{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Update{{.ServiceType}}Request) error {
panic("todo")
}
func (r *{{.ServiceType}}UseCase) Delete{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Delete{{.ServiceType}}Request) error{
func ({{$firstLetter}} *{{.ServiceType}}UseCase) Delete{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Delete{{.ServiceType}}Request) error{
panic("todo")
}
func (r *{{.ServiceType}}UseCase) Get{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Get{{.ServiceType}}Request) (*entities.{{.ServiceType}},error){
func ({{$firstLetter}} *{{.ServiceType}}UseCase) Get{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.Get{{.ServiceType}}Request) (*entities.{{.ServiceType}},error){
panic("todo")
}
func (r *{{.ServiceType}}UseCase) List{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.List{{.ServiceType}}Request) ([]*entities.{{.ServiceType}},int64,error){
func ({{$firstLetter}} *{{.ServiceType}}UseCase) List{{.ServiceType}}(ctx context.Context,in *{{.PackageName}}.List{{.ServiceType}}Request) ([]*entities.{{.ServiceType}},int64,error){
panic("todo")
}
func ({{$firstLetter}} {{.ServiceType}}UseCase) Log(ctx context.Context) *zap.SugaredLogger {
return global.Logger(ctx).Named("{{.ServiceType}}Repo")
}
`

type serviceDesc struct {
Expand Down Expand Up @@ -78,6 +87,7 @@ func (s *serviceDesc) execute() string {
buf := new(bytes.Buffer)
tmpl, err := template.New("crud").Funcs(template.FuncMap{
"IsHasPackagePrefix": IsHasPackagePrefix,
"GetFirstLetter": GetFirstLetter,
}).Parse(strings.TrimSpace(crudTemplate))
if err != nil {
panic(err)
Expand All @@ -91,3 +101,7 @@ func (s *serviceDesc) execute() string {
func IsHasPackagePrefix(s string) bool {
return strings.Contains(s, ".")
}

func GetFirstLetter(s string) string {
return strings.ToLower(s[0:1])
}

0 comments on commit 77f6432

Please sign in to comment.