From 58450dd63de213a549b1c137e867d5627bd6af88 Mon Sep 17 00:00:00 2001 From: Rogee Date: Sat, 21 Dec 2024 00:17:48 +0800 Subject: [PATCH] feat: update templates --- .../project/app/requests/pagination.go.tpl | 31 +++ .../requests/sort.go.tpl} | 32 +--- templates/project/main_test.go.tpl | 6 - templates/project/pkg/consts/ctx.gen.go.tpl | 179 ------------------ templates/project/pkg/consts/ctx.go.tpl | 9 - templates/project/pkg/dao.go.tpl | 13 -- templates/project/pkg/db/pagination.go.tpl | 7 - templates/project/pkg/pg/.gitkeep | 0 templates/project/pkg/{db => pg}/db.go.tpl | 12 ++ 9 files changed, 44 insertions(+), 245 deletions(-) create mode 100644 templates/project/app/requests/pagination.go.tpl rename templates/project/{pkg/data_structures.go.tpl => app/requests/sort.go.tpl} (53%) mode change 100755 => 100644 delete mode 100644 templates/project/pkg/consts/ctx.gen.go.tpl delete mode 100644 templates/project/pkg/consts/ctx.go.tpl delete mode 100755 templates/project/pkg/dao.go.tpl delete mode 100644 templates/project/pkg/db/pagination.go.tpl delete mode 100644 templates/project/pkg/pg/.gitkeep rename templates/project/pkg/{db => pg}/db.go.tpl (75%) diff --git a/templates/project/app/requests/pagination.go.tpl b/templates/project/app/requests/pagination.go.tpl new file mode 100644 index 0000000..910457c --- /dev/null +++ b/templates/project/app/requests/pagination.go.tpl @@ -0,0 +1,31 @@ +package requests + +type Pager struct { + Pagination `json:",inline"` + Total int64 `json:"total"` + Items interface{} `json:"items"` +} + +type Pagination struct { + Page int `json:"page" form:"page"` + Limit int `json:"limit" form:"limit"` +} + +func (filter *PageQueryFilter) Offset() int { + return (filter.Page - 1) * filter.Limit +} + +func (filter *PageQueryFilter) Format() *PageQueryFilter { + if filter.Page <= 0 { + filter.Page = 1 + } + + if filter.Limit <= 0 { + filter.Limit = 10 + } + + if filter.Limit > 50 { + filter.Limit = 50 + } + return filter +} diff --git a/templates/project/pkg/data_structures.go.tpl b/templates/project/app/requests/sort.go.tpl old mode 100755 new mode 100644 similarity index 53% rename from templates/project/pkg/data_structures.go.tpl rename to templates/project/app/requests/sort.go.tpl index 36623c5..517b419 --- a/templates/project/pkg/data_structures.go.tpl +++ b/templates/project/app/requests/sort.go.tpl @@ -1,4 +1,4 @@ -package pkg +package requests import ( "strings" @@ -39,33 +39,3 @@ func (s *SortQueryFilter) DescID() *SortQueryFilter { s.Desc = lo.ToPtr(strings.Join(items, ",")) return s } - -type PageDataResponse struct { - PageQueryFilter `json:",inline"` - Total int64 `json:"total"` - Items interface{} `json:"items"` -} - -type PageQueryFilter struct { - Page int `json:"page" form:"page"` - Limit int `json:"limit" form:"limit"` -} - -func (filter *PageQueryFilter) Offset() int { - return (filter.Page - 1) * filter.Limit -} - -func (filter *PageQueryFilter) Format() *PageQueryFilter { - if filter.Page <= 0 { - filter.Page = 1 - } - - if filter.Limit <= 0 { - filter.Limit = 10 - } - - if filter.Limit > 50 { - filter.Limit = 50 - } - return filter -} diff --git a/templates/project/main_test.go.tpl b/templates/project/main_test.go.tpl index 4a86085..06ab7d0 100755 --- a/templates/project/main_test.go.tpl +++ b/templates/project/main_test.go.tpl @@ -1,7 +1 @@ package main - -import ( - "testing" - - "git.ipao.vip/rogeecn/atom" -) diff --git a/templates/project/pkg/consts/ctx.gen.go.tpl b/templates/project/pkg/consts/ctx.gen.go.tpl deleted file mode 100644 index ba74cf5..0000000 --- a/templates/project/pkg/consts/ctx.gen.go.tpl +++ /dev/null @@ -1,179 +0,0 @@ -// Code generated by go-enum DO NOT EDIT. -// Version: - -// Revision: - -// Build Date: - -// Built By: - - -package consts - -import ( - "database/sql/driver" - "errors" - "fmt" - "strings" -) - -const ( - // CtxKeyTx is a CtxKey of type Tx. - CtxKeyTx CtxKey = "__ctx_db:" - // CtxKeyJwt is a CtxKey of type Jwt. - CtxKeyJwt CtxKey = "__jwt_token:" - // CtxKeyClaim is a CtxKey of type Claim. - CtxKeyClaim CtxKey = "__jwt_claim:" -) - -var ErrInvalidCtxKey = fmt.Errorf("not a valid CtxKey, try [%s]", strings.Join(_CtxKeyNames, ", ")) - -var _CtxKeyNames = []string{ - string(CtxKeyTx), - string(CtxKeyJwt), - string(CtxKeyClaim), -} - -// CtxKeyNames returns a list of possible string values of CtxKey. -func CtxKeyNames() []string { - tmp := make([]string, len(_CtxKeyNames)) - copy(tmp, _CtxKeyNames) - return tmp -} - -// CtxKeyValues returns a list of the values for CtxKey -func CtxKeyValues() []CtxKey { - return []CtxKey{ - CtxKeyTx, - CtxKeyJwt, - CtxKeyClaim, - } -} - -// String implements the Stringer interface. -func (x CtxKey) String() string { - return string(x) -} - -// IsValid provides a quick way to determine if the typed value is -// part of the allowed enumerated values -func (x CtxKey) IsValid() bool { - _, err := ParseCtxKey(string(x)) - return err == nil -} - -var _CtxKeyValue = map[string]CtxKey{ - "__ctx_db:": CtxKeyTx, - "__jwt_token:": CtxKeyJwt, - "__jwt_claim:": CtxKeyClaim, -} - -// ParseCtxKey attempts to convert a string to a CtxKey. -func ParseCtxKey(name string) (CtxKey, error) { - if x, ok := _CtxKeyValue[name]; ok { - return x, nil - } - return CtxKey(""), fmt.Errorf("%s is %w", name, ErrInvalidCtxKey) -} - -var errCtxKeyNilPtr = errors.New("value pointer is nil") // one per type for package clashes - -// Scan implements the Scanner interface. -func (x *CtxKey) Scan(value interface{}) (err error) { - if value == nil { - *x = CtxKey("") - return - } - - // A wider range of scannable types. - // driver.Value values at the top of the list for expediency - switch v := value.(type) { - case string: - *x, err = ParseCtxKey(v) - case []byte: - *x, err = ParseCtxKey(string(v)) - case CtxKey: - *x = v - case *CtxKey: - if v == nil { - return errCtxKeyNilPtr - } - *x = *v - case *string: - if v == nil { - return errCtxKeyNilPtr - } - *x, err = ParseCtxKey(*v) - default: - return errors.New("invalid type for CtxKey") - } - - return -} - -// Value implements the driver Valuer interface. -func (x CtxKey) Value() (driver.Value, error) { - return x.String(), nil -} - -// Set implements the Golang flag.Value interface func. -func (x *CtxKey) Set(val string) error { - v, err := ParseCtxKey(val) - *x = v - return err -} - -// Get implements the Golang flag.Getter interface func. -func (x *CtxKey) Get() interface{} { - return *x -} - -// Type implements the github.com/spf13/pFlag Value interface. -func (x *CtxKey) Type() string { - return "CtxKey" -} - -type NullCtxKey struct { - CtxKey CtxKey - Valid bool -} - -func NewNullCtxKey(val interface{}) (x NullCtxKey) { - err := x.Scan(val) // yes, we ignore this error, it will just be an invalid value. - _ = err // make any errcheck linters happy - return -} - -// Scan implements the Scanner interface. -func (x *NullCtxKey) Scan(value interface{}) (err error) { - if value == nil { - x.CtxKey, x.Valid = CtxKey(""), false - return - } - - err = x.CtxKey.Scan(value) - x.Valid = (err == nil) - return -} - -// Value implements the driver Valuer interface. -func (x NullCtxKey) Value() (driver.Value, error) { - if !x.Valid { - return nil, nil - } - // driver.Value accepts int64 for int values. - return string(x.CtxKey), nil -} - -type NullCtxKeyStr struct { - NullCtxKey -} - -func NewNullCtxKeyStr(val interface{}) (x NullCtxKeyStr) { - x.Scan(val) // yes, we ignore this error, it will just be an invalid value. - return -} - -// Value implements the driver Valuer interface. -func (x NullCtxKeyStr) Value() (driver.Value, error) { - if !x.Valid { - return nil, nil - } - return x.CtxKey.String(), nil -} diff --git a/templates/project/pkg/consts/ctx.go.tpl b/templates/project/pkg/consts/ctx.go.tpl deleted file mode 100644 index 5a98c69..0000000 --- a/templates/project/pkg/consts/ctx.go.tpl +++ /dev/null @@ -1,9 +0,0 @@ -package consts - -// swagger:enum CacheKey -// ENUM( -// Tx = "__ctx_db:", -// Jwt = "__jwt_token:", -// Claim = "__jwt_claim:", -// ) -type CtxKey string diff --git a/templates/project/pkg/dao.go.tpl b/templates/project/pkg/dao.go.tpl deleted file mode 100755 index df67d26..0000000 --- a/templates/project/pkg/dao.go.tpl +++ /dev/null @@ -1,13 +0,0 @@ -package pkg - -func WrapLike(v string) string { - return "%" + v + "%" -} - -func WrapLikeLeft(v string) string { - return "%" + v -} - -func WrapLikeRight(v string) string { - return "%" + v -} diff --git a/templates/project/pkg/db/pagination.go.tpl b/templates/project/pkg/db/pagination.go.tpl deleted file mode 100644 index 01f448c..0000000 --- a/templates/project/pkg/db/pagination.go.tpl +++ /dev/null @@ -1,7 +0,0 @@ -package db - -type Pagination struct { - Offset string `json:"offset,omitempty"` - OffsetID int64 `json:"-"` - Action int `json:"action"` // action: 0 :加载更多 1:刷新 -} diff --git a/templates/project/pkg/pg/.gitkeep b/templates/project/pkg/pg/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/templates/project/pkg/db/db.go.tpl b/templates/project/pkg/pg/db.go.tpl similarity index 75% rename from templates/project/pkg/db/db.go.tpl rename to templates/project/pkg/pg/db.go.tpl index b2a6d87..98c1ea3 100644 --- a/templates/project/pkg/db/db.go.tpl +++ b/templates/project/pkg/pg/db.go.tpl @@ -26,3 +26,15 @@ func TruncateAllTables(ctx context.Context, db *sql.DB, tableName ...string) err } return nil } + +func WrapLike(v string) string { + return "%" + v + "%" +} + +func WrapLikeLeft(v string) string { + return "%" + v +} + +func WrapLikeRight(v string) string { + return "%" + v +}