interface -> any

This commit is contained in:
xing 2022-09-20 22:37:31 +08:00
parent a656ea3fd5
commit 721e7e46a3
4 changed files with 24 additions and 24 deletions

View File

@ -92,7 +92,7 @@ func getMonthPost(args ...any) ([]models.WpPosts, error) {
} }
return models.Find[models.WpPosts](where, "ID", "", return models.Find[models.WpPosts](where, "ID", "",
models.SqlBuilder{{"post_date", "asc"}}, models.SqlBuilder{{"post_date", "asc"}},
nil, 0, []interface{}{"post"}, []interface{}{"publish"}, nil, 0, []any{"post"}, []any{"publish"},
) )
} }
@ -121,12 +121,12 @@ func getPostContext(t time.Time) (prev, next models.WpPosts, err error) {
{"post_date", ">", t.Format("2006-01-02 15:04:05")}, {"post_date", ">", t.Format("2006-01-02 15:04:05")},
{"post_status", "in", ""}, {"post_status", "in", ""},
{"post_type", "post"}, {"post_type", "post"},
}, "ID,post_title,post_password", nil, []interface{}{"publish", "private"}) }, "ID,post_title,post_password", nil, []any{"publish", "private"})
prev, err = models.FirstOne[models.WpPosts](models.SqlBuilder{ prev, err = models.FirstOne[models.WpPosts](models.SqlBuilder{
{"post_date", "<", t.Format("2006-01-02 15:04:05")}, {"post_date", "<", t.Format("2006-01-02 15:04:05")},
{"post_status", "in", ""}, {"post_status", "in", ""},
{"post_type", "post"}, {"post_type", "post"},
}, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}, []interface{}{"publish", "private"}) }, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}, []any{"publish", "private"})
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
err = nil err = nil
} }
@ -143,7 +143,7 @@ func GetPostFromCache(Id uint64) (r models.WpPosts) {
func QueryAndSetPostCache(postIds []models.WpPosts) (err error) { func QueryAndSetPostCache(postIds []models.WpPosts) (err error) {
var all []uint64 var all []uint64
var needQuery []interface{} var needQuery []any
for _, wpPosts := range postIds { for _, wpPosts := range postIds {
all = append(all, wpPosts.Id) all = append(all, wpPosts.Id)
if _, ok := PostsCache.Load(wpPosts.Id); !ok { if _, ok := PostsCache.Load(wpPosts.Id); !ok {
@ -214,7 +214,7 @@ func Categories(ctx context.Context) []models.WpTermsMy {
} }
func categories(...any) (terms []models.WpTermsMy, err error) { func categories(...any) (terms []models.WpTermsMy, err error) {
var in = []interface{}{"category"} var in = []any{"category"}
terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{ terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{
{"tt.count", ">", "0", "int"}, {"tt.count", ">", "0", "int"},
{"tt.taxonomy", "in", ""}, {"tt.taxonomy", "in", ""},

View File

@ -31,8 +31,8 @@ type indexHandle struct {
orderBy models.SqlBuilder orderBy models.SqlBuilder
order string order string
join models.SqlBuilder join models.SqlBuilder
postType []interface{} postType []any
status []interface{} status []any
header string header string
paginationStep int paginationStep int
} }
@ -52,8 +52,8 @@ func newIndexHandle(ctx *gin.Context) *indexHandle {
}, },
orderBy: models.SqlBuilder{}, orderBy: models.SqlBuilder{},
join: models.SqlBuilder{}, join: models.SqlBuilder{},
postType: []interface{}{"post"}, postType: []any{"post"},
status: []interface{}{"publish"}, status: []any{"publish"},
} }
} }
func (h *indexHandle) setTitleLR(l, r string) { func (h *indexHandle) setTitleLR(l, r string) {

View File

@ -16,7 +16,7 @@ type Model interface {
} }
type ParseWhere interface { type ParseWhere interface {
ParseWhere(in ...[]interface{}) (string, []interface{}) ParseWhere(in ...[]any) (string, []any)
} }
type SqlBuilder [][]string type SqlBuilder [][]string
@ -38,7 +38,7 @@ func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
} }
} }
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]interface{}, in [][]interface{}) (t bool) { func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in [][]any) (t bool) {
if ss[1] == "in" && len(in) > 0 { if ss[1] == "in" && len(in) > 0 {
s.WriteString(" (") s.WriteString(" (")
for _, p := range in[*c] { for _, p := range in[*c] {
@ -55,7 +55,7 @@ func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]int
return t return t
} }
func (w SqlBuilder) parseType(ss []string, args *[]interface{}) { func (w SqlBuilder) parseType(ss []string, args *[]any) {
if len(ss) == 4 && ss[3] == "int" { if len(ss) == 4 && ss[3] == "int" {
i, _ := strconv.Atoi(ss[2]) i, _ := strconv.Atoi(ss[2])
*args = append(*args, i) *args = append(*args, i)
@ -67,9 +67,9 @@ func (w SqlBuilder) parseType(ss []string, args *[]interface{}) {
} }
} }
func (w SqlBuilder) ParseWhere(in ...[]interface{}) (string, []interface{}) { func (w SqlBuilder) ParseWhere(in ...[]any) (string, []any) {
var s strings.Builder var s strings.Builder
args := make([]interface{}, 0, len(w)) args := make([]any, 0, len(w))
c := 0 c := 0
for _, ss := range w { for _, ss := range w {
if len(ss) == 2 { if len(ss) == 2 {
@ -163,7 +163,7 @@ func (w SqlBuilder) parseJoin() string {
return s.String() return s.String()
} }
func SimplePagination[T Model](where ParseWhere, fields, group string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]interface{}) (r []T, total int, err error) { func SimplePagination[T Model](where ParseWhere, fields, group string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]any) (r []T, total int, err error) {
var rr T var rr T
w, args := where.ParseWhere(in...) w, args := where.ParseWhere(in...)
n := struct { n := struct {
@ -221,7 +221,7 @@ func FindOneById[T Model](id int) (T, error) {
return r, nil return r, nil
} }
func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...[]interface{}) (T, error) { func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...[]any) (T, error) {
var r T var r T
w, args := where.ParseWhere(in...) w, args := where.ParseWhere(in...)
tp := "select %s from %s %s %s" tp := "select %s from %s %s %s"
@ -233,7 +233,7 @@ func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...
return r, nil return r, nil
} }
func LastOne[T Model](where ParseWhere, fields string, in ...[]interface{}) (T, error) { func LastOne[T Model](where ParseWhere, fields string, in ...[]any) (T, error) {
var r T var r T
w, args := where.ParseWhere(in...) w, args := where.ParseWhere(in...)
tp := "select %s from %s %s order by %s desc limit 1" tp := "select %s from %s %s order by %s desc limit 1"
@ -245,7 +245,7 @@ func LastOne[T Model](where ParseWhere, fields string, in ...[]interface{}) (T,
return r, nil return r, nil
} }
func SimpleFind[T Model](where ParseWhere, fields string, in ...[]interface{}) ([]T, error) { func SimpleFind[T Model](where ParseWhere, fields string, in ...[]any) ([]T, error) {
var r []T var r []T
var rr T var rr T
w, args := where.ParseWhere(in...) w, args := where.ParseWhere(in...)
@ -258,7 +258,7 @@ func SimpleFind[T Model](where ParseWhere, fields string, in ...[]interface{}) (
return r, nil return r, nil
} }
func Select[T Model](sql string, params ...interface{}) ([]T, error) { func Select[T Model](sql string, params ...any) ([]T, error) {
var r []T var r []T
var rr T var rr T
sql = strings.Replace(sql, "{table}", rr.Table(), -1) sql = strings.Replace(sql, "{table}", rr.Table(), -1)
@ -269,10 +269,10 @@ func Select[T Model](sql string, params ...interface{}) ([]T, error) {
return r, nil return r, nil
} }
func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) { func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, limit int, in ...[]any) (r []T, err error) {
var rr T var rr T
w := "" w := ""
var args []interface{} var args []any
if where != nil { if where != nil {
w, args = where.ParseWhere(in...) w, args = where.ParseWhere(in...)
} }
@ -295,7 +295,7 @@ func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, joi
return return
} }
func Get[T Model](sql string, params ...interface{}) (r T, err error) { func Get[T Model](sql string, params ...any) (r T, err error) {
sql = strings.Replace(sql, "{table}", r.Table(), -1) sql = strings.Replace(sql, "{table}", r.Table(), -1)
err = db.Db.Get(&r, sql, params...) err = db.Db.Get(&r, sql, params...)
return return

View File

@ -18,10 +18,10 @@ func SetupRouter() *gin.Engine {
// gin.DisableConsoleColor() // gin.DisableConsoleColor()
r := gin.Default() r := gin.Default()
r.HTMLRender = templates.NewFsTemplate(template.FuncMap{ r.HTMLRender = templates.NewFsTemplate(template.FuncMap{
"unescaped": func(s string) interface{} { "unescaped": func(s string) any {
return template.HTML(s) return template.HTML(s)
}, },
"dateCh": func(t time.Time) interface{} { "dateCh": func(t time.Time) any {
return t.Format("2006年 01月 02日") return t.Format("2006年 01月 02日")
}, },
}).SetTemplate() }).SetTemplate()