From ce9ef06a0ea4871fc578fe4207ffe99eea62dd8a Mon Sep 17 00:00:00 2001 From: "yuhao.su" <644208937@qq.com> Date: Fri, 16 Jun 2023 17:35:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A4=A7=E5=B0=8F=E5=86=99=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protoc-gen-go-crud/repositories/template.go | 26 ++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/protoc-gen-go-crud/repositories/template.go b/protoc-gen-go-crud/repositories/template.go index 501cb0d..c3fab12 100644 --- a/protoc-gen-go-crud/repositories/template.go +++ b/protoc-gen-go-crud/repositories/template.go @@ -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") } @@ -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) @@ -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:] +}