template

package
v2.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2024 License: MIT, MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TableQueryStruct table query struct
	TableQueryStruct = createMethod + `
	type {{.QueryStructName}} struct {
		{{.QueryStructName}}Do
		` + fields + `
	}
	` + tableMethod + asMethond + updateFieldMethod + getFieldMethod + getFieldExprByName + 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 + getFieldExprByName + 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 model.{{.ModelStructName}}) (model.{{.ModelStructName}}, error)

// GetGen{{.ModelStructName}} {{.StructComment}}
` + NotEditMarkForGDDShort + `
GetGen{{.ModelStructName}}(ctx context.Context, body model.{{.ModelStructName}}) (model.{{.ModelStructName}}, error)

// PutGen{{.ModelStructName}} {{.StructComment}}
` + NotEditMarkForGDDShort + `
PutGen{{.ModelStructName}}(ctx context.Context, body model.{{.ModelStructName}}) error

// DeleteGen{{.ModelStructName}} {{.StructComment}}
` + NotEditMarkForGDDShort + `
DeleteGen{{.ModelStructName}}(ctx context.Context, body model.{{.ModelStructName}}) 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}}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}}
	copier.DeepCopy(body, &m)
	u := receiver.q.{{.ModelStructName}}
	if err = u.WithContext(ctx).Create(&m); err != nil {
		return nil, errors.WithStack(err)
	}
	data = new(pb.{{.ModelStructName}})
	copier.DeepCopy(m, data)
	return
}

// 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.{{.PbPrimaryProp}})).First()
	if err != nil {
		return nil, errors.WithStack(err)
	}
	data = new(pb.{{.ModelStructName}})
	copier.DeepCopy(m, data)
	return
}

// 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.{{.PbPrimaryProp}})).Updates(m)
	return &emptypb.Empty{}, errors.WithStack(err)
}

// 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.{{.PbPrimaryProp}})).Delete()
	return &emptypb.Empty{}, errors.WithStack(err)
}

// 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
	copier.DeepCopy(request, &body)
	resCxt := receiver.pg.With(receiver.q.Db().Model(&model.{{.ModelStructName}}{})).Request(body)
	paginated := resCxt.Response(&[]model.{{.ModelStructName}}{})
	if resCxt.Error() != nil {
		return nil, errors.WithStack(resCxt.Error())
	}
	data = new(pb.Page)
	copier.DeepCopy(paginated, data)
	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}}
	copier.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}}
		copier.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}}
	copier.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}}
	copier.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
	copier.DeepCopy(request, &parameter)
	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}}
		copier.DeepCopy(d, &msg)
		a, err := anypb.New(&msg)
		if err != nil {
			return nil, errors.WithStack(err)
		}
		items = append(items, a)
	}
	var ret pb.Page
	copier.DeepCopy(data, &ret)
	ret.Items = items
	return &ret, nil
}

`
View Source
const CRUDMethod = `` /* 8191-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"

	"github.com/wubin1989/datatypes"
	"github.com/wubin1989/gorm"
	"github.com/wubin1989/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 = `` /* 184-byte string literal not displayed */
View Source
const EditMarkForGDD = `` /* 226-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 (
	"{{.ConfigPackage}}"
	"fmt"
	"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"

	"encoding/json"
	"time"

	"github.com/wubin1989/datatypes"
	"github.com/wubin1989/gorm"
	"github.com/wubin1989/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}}
}

// TableName {{.ModelStructName}}'s table name
func (*{{.ModelStructName}}) TableName() string {
	{{if .TableName -}}var TableName{{.ModelStructName}} string{{- end}}
	
	if stringutils.IsNotEmpty(config.G_Config.Db.Name) {
		TableName{{.ModelStructName}} = fmt.Sprintf("%s.{{.TableName}}", config.G_Config.Db.Name)
	} else {
		TableName{{.ModelStructName}} = "{{.TableName}}"
	}

	return TableName{{.ModelStructName}}
}
`

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 = `` /* 181-byte string literal not displayed */
View Source
const NotEditMarkForGDDShort = `// Code generated by github.com/wubin1989/gen for go-doudou. DO NOT EDIT.`
View Source
const QueryMethod = `` /* 1949-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 SvcImpl = EditMarkForGDD + `
package service

import ()

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 = `` /* 394-byte string literal not displayed */
View Source
const SvcImplImportGrpc = `` /* 268-byte string literal not displayed */

Variables

This section is empty.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL