优化代码,添加字符串转整数函数
This commit is contained in:
parent
f31f7a4cc0
commit
404f27f693
|
@ -49,6 +49,11 @@ func AnyAnyToStrAny(m map[any]any) (r map[string]any) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsExists[K comparable, V any](m map[K]V, k K) bool {
|
||||||
|
_, ok := m[k]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
func Reduce[T, V any, K comparable](m map[K]V, fn func(K, V, T) T, r T) T {
|
func Reduce[T, V any, K comparable](m map[K]V, fn func(K, V, T) T, r T) T {
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
r = fn(k, v, r)
|
r = fn(k, v, r)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package number
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"golang.org/x/exp/constraints"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -195,10 +196,10 @@ func TestRand(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAbs(t *testing.T) {
|
func TestAbs(t *testing.T) {
|
||||||
type args[T Number] struct {
|
type args[T constraints.Integer | constraints.Float] struct {
|
||||||
n T
|
n T
|
||||||
}
|
}
|
||||||
type testCase[T Number] struct {
|
type testCase[T constraints.Integer | constraints.Float] struct {
|
||||||
name string
|
name string
|
||||||
args args[T]
|
args args[T]
|
||||||
want T
|
want T
|
||||||
|
|
|
@ -3,7 +3,9 @@ package strings
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"golang.org/x/exp/constraints"
|
||||||
"io"
|
"io"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,6 +22,17 @@ func Join(s ...string) (str string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToInteger[T constraints.Integer](s string, defaults T) T {
|
||||||
|
if s == "" {
|
||||||
|
return defaults
|
||||||
|
}
|
||||||
|
i, err := strconv.ParseInt(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return defaults
|
||||||
|
}
|
||||||
|
return T(i)
|
||||||
|
}
|
||||||
|
|
||||||
func Md5(str string) string {
|
func Md5(str string) string {
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
_, err := io.WriteString(h, str)
|
_, err := io.WriteString(h, str)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package strings
|
package strings
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"golang.org/x/exp/constraints"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestStrJoin(t *testing.T) {
|
func TestStrJoin(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
|
@ -21,3 +24,32 @@ func TestStrJoin(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToInteger(t *testing.T) {
|
||||||
|
type args[T constraints.Integer] struct {
|
||||||
|
s string
|
||||||
|
z T
|
||||||
|
}
|
||||||
|
type testCase[T constraints.Integer] struct {
|
||||||
|
name string
|
||||||
|
args args[T]
|
||||||
|
want T
|
||||||
|
}
|
||||||
|
tests := []testCase[int64]{
|
||||||
|
{
|
||||||
|
name: "t1",
|
||||||
|
args: args[int64]{
|
||||||
|
"10",
|
||||||
|
0,
|
||||||
|
},
|
||||||
|
want: int64(10),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := ToInteger[int64](tt.args.s, tt.args.z); got != tt.want {
|
||||||
|
t.Errorf("StrToInt() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/internal/mail"
|
"github.com/fthvgb1/wp-go/internal/mail"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||||
|
@ -14,7 +15,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -92,8 +92,8 @@ func PostComment(c *gin.Context) {
|
||||||
}
|
}
|
||||||
cc := c.Copy()
|
cc := c.Copy()
|
||||||
go func() {
|
go func() {
|
||||||
id, err := strconv.ParseUint(i, 10, 64)
|
id := str.ToInteger[uint64](i, 0)
|
||||||
if err != nil {
|
if id <= 0 {
|
||||||
logs.ErrPrintln(err, "获取文档id", i)
|
logs.ErrPrintln(err, "获取文档id", i)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package actions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||||
|
@ -10,7 +11,6 @@ import (
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type detailHandler struct {
|
type detailHandler struct {
|
||||||
|
@ -47,18 +47,11 @@ func Detail(c *gin.Context) {
|
||||||
t := theme.GetTemplateName()
|
t := theme.GetTemplateName()
|
||||||
theme.Hook(t, code, c, ginH, plugins.Detail, status)
|
theme.Hook(t, code, c, ginH, plugins.Detail, status)
|
||||||
}()
|
}()
|
||||||
id := c.Param("id")
|
ID := str.ToInteger[uint64](c.Param("id"), 0)
|
||||||
Id := 0
|
|
||||||
if id != "" {
|
|
||||||
Id, err = strconv.Atoi(id)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ID := uint64(Id)
|
|
||||||
maxId, err := cache.GetMaxPostId(c)
|
maxId, err := cache.GetMaxPostId(c)
|
||||||
logs.ErrPrintln(err, "get max post id")
|
logs.ErrPrintln(err, "get max post id")
|
||||||
if ID > maxId || err != nil {
|
if ID > maxId || ID <= 0 || err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
post, err := cache.GetPostById(c, ID)
|
post, err := cache.GetPostById(c, ID)
|
||||||
|
@ -92,12 +85,7 @@ func Detail(c *gin.Context) {
|
||||||
ginH["post"] = post
|
ginH["post"] = post
|
||||||
ginH["showComment"] = showComment
|
ginH["showComment"] = showComment
|
||||||
ginH["prev"] = prev
|
ginH["prev"] = prev
|
||||||
depth := wpconfig.Options.Value("thread_comments_depth")
|
d := str.ToInteger(wpconfig.Options.Value("thread_comments_depth"), 5)
|
||||||
d, err := strconv.Atoi(depth)
|
|
||||||
if err != nil {
|
|
||||||
logs.ErrPrintln(err, "get comment depth ", depth)
|
|
||||||
d = 5
|
|
||||||
}
|
|
||||||
ginH["maxDep"] = d
|
ginH["maxDep"] = d
|
||||||
ginH["next"] = next
|
ginH["next"] = next
|
||||||
ginH["user"] = user
|
ginH["user"] = user
|
||||||
|
|
|
@ -3,6 +3,7 @@ package actions
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/maps"
|
||||||
"github.com/fthvgb1/wp-go/helper/number"
|
"github.com/fthvgb1/wp-go/helper/number"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
|
@ -52,13 +53,12 @@ type indexHandle struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIndexHandle(ctx *gin.Context) *indexHandle {
|
func newIndexHandle(ctx *gin.Context) *indexHandle {
|
||||||
size := wpconfig.Options.Value("posts_per_page")
|
size := str.ToInteger(wpconfig.Options.Value("posts_per_page"), 10)
|
||||||
si, _ := strconv.Atoi(size)
|
|
||||||
return &indexHandle{
|
return &indexHandle{
|
||||||
c: ctx,
|
c: ctx,
|
||||||
session: sessions.Default(ctx),
|
session: sessions.Default(ctx),
|
||||||
page: 1,
|
page: 1,
|
||||||
pageSize: si,
|
pageSize: size,
|
||||||
paginationStep: 1,
|
paginationStep: 1,
|
||||||
titleL: wpconfig.Options.Value("blogname"),
|
titleL: wpconfig.Options.Value("blogname"),
|
||||||
titleR: wpconfig.Options.Value("blogdescription"),
|
titleR: wpconfig.Options.Value("blogdescription"),
|
||||||
|
@ -93,24 +93,20 @@ func (h *indexHandle) getSearchKey() string {
|
||||||
return fmt.Sprintf("action:%s|%s|%s|%s|%s|%s|%d|%d", h.author, h.search, h.orderBy, h.order, h.category, h.categoryType, h.page, h.pageSize)
|
return fmt.Sprintf("action:%s|%s|%s|%s|%s|%s|%d|%d", h.author, h.search, h.orderBy, h.order, h.category, h.categoryType, h.page, h.pageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
var orders = []string{"asc", "desc"}
|
var orders = map[string]struct{}{"asc": {}, "desc": {}}
|
||||||
|
|
||||||
func (h *indexHandle) parseParams() (err error) {
|
func (h *indexHandle) parseParams() (err error) {
|
||||||
h.order = h.c.Query("order")
|
h.order = h.c.Query("order")
|
||||||
|
if !maps.IsExists(orders, h.order) {
|
||||||
if !slice.IsContained(h.order, orders) {
|
|
||||||
order := config.GetConfig().PostOrder
|
order := config.GetConfig().PostOrder
|
||||||
h.order = "asc"
|
h.order = "asc"
|
||||||
if order != "" && slice.IsContained(order, orders) {
|
if order != "" && maps.IsExists(orders, order) {
|
||||||
h.order = order
|
h.order = order
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
year := h.c.Param("year")
|
year := h.c.Param("year")
|
||||||
if year != "" {
|
if year != "" {
|
||||||
y, er := strconv.Atoi(year)
|
y := str.ToInteger(year, -1)
|
||||||
if er != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if y > time.Now().Year() || y <= 1970 {
|
if y > time.Now().Year() || y <= 1970 {
|
||||||
return errors.New(str.Join("year err : ", year))
|
return errors.New(str.Join("year err : ", year))
|
||||||
}
|
}
|
||||||
|
@ -120,11 +116,8 @@ func (h *indexHandle) parseParams() (err error) {
|
||||||
}
|
}
|
||||||
month := h.c.Param("month")
|
month := h.c.Param("month")
|
||||||
if month != "" {
|
if month != "" {
|
||||||
m, err := strconv.Atoi(month)
|
m := str.ToInteger(month, -1)
|
||||||
if err != nil {
|
if !maps.IsExists(months, m) {
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, ok := months[m]; !ok {
|
|
||||||
return errors.New(str.Join("months err ", month))
|
return errors.New(str.Join("months err ", month))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,27 +130,26 @@ func (h *indexHandle) parseParams() (err error) {
|
||||||
h.scene = plugins.Archive
|
h.scene = plugins.Archive
|
||||||
}
|
}
|
||||||
category := h.c.Param("category")
|
category := h.c.Param("category")
|
||||||
if category == "" {
|
|
||||||
category = h.c.Param("tag")
|
|
||||||
if category != "" {
|
if category != "" {
|
||||||
h.scene = plugins.Tag
|
|
||||||
allNames := cache.AllCategoryTagsNames(h.c, plugins.Tag)
|
|
||||||
if _, ok := allNames[category]; !ok {
|
|
||||||
return errors.New(str.Join("not exists tag ", category))
|
|
||||||
}
|
|
||||||
h.categoryType = "post_tag"
|
|
||||||
h.header = fmt.Sprintf("标签: <span>%s</span>", category)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
h.scene = plugins.Category
|
h.scene = plugins.Category
|
||||||
allNames := cache.AllCategoryTagsNames(h.c, plugins.Category)
|
if !maps.IsExists(cache.AllCategoryTagsNames(h.c, plugins.Category), category) {
|
||||||
if _, ok := allNames[category]; !ok {
|
|
||||||
return errors.New(str.Join("not exists category ", category))
|
return errors.New(str.Join("not exists category ", category))
|
||||||
}
|
}
|
||||||
h.categoryType = "category"
|
h.categoryType = "category"
|
||||||
h.header = fmt.Sprintf("分类: <span>%s</span>", category)
|
h.header = fmt.Sprintf("分类: <span>%s</span>", category)
|
||||||
}
|
|
||||||
h.category = category
|
h.category = category
|
||||||
|
}
|
||||||
|
tag := h.c.Param("tag")
|
||||||
|
if tag != "" {
|
||||||
|
h.scene = plugins.Tag
|
||||||
|
if !maps.IsExists(cache.AllCategoryTagsNames(h.c, plugins.Tag), tag) {
|
||||||
|
return errors.New(str.Join("not exists tag ", tag))
|
||||||
|
}
|
||||||
|
h.categoryType = "post_tag"
|
||||||
|
h.header = fmt.Sprintf("标签: <span>%s</span>", tag)
|
||||||
|
h.category = tag
|
||||||
|
}
|
||||||
|
|
||||||
username := h.c.Param("author")
|
username := h.c.Param("author")
|
||||||
if username != "" {
|
if username != "" {
|
||||||
allUsername, er := cache.GetAllUsername(h.c)
|
allUsername, er := cache.GetAllUsername(h.c)
|
||||||
|
@ -165,7 +157,7 @@ func (h *indexHandle) parseParams() (err error) {
|
||||||
err = er
|
err = er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, ok := allUsername[username]; !ok {
|
if !maps.IsExists(allUsername, username) {
|
||||||
err = errors.New(str.Join("user ", username, " is not exists"))
|
err = errors.New(str.Join("user ", username, " is not exists"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -179,9 +171,9 @@ func (h *indexHandle) parseParams() (err error) {
|
||||||
"post_author", "=", strconv.FormatUint(user.Id, 10), "int",
|
"post_author", "=", strconv.FormatUint(user.Id, 10), "int",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if category != "" {
|
if h.category != "" {
|
||||||
h.where = append(h.where, []string{
|
h.where = append(h.where, []string{
|
||||||
"d.name", category,
|
"d.name", h.category,
|
||||||
}, []string{"taxonomy", h.categoryType})
|
}, []string{"taxonomy", h.categoryType})
|
||||||
h.join = append(h.join, []string{
|
h.join = append(h.join, []string{
|
||||||
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
|
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
|
||||||
|
@ -190,7 +182,7 @@ func (h *indexHandle) parseParams() (err error) {
|
||||||
}, []string{
|
}, []string{
|
||||||
"left join", "wp_terms d", "c.term_id=d.term_id",
|
"left join", "wp_terms d", "c.term_id=d.term_id",
|
||||||
})
|
})
|
||||||
h.setTitleLR(category, wpconfig.Options.Value("blogname"))
|
h.setTitleLR(h.category, wpconfig.Options.Value("blogname"))
|
||||||
}
|
}
|
||||||
s := h.c.Query("s")
|
s := h.c.Query("s")
|
||||||
if s != "" && strings.Replace(s, " ", "", -1) != "" {
|
if s != "" && strings.Replace(s, " ", "", -1) != "" {
|
||||||
|
@ -206,15 +198,7 @@ func (h *indexHandle) parseParams() (err error) {
|
||||||
h.search = s
|
h.search = s
|
||||||
h.scene = plugins.Search
|
h.scene = plugins.Search
|
||||||
}
|
}
|
||||||
p := h.c.Query("paged")
|
h.page = str.ToInteger(h.c.Param("page"), 1)
|
||||||
if p == "" {
|
|
||||||
p = h.c.Param("page")
|
|
||||||
}
|
|
||||||
if p != "" {
|
|
||||||
if pa, err := strconv.Atoi(p); err == nil {
|
|
||||||
h.page = pa
|
|
||||||
}
|
|
||||||
}
|
|
||||||
total := int(atomic.LoadInt64(&dao.TotalRaw))
|
total := int(atomic.LoadInt64(&dao.TotalRaw))
|
||||||
if total > 0 && total < (h.page-1)*h.pageSize {
|
if total > 0 && total < (h.page-1)*h.pageSize {
|
||||||
h.page = 1
|
h.page = 1
|
||||||
|
|
13
internal/pkg/cache/feed.go
vendored
13
internal/pkg/cache/feed.go
vendored
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fthvgb1/wp-go/cache"
|
"github.com/fthvgb1/wp-go/cache"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||||
|
@ -11,7 +12,6 @@ import (
|
||||||
"github.com/fthvgb1/wp-go/plugin/digest"
|
"github.com/fthvgb1/wp-go/plugin/digest"
|
||||||
"github.com/fthvgb1/wp-go/rss2"
|
"github.com/fthvgb1/wp-go/rss2"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -93,17 +93,10 @@ func feed(arg ...any) (xml []string, err error) {
|
||||||
func postFeed(arg ...any) (x string, err error) {
|
func postFeed(arg ...any) (x string, err error) {
|
||||||
c := arg[0].(*gin.Context)
|
c := arg[0].(*gin.Context)
|
||||||
id := arg[1].(string)
|
id := arg[1].(string)
|
||||||
Id := 0
|
ID := str.ToInteger[uint64](id, 0)
|
||||||
if id != "" {
|
|
||||||
Id, err = strconv.Atoi(id)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ID := uint64(Id)
|
|
||||||
maxId, err := GetMaxPostId(c)
|
maxId, err := GetMaxPostId(c)
|
||||||
logs.ErrPrintln(err, "get max post id")
|
logs.ErrPrintln(err, "get max post id")
|
||||||
if ID > maxId || err != nil {
|
if ID < 1 || ID > maxId || err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
post, err := GetPostById(c, ID)
|
post, err := GetPostById(c, ID)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user