Documentation ¶
Index ¶
Constants ¶
View Source
const ( // TableQueryStruct table query struct TableQueryStruct = createMethod + ` type {{.QueryStructName}} struct { {{.QueryStructName}}Do ` + fields + ` } ` + tableMethod + asMethond + updateFieldMethod + getFieldMethod + fillFieldMapMethod + cloneMethod + replaceMethod + relationship + defineMethodStruct // TableQueryStructWithContext table query struct with context TableQueryStructWithContext = createMethod + ` type {{.QueryStructName}} struct { {{.QueryStructName}}Do {{.QueryStructName}}Do ` + fields + ` } ` + tableMethod + asMethond + updateFieldMethod + ` func ({{.S}} *{{.QueryStructName}}) WithContext(ctx context.Context) {{.ReturnObject}} { return {{.S}}.{{.QueryStructName}}Do.WithContext(ctx)} func ({{.S}} {{.QueryStructName}}) TableName() string { return {{.S}}.{{.QueryStructName}}Do.TableName() } func ({{.S}} {{.QueryStructName}}) Alias() string { return {{.S}}.{{.QueryStructName}}Do.Alias() } func ({{.S}} {{.QueryStructName}}) Columns(cols ...field.Expr) gormgen.Columns { return {{.S}}.{{.QueryStructName}}Do.Columns(cols...) } ` + getFieldMethod + fillFieldMapMethod + cloneMethod + replaceMethod + relationship + defineMethodStruct // TableQueryIface table query interface TableQueryIface = defineDoInterface )
View Source
const AppendSvc = ` // PostGen{{.ModelStructName}} {{.StructComment}} ` + NotEditMarkForGDDShort + ` PostGen{{.ModelStructName}}(ctx context.Context, body dto.{{.ModelStructName}}) (data {{.PriKeyType}}, err error) // PostGen{{.ModelStructName}}s {{.StructComment}} ` + NotEditMarkForGDDShort + ` PostGen{{.ModelStructName}}s(ctx context.Context, body []dto.{{.ModelStructName}}) (data []{{.PriKeyType}}, err error) // GetGen{{.ModelStructName}}_Id {{.StructComment}} ` + NotEditMarkForGDDShort + ` GetGen{{.ModelStructName}}_Id(ctx context.Context, id {{.PriKeyType}}) (data dto.{{.ModelStructName}}, err error) // PutGen{{.ModelStructName}} {{.StructComment}} ` + NotEditMarkForGDDShort + ` PutGen{{.ModelStructName}}(ctx context.Context, body dto.{{.ModelStructName}}) error // DeleteGen{{.ModelStructName}}_Id {{.StructComment}} ` + NotEditMarkForGDDShort + ` DeleteGen{{.ModelStructName}}_Id(ctx context.Context, id {{.PriKeyType}}) error // GetGen{{.ModelStructName}}s {{.StructComment}} ` + NotEditMarkForGDDShort + ` GetGen{{.ModelStructName}}s(ctx context.Context, parameter dto.Parameter) (data dto.Page, err error) `
View Source
const AppendSvcImpl = ` // PostGen{{.ModelStructName}} {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) PostGen{{.ModelStructName}}(ctx context.Context, body dto.{{.ModelStructName}}) (data {{.PriKeyType}}, err error) { m := model.{{.ModelStructName}}(body) u := receiver.q.{{.ModelStructName}} err = errors.WithStack(u.WithContext(ctx).Create(&m)) data = m.ID return } // PostGen{{.ModelStructName}}s {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) PostGen{{.ModelStructName}}s(ctx context.Context, body []dto.{{.ModelStructName}}) (data []{{.PriKeyType}}, err error) { list := make([]*model.{{.ModelStructName}}, 0, len(body)) for _, item := range body { m := model.{{.ModelStructName}}(item) list = append(list, &m) } u := receiver.q.{{.ModelStructName}} if err = errors.WithStack(u.WithContext(ctx).Create(list...)); err != nil { return } data = make([]{{.PriKeyType}}, 0, len(list)) for _, item := range list { data = append(data, item.ID) } return } // GetGen{{.ModelStructName}}_Id {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) GetGen{{.ModelStructName}}_Id(ctx context.Context, id {{.PriKeyType}}) (data dto.{{.ModelStructName}}, err error) { u := receiver.q.{{.ModelStructName}} m, err := u.WithContext(ctx).Where(u.ID.Eq(id)).First() if err != nil { return dto.{{.ModelStructName}}{}, errors.WithStack(err) } return dto.{{.ModelStructName}}(*m), nil } // PutGen{{.ModelStructName}} {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) PutGen{{.ModelStructName}}(ctx context.Context, body dto.{{.ModelStructName}}) (err error) { m := model.{{.ModelStructName}}(body) u := receiver.q.{{.ModelStructName}} _, err = u.WithContext(ctx).Where(u.ID.Eq(body.ID)).Updates(m) return errors.WithStack(err) } // DeleteGen{{.ModelStructName}}_Id {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) DeleteGen{{.ModelStructName}}_Id(ctx context.Context, id {{.PriKeyType}}) (err error) { u := receiver.q.{{.ModelStructName}} _, err = u.WithContext(ctx).Where(u.ID.Eq(id)).Delete() return errors.WithStack(err) } // GetGen{{.ModelStructName}}s {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) GetGen{{.ModelStructName}}s(ctx context.Context, parameter dto.Parameter) (data dto.Page, err error) { paginated := receiver.pg.With(database.Db.Model(&model.{{.ModelStructName}}{})).Request(parameter).Response(&[]model.{{.ModelStructName}}{}) data = dto.Page(paginated) return } `
View Source
const AppendSvcImplGrpc = ` // PostGen{{.ModelStructName}}Rpc {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) PostGen{{.ModelStructName}}Rpc(ctx context.Context, request *pb.{{.ModelStructName}}) (*pb.PostGen{{.ModelStructName}}RpcResponse, error) { var body dto.{{.ModelStructName}} jsoncopier.DeepCopy(request, &body) data, err := receiver.PostGen{{.ModelStructName}}(ctx, body) return &pb.PostGen{{.ModelStructName}}RpcResponse{ Data: data, }, errors.WithStack(err) } // PostGen{{.ModelStructName}}sRpc {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) PostGen{{.ModelStructName}}sRpc(ctx context.Context, request *pb.PostGen{{.ModelStructName}}sRpcRequest) (*pb.PostGen{{.ModelStructName}}sRpcResponse, error) { list := make([]dto.{{.ModelStructName}}, 0, len(request.Body)) for _, item := range request.Body { var d dto.{{.ModelStructName}} jsoncopier.DeepCopy(item, &d) list = append(list, d) } data, err := receiver.PostGen{{.ModelStructName}}s(ctx, list) return &pb.PostGen{{.ModelStructName}}sRpcResponse{ Data: data, }, errors.WithStack(err) } // GetGen{{.ModelStructName}}IdRpc {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) GetGen{{.ModelStructName}}IdRpc(ctx context.Context, request *pb.GetGen{{.ModelStructName}}IdRpcRequest) (*pb.{{.ModelStructName}}, error) { data, err := receiver.GetGen{{.ModelStructName}}_Id(ctx, request.Id) if err != nil { return nil, errors.WithStack(err) } var ret pb.{{.ModelStructName}} jsoncopier.DeepCopy(data, &ret) return &ret, nil } // PutGen{{.ModelStructName}}Rpc {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) PutGen{{.ModelStructName}}Rpc(ctx context.Context, request *pb.{{.ModelStructName}}) (*emptypb.Empty, error) { var body dto.{{.ModelStructName}} jsoncopier.DeepCopy(request, &body) return &emptypb.Empty{}, errors.WithStack(receiver.PutGen{{.ModelStructName}}(ctx, body)) } // DeleteGen{{.ModelStructName}}IdRpc {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) DeleteGen{{.ModelStructName}}IdRpc(ctx context.Context, request *pb.DeleteGen{{.ModelStructName}}IdRpcRequest) (*emptypb.Empty, error) { return &emptypb.Empty{}, errors.WithStack(receiver.DeleteGen{{.ModelStructName}}_Id(ctx, request.Id)) } // GetGen{{.ModelStructName}}sRpc {{.StructComment}} ` + NotEditMarkForGDDShort + ` func (receiver *{{.InterfaceName}}Impl) GetGen{{.ModelStructName}}sRpc(ctx context.Context, request *pb.Parameter) (*pb.Page, error) { filters := make([]interface{}, 0, len(request.Filters)) for _, item := range request.Filters { str := wrappers.StringValue{} if err := anypb.UnmarshalTo(item, &str, proto.UnmarshalOptions{}); err != nil { return nil, errors.WithStack(err) } filters = append(filters, str.Value) } var parameter dto.Parameter jsoncopier.DeepCopy(request, ¶meter) parameter.Filters = filters data, err := receiver.GetGen{{.ModelStructName}}s(ctx, parameter) if err != nil { return nil, errors.WithStack(err) } items := make([]*anypb.Any, 0, len(data.Items)) for _, item := range data.Items { d := dto.{{.ModelStructName}}(item.(model.{{.ModelStructName}})) var msg pb.{{.ModelStructName}} jsoncopier.DeepCopy(d, &msg) a, err := anypb.New(&msg) if err != nil { return nil, errors.WithStack(err) } items = append(items, a) } var ret pb.Page jsoncopier.DeepCopy(data, &ret) ret.Items = items return &ret, nil } `
View Source
const CRUDMethod = `` /* 8079-byte string literal not displayed */
CRUDMethod CRUD method
View Source
const CRUDMethodTest = `` /* 3990-byte string literal not displayed */
CRUDMethodTest CRUD method test
View Source
const DIYMethod = `` /* 1410-byte string literal not displayed */
DIYMethod DIY method
View Source
const DIYMethodTest = `` /* 496-byte string literal not displayed */
DIYMethodTest DIY method test
View Source
const DIYMethodTestBasic = `` /* 138-byte string literal not displayed */
DIYMethodTestBasic DIY method test basic
View Source
const DefaultQuery = `` /* 290-byte string literal not displayed */
DefaultQuery default query
View Source
const Dto = EditMark + `
package dto
import (
"encoding/json"
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/schema"
{{range .ImportPkgPaths}}{{.}} ` + "\n" + `{{end}}
)
// {{.ModelStructName}} {{.StructComment}}
type {{.ModelStructName}} struct {
{{range .Fields}}
{{if .MultilineComment -}}
/*
{{.ColumnComment}}
*/
{{end -}}
{{.Name}} {{.Type | convert}} ` + "`{{.Tags}}` " +
"{{if not .MultilineComment}}{{if .ColumnComment}}// {{.ColumnComment}}{{end}}{{end}}" +
`{{end}}
}
`
Dto used as a variable because it cannot load template file after packed, params still can pass file
View Source
const EditMark = `` /* 145-byte string literal not displayed */
View Source
const EditMarkForGDD = `` /* 187-byte string literal not displayed */
View Source
const Header = NotEditMark + `
package {{.Package}}
import(
{{range .ImportPkgPaths}}{{.}}` + "\n" + `{{end}}
)
`
View Source
const Model = NotEditMark + `
package {{.StructInfo.Package}}
import (
"encoding/json"
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/schema"
{{range .ImportPkgPaths}}{{.}} ` + "\n" + `{{end}}
)
{{if .TableName -}}const TableName{{.ModelStructName}} = "{{.TableName}}"{{- end}}
// {{.ModelStructName}} {{.StructComment}}
type {{.ModelStructName}} struct {
{{range .Fields}}
{{if .MultilineComment -}}
/*
{{.ColumnComment}}
*/
{{end -}}
{{.Name}} {{.Type | convert}} ` + "`{{.Tags}}` " +
"{{if not .MultilineComment}}{{if .ColumnComment}}// {{.ColumnComment}}{{end}}{{end}}" +
`{{end}}
}
`
Model used as a variable because it cannot load template file after packed, params still can pass file
View Source
const ModelMethod = `` /* 145-byte string literal not displayed */
ModelMethod model struct DIY method
View Source
const NotEditMark = `` /* 142-byte string literal not displayed */
View Source
const NotEditMarkForGDDShort = `// Code generated by gorm.io/gen for go-doudou. DO NOT EDIT.`
View Source
const QueryMethod = `` /* 1901-byte string literal not displayed */
QueryMethod query method template
View Source
const QueryMethodTest = `
const dbName = "gen_test.db"
var db *gorm.DB
var once sync.Once
func init() {
InitializeDB()
db.AutoMigrate(&_another{})
}
func InitializeDB() {
once.Do(func() {
var err error
db, err = gorm.Open(sqlite.Open(dbName), &gorm.Config{})
if err != nil {
panic(fmt.Errorf("open sqlite %q fail: %w", dbName, err))
}
})
}
func assert(t *testing.T, methodName string, res, exp interface{}) {
if !reflect.DeepEqual(res, exp) {
t.Errorf("%v() gotResult = %v, want %v", methodName, res, exp)
}
}
type _another struct {
ID uint64 ` + "`" + `gorm:"primaryKey"` + "`" + `
}
func (*_another) TableName() string { return "another_for_unit_test" }
func Test_Available(t *testing.T) {
if !Use(db).Available() {
t.Errorf("query.Available() == false")
}
}
func Test_WithContext(t *testing.T) {
query := Use(db)
if !query.Available() {
t.Errorf("query Use(db) fail: query.Available() == false")
}
type Content string
var key, value Content = "gen_tag", "unit_test"
qCtx := query.WithContext(context.WithValue(context.Background(), key, value))
for _, ctx := range []context.Context{
{{range $name,$d :=.Data -}}
qCtx.{{$d.ModelStructName}}.UnderlyingDB().Statement.Context,
{{end}}
} {
if v := ctx.Value(key); v != value {
t.Errorf("get value from context fail, expect %q, got %q", value, v)
}
}
}
func Test_Transaction(t *testing.T) {
query := Use(db)
if !query.Available() {
t.Errorf("query Use(db) fail: query.Available() == false")
}
err := query.Transaction(func(tx *Query) error { return nil })
if err != nil {
t.Errorf("query.Transaction execute fail: %s", err)
}
tx := query.Begin()
err = tx.SavePoint("point")
if err != nil {
t.Errorf("query tx SavePoint fail: %s", err)
}
err = tx.RollbackTo("point")
if err != nil {
t.Errorf("query tx RollbackTo fail: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("query tx Commit fail: %s", err)
}
err = query.Begin().Rollback()
if err != nil {
t.Errorf("query tx Rollback fail: %s", err)
}
}
`
QueryMethodTest query method test template
View Source
const Svc = EditMarkForGDD + `
package service
import (
"context"
"{{.DtoPackage}}"
v3 "github.com/unionj-cloud/go-doudou/v2/toolkit/openapi/v3"
)
//go:generate go-doudou svc http -c
//go:generate go-doudou svc grpc
type {{.InterfaceName}} interface {
}
`
View Source
const SvcImpl = EditMarkForGDD + `
package service
import ()
func init() {
query.SetDefault(database.Db)
}
var _ {{.InterfaceName}} = (*{{.InterfaceName}}Impl)(nil)
type {{.InterfaceName}}Impl struct {
conf *config.Config
pg *paginate.Pagination
q *query.Query
}
func New{{.InterfaceName}}(conf *config.Config) *{{.InterfaceName}}Impl {
pg := paginate.New(&paginate.Config{
FieldSelectorEnabled: true,
})
return &{{.InterfaceName}}Impl{
conf: conf,
pg: pg,
q: query.Q,
}
}
func (receiver {{.InterfaceName}}Impl) clone(q *query.Query) *{{.InterfaceName}}Impl {
receiver.q = q
return &receiver
}
`
View Source
const SvcImplImport = `` /* 282-byte string literal not displayed */
View Source
const SvcImplImportGrpc = `` /* 279-byte string literal not displayed */
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.