diff --git a/protoc-gen-go-crud/deliveries/template.go b/protoc-gen-go-crud/deliveries/template.go index 07a7c5e..bb6ee44 100644 --- a/protoc-gen-go-crud/deliveries/template.go +++ b/protoc-gen-go-crud/deliveries/template.go @@ -10,6 +10,7 @@ var crudTemplate = ` type {{.ServiceType}}Service struct { } +{{$firstLetter := (GetFirstLetter .ServiceType)}} {{$svrType := .ServiceType}} {{$svrName := .ServiceName}} {{- range .MethodSets}} @@ -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 { @@ -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) @@ -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]) +} diff --git a/protoc-gen-go-crud/internal/authorization/deliveries/authorization.go b/protoc-gen-go-crud/internal/authorization/deliveries/authorization.go index 5390b33..de6eae8 100644 --- a/protoc-gen-go-crud/internal/authorization/deliveries/authorization.go +++ b/protoc-gen-go-crud/internal/authorization/deliveries/authorization.go @@ -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") +} diff --git a/protoc-gen-go-crud/internal/authorization/deliveries/role.go b/protoc-gen-go-crud/internal/authorization/deliveries/role.go index 25b013a..12cc44d 100644 --- a/protoc-gen-go-crud/internal/authorization/deliveries/role.go +++ b/protoc-gen-go-crud/internal/authorization/deliveries/role.go @@ -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") +} diff --git a/protoc-gen-go-crud/internal/authorization/repositories/authorization.go b/protoc-gen-go-crud/internal/authorization/repositories/authorization.go index 84161e3..ef7f50d 100644 --- a/protoc-gen-go-crud/internal/authorization/repositories/authorization.go +++ b/protoc-gen-go-crud/internal/authorization/repositories/authorization.go @@ -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") +} diff --git a/protoc-gen-go-crud/internal/authorization/repositories/role.go b/protoc-gen-go-crud/internal/authorization/repositories/role.go index a1fcbdf..523e446 100644 --- a/protoc-gen-go-crud/internal/authorization/repositories/role.go +++ b/protoc-gen-go-crud/internal/authorization/repositories/role.go @@ -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") +} diff --git a/protoc-gen-go-crud/internal/authorization/usecase/authorization.go b/protoc-gen-go-crud/internal/authorization/usecase/authorization.go index cc188cf..d2be418 100644 --- a/protoc-gen-go-crud/internal/authorization/usecase/authorization.go +++ b/protoc-gen-go-crud/internal/authorization/usecase/authorization.go @@ -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 { @@ -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") +} diff --git a/protoc-gen-go-crud/internal/authorization/usecase/role.go b/protoc-gen-go-crud/internal/authorization/usecase/role.go index 95b0fc6..3773791 100644 --- a/protoc-gen-go-crud/internal/authorization/usecase/role.go +++ b/protoc-gen-go-crud/internal/authorization/usecase/role.go @@ -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 { @@ -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") +} diff --git a/protoc-gen-go-crud/protoc-gen-go-crud b/protoc-gen-go-crud/protoc-gen-go-crud index 1cbf10b..5632d1d 100755 Binary files a/protoc-gen-go-crud/protoc-gen-go-crud and b/protoc-gen-go-crud/protoc-gen-go-crud differ diff --git a/protoc-gen-go-crud/repositories/template.go b/protoc-gen-go-crud/repositories/template.go index c3fab12..384715c 100644 --- a/protoc-gen-go-crud/repositories/template.go +++ b/protoc-gen-go-crud/repositories/template.go @@ -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 } @@ -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 { diff --git a/protoc-gen-go-crud/usecase/template.go b/protoc-gen-go-crud/usecase/template.go index 618e9fb..51f2e13 100644 --- a/protoc-gen-go-crud/usecase/template.go +++ b/protoc-gen-go-crud/usecase/template.go @@ -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 { @@ -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 { @@ -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) @@ -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]) +}