Skip to content

Commit

Permalink
feat:大小写优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiaosu committed Jun 16, 2023
1 parent acd6e27 commit ce9ef06
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions protoc-gen-go-crud/repositories/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,34 @@ type I{{.ServiceType}}Repo interface {
}
{{$firstLetter := (GetFirstLetter .ServiceType)}}
{{$entityName := (GetEntityName .ServiceType)}}
type {{.ServiceType}}Repo struct {
}
func (m *{{.ServiceType}}Repo) DB(ctx context.Context) *gorm.DB {
func ({{$firstLetter}} *{{.ServiceType}}Repo) DB(ctx context.Context) *gorm.DB {
return global.DBFromContext(ctx).Model(&entities.{{.ServiceType}}{})
}
func (m {{.ServiceType}}Repo) Create{{.ServiceType}}(ctx context.Context, {{.ServiceType}} *entities.{{.ServiceType}}) error {
func ({{$firstLetter}} {{.ServiceType}}Repo) Create{{.ServiceType}}(ctx context.Context, {{$entityName}} *entities.{{.ServiceType}}) error {
panic("todo")
}
func (m {{.ServiceType}}Repo) Update{{.ServiceType}}(ctx context.Context, updateFields []string, {{.ServiceType}} *entities.{{.ServiceType}}) error {
func ({{$firstLetter}} {{.ServiceType}}Repo) Update{{.ServiceType}}(ctx context.Context, updateFields []string, {{$entityName}} *entities.{{.ServiceType}}) error {
panic("todo")
}
func (m {{.ServiceType}}Repo) Get{{.ServiceType}}(ctx context.Context, id entity.ID) (*entities.{{.ServiceType}}, error) {
func ({{$firstLetter}} {{.ServiceType}}Repo) Get{{.ServiceType}}(ctx context.Context, id entity.ID) (*entities.{{.ServiceType}}, error) {
panic("todo")
}
func (m {{.ServiceType}}Repo) Delete{{.ServiceType}}(ctx context.Context, id entity.ID) error {
func ({{$firstLetter}} {{.ServiceType}}Repo) Delete{{.ServiceType}}(ctx context.Context, id entity.ID) error {
panic("todo")
}
func (m {{.ServiceType}}Repo) Get{{.ServiceType}}s(ctx context.Context, filter *database.Filter) (res []*entities.{{.ServiceType}}, count int64, err error) {
func ({{$firstLetter}} {{.ServiceType}}Repo) Get{{.ServiceType}}s(ctx context.Context, filter *database.Filter) (res []*entities.{{.ServiceType}}, count int64, err error) {
panic("todo")
}
Expand Down Expand Up @@ -80,6 +84,8 @@ func (s *serviceDesc) execute() string {
buf := new(bytes.Buffer)
tmpl, err := template.New("crud").Funcs(template.FuncMap{
"IsHasPackagePrefix": IsHasPackagePrefix,
"GetFirstLetter": GetFirstLetter,
"GetEntityName": GetEntityName,
}).Parse(strings.TrimSpace(crudTemplate))
if err != nil {
panic(err)
Expand All @@ -93,3 +99,11 @@ 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])
}

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

0 comments on commit ce9ef06

Please sign in to comment.