Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin1989 committed Oct 7, 2024
1 parent 77d8b82 commit c9626e4
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
1 change: 0 additions & 1 deletion cmd/internal/svc/codegen/database/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func (b *AbstractBaseGenerator) GenService() {
panic(err)
}
os.Chdir(wd)
//b.svcImplGrpc(grpcSvc)
codegen.GenHttpHandler(b.Dir, ic, 0)
codegen.GenHttp2Grpc(b.Dir, ic, codegen.GenHttp2GrpcConfig{
AllowGetWithReqBody: b.AllowGetWithReqBody,
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/templates/mainpkgmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ require (
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
github.com/wubin1989/gorm v0.0.3 // indirect
github.com/wubin1989/gorm v0.0.4 // indirect
)`
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ require (
github.com/wubin1989/clickhouse v0.0.3-0.20240611142627-b655b8690cda
github.com/wubin1989/datatypes v0.0.2-0.20240611143143-56e1066d0d46
github.com/wubin1989/dbresolver v0.0.2-0.20240611143237-5d31d83b04ba
github.com/wubin1989/gorm v0.0.3
github.com/wubin1989/gorm v0.0.4
github.com/wubin1989/hints v0.0.2-0.20240611143337-989d3cd7fd24
github.com/wubin1989/mysql v0.0.2-0.20240611142742-11722462c1f3
github.com/wubin1989/prometheus v0.0.2-0.20240611143457-960b4a499cef
Expand Down Expand Up @@ -166,7 +166,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/protobuf v1.5.4
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
Expand Down Expand Up @@ -299,5 +299,3 @@ require (
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0
)

replace github.com/wubin1989/gorm v0.0.3 => E:\workspace\gorm
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,9 @@ github.com/wubin1989/datatypes v0.0.2-0.20240611143143-56e1066d0d46 h1:2PtAVdFag
github.com/wubin1989/datatypes v0.0.2-0.20240611143143-56e1066d0d46/go.mod h1:zf0H8F3yk6l0WxQTtuf6c5cQ1KyMniqPGVi+wsP+Pwo=
github.com/wubin1989/dbresolver v0.0.2-0.20240611143237-5d31d83b04ba h1:t8iGZmi0DDV3fFXQLTbvJo3oi6FIrcW+QoIq1JyaR40=
github.com/wubin1989/dbresolver v0.0.2-0.20240611143237-5d31d83b04ba/go.mod h1:fdp9eyI05mcyy42qGZzxmxX9JMCMjeId9pzIb7ETN2M=
github.com/wubin1989/gorm v0.0.3/go.mod h1:sv3afa8/Q6XTgEbTTWTjJd0NIlBMH4bMq5VpEnXr/Cc=
github.com/wubin1989/gorm v0.0.4 h1:ItRawZeMeGsGFw9H2biEV2o8Bg3qv+07MWwZrnZfLOE=
github.com/wubin1989/gorm v0.0.4/go.mod h1:sv3afa8/Q6XTgEbTTWTjJd0NIlBMH4bMq5VpEnXr/Cc=
github.com/wubin1989/hints v0.0.2-0.20240611143337-989d3cd7fd24 h1:ZCC6dSYWh4251NNDrQ8LnR8vGVTqJmUJQ6OvGyjj86c=
github.com/wubin1989/hints v0.0.2-0.20240611143337-989d3cd7fd24/go.mod h1:D2RyfIt/EQDPauzJoc21JHaEPRDtkJXjpCyJii6Qowo=
github.com/wubin1989/mysql v0.0.2-0.20240611142742-11722462c1f3 h1:23Whb/nEWyGXOrXs41cvzqZOKrtxE6iq4+9K5yhoRC0=
Expand Down
4 changes: 4 additions & 0 deletions toolkit/gormgen/internal/generate/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generate
import (
"context"
"fmt"
"github.com/golang/protobuf/protoc-gen-go/generator"
"reflect"
"strings"

Expand Down Expand Up @@ -38,9 +39,11 @@ func GetQueryStructMeta(db *gorm.DB, conf *model.Config) (*QueryStructMeta, erro

fields := getFields(db, conf, columns)
priKeyType := "string"
pbPrimaryProp := "Id"
for _, field := range fields {
if field.PriKey {
priKeyType = field.Type
pbPrimaryProp = generator.CamelCase(field.ColumnName)
break
}
}
Expand All @@ -58,6 +61,7 @@ func GetQueryStructMeta(db *gorm.DB, conf *model.Config) (*QueryStructMeta, erro
ImportPkgPaths: conf.ImportPkgPaths,
Fields: fields,
PriKeyType: priKeyType,
PbPrimaryProp: pbPrimaryProp,
}).addMethodFromAddMethodOpt(conf.GetModelMethods()...), nil
}

Expand Down
7 changes: 3 additions & 4 deletions toolkit/gormgen/internal/generate/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ type QueryStructMeta struct {
Source model.SourceCode
ImportPkgPaths []string
ModelMethods []*parser.Method // user custom method bind to db base struct

interfaceMode bool

PriKeyType string
interfaceMode bool
PriKeyType string
PbPrimaryProp string
}

// parseStruct get all elements of struct with gorm's Parse, ignore unexported elements
Expand Down
16 changes: 8 additions & 8 deletions toolkit/gormgen/internal/template/appendsvcimpl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package template

const AppendSvcImpl = `
// PostGen{{.ModelStructName}} {{.StructComment}}
// PostGen{{.ModelStructName}}Rpc {{.StructComment}}
` + NotEditMarkForGDDShort + `
func (receiver *{{.InterfaceName}}Impl) PostGen{{.ModelStructName}}Rpc(ctx context.Context, body *pb.{{.ModelStructName}}) (data *pb.{{.ModelStructName}}, err error) {
var m model.{{.ModelStructName}}
Expand All @@ -15,11 +15,11 @@ func (receiver *{{.InterfaceName}}Impl) PostGen{{.ModelStructName}}Rpc(ctx conte
return
}
// GetGen{{.ModelStructName}} {{.StructComment}}
// GetGen{{.ModelStructName}}Rpc {{.StructComment}}
` + NotEditMarkForGDDShort + `
func (receiver *{{.InterfaceName}}Impl) GetGen{{.ModelStructName}}Rpc(ctx context.Context, body *pb.{{.ModelStructName}}) (data *pb.{{.ModelStructName}}, err error) {
u := receiver.q.{{.ModelStructName}}
m, err := u.WithContext(ctx).Where(u.ID.Eq(body.ID)).First()
m, err := u.WithContext(ctx).Where(u.ID.Eq(*body.{{.PbPrimaryProp}})).First()
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -28,25 +28,25 @@ func (receiver *{{.InterfaceName}}Impl) GetGen{{.ModelStructName}}Rpc(ctx contex
return
}
// PutGen{{.ModelStructName}} {{.StructComment}}
// PutGen{{.ModelStructName}}Rpc {{.StructComment}}
` + NotEditMarkForGDDShort + `
func (receiver *{{.InterfaceName}}Impl) PutGen{{.ModelStructName}}Rpc(ctx context.Context, body *pb.{{.ModelStructName}}) (*emptypb.Empty, error) {
var m model.{{.ModelStructName}}
copier.DeepCopy(body, &m)
u := receiver.q.{{.ModelStructName}}
_, err := u.WithContext(ctx).Where(u.ID.Eq(body.ID)).Updates(m)
_, err := u.WithContext(ctx).Where(u.ID.Eq(*body.{{.PbPrimaryProp}})).Updates(m)
return &emptypb.Empty{}, errors.WithStack(err)
}
// DeleteGen{{.ModelStructName}} {{.StructComment}}
// DeleteGen{{.ModelStructName}}Rpc {{.StructComment}}
` + NotEditMarkForGDDShort + `
func (receiver *{{.InterfaceName}}Impl) DeleteGen{{.ModelStructName}}Rpc(ctx context.Context, body *pb.{{.ModelStructName}}) (*emptypb.Empty, error) {
u := receiver.q.{{.ModelStructName}}
_, err := u.WithContext(ctx).Where(u.ID.Eq(body.ID)).Delete()
_, err := u.WithContext(ctx).Where(u.ID.Eq(*body.{{.PbPrimaryProp}})).Delete()
return &emptypb.Empty{}, errors.WithStack(err)
}
// GetGen{{.ModelStructName}}s {{.StructComment}}
// GetGen{{.ModelStructName}}sRpc {{.StructComment}}
` + NotEditMarkForGDDShort + `
func (receiver *{{.InterfaceName}}Impl) GetGen{{.ModelStructName}}sRpc(ctx context.Context, request *pb.Parameter) (data *pb.Page, err error) {
var body dto.Parameter
Expand Down

0 comments on commit c9626e4

Please sign in to comment.