From b6b92ede34fb34e985880d0a49530d1d49f82915 Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 12 Mar 2023 20:28:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- helper/func.go | 36 ++++++++++++++ helper/func_test.go | 73 +++++++++++++++++++++++++++++ helper/maps/map.go | 19 ++++++++ internal/theme/wp/archive.go | 50 +++++++++++--------- internal/theme/wp/recentcomments.go | 45 ++++++++++-------- internal/theme/wp/recentposts.go | 46 ++++++++++-------- internal/theme/wp/search.go | 31 +++++++----- 8 files changed, 228 insertions(+), 74 deletions(-) diff --git a/go.mod b/go.mod index 12c9ce7..38dbb1c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/fthvgb1/wp-go -go 1.19 +go 1.20 require ( github.com/dlclark/regexp2 v1.7.0 diff --git a/helper/func.go b/helper/func.go index 695c1b8..be8370d 100644 --- a/helper/func.go +++ b/helper/func.go @@ -1,8 +1,11 @@ package helper import ( + "fmt" + str "github.com/fthvgb1/wp-go/helper/strings" "net/url" "reflect" + "strconv" "strings" ) @@ -58,3 +61,36 @@ func DefaultVal[T any](v, defaults T) T { } return v } + +func IsZero[T comparable](t T) bool { + var vv T + return vv != t +} +func IsZeros(v any) bool { + switch v.(type) { + case int64, int, int8, int16, int32, uint64, uint, uint8, uint16, uint32: + i := fmt.Sprintf("%d", v) + return str.ToInt[int](i) == 0 + case float32, float64: + f := fmt.Sprintf("%v", v) + ff, _ := strconv.ParseFloat(f, 64) + return ff == float64(0) + case bool: + return v.(bool) == false + case string: + s := v.(string) + return s == "" + } + return false +} + +func ToBool[T comparable](t T) bool { + v := any(t) + switch v.(type) { + case string: + s := v.(string) + return s != "" && s != "0" + } + var vv T + return vv != t +} diff --git a/helper/func_test.go b/helper/func_test.go index 26e8363..b718244 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -144,3 +144,76 @@ func TestDefaults(t *testing.T) { }) } } + +func TestToBool(t *testing.T) { + { + name := "bool" + args := true + want := true + t.Run(name, func(t *testing.T) { + if got := ToBool(args); got != want { + t.Errorf("ToBool() = %v, want %v", got, want) + } + }) + } + { + name := "int" + args := 0 + want := false + t.Run(name, func(t *testing.T) { + if got := ToBool(args); got != want { + t.Errorf("ToBool() = %v, want %v", got, want) + } + }) + } + { + name := "int" + args := 1 + want := true + t.Run(name, func(t *testing.T) { + if got := ToBool(args); got != want { + t.Errorf("ToBool() = %v, want %v", got, want) + } + }) + } + { + name := "string" + args := "1" + want := true + t.Run(name, func(t *testing.T) { + if got := ToBool(args); got != want { + t.Errorf("ToBool() = %v, want %v", got, want) + } + }) + } + { + name := "string" + args := "0" + want := false + t.Run(name, func(t *testing.T) { + if got := ToBool(args); got != want { + t.Errorf("ToBool() = %v, want %v", got, want) + } + }) + } + { + name := "string" + args := "" + want := false + t.Run(name, func(t *testing.T) { + if got := ToBool(args); got != want { + t.Errorf("ToBool() = %v, want %v", got, want) + } + }) + } + { + name := "float" + args := 0.2 + want := true + t.Run(name, func(t *testing.T) { + if got := ToBool(args); got != want { + t.Errorf("ToBool() = %v, want %v", got, want) + } + }) + } +} diff --git a/helper/maps/map.go b/helper/maps/map.go index b9d7dd8..f3ee9af 100644 --- a/helper/maps/map.go +++ b/helper/maps/map.go @@ -2,6 +2,7 @@ package maps import ( "encoding/json" + "github.com/fthvgb1/wp-go/helper" ) func StrAnyMapToStruct[T any, M any](m M) (r T, err error) { @@ -109,6 +110,24 @@ func Merge[K comparable, V any](m ...map[K]V) map[K]V { return mm } +func FilterZeroMerge[K comparable, V any](m ...map[K]V) map[K]V { + if len(m) < 1 { + panic("no map") + } else if len(m) < 2 { + return m[0] + } + mm := m[0] + for _, m2 := range m[1:] { + for k, v := range m2 { + if helper.IsZeros(v) { + continue + } + mm[k] = v + } + } + return mm +} + func WithDefaultVal[K comparable, V any](m map[K]V, k K, defaults V) V { vv, ok := m[k] if ok { diff --git a/internal/theme/wp/archive.go b/internal/theme/wp/archive.go index 04ac07a..753f977 100644 --- a/internal/theme/wp/archive.go +++ b/internal/theme/wp/archive.go @@ -9,29 +9,35 @@ import ( "github.com/fthvgb1/wp-go/internal/pkg/constraints/components" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/wpconfig" + "github.com/fthvgb1/wp-go/safety" "strings" ) -var archivesConfig = map[any]any{ - "count": int64(0), - "dropdown": int64(0), - "title": "归档", -} +var archivesConfig = func() safety.Var[map[any]any] { + v := safety.Var[map[any]any]{} + v.Store(map[any]any{ + "count": int64(0), + "dropdown": int64(0), + "title": "归档", + }) + archiveArgs.Store(map[string]string{ + "{$before_widget}": `", + "{$before_title}": `

`, + "{$after_title}": "

", + "{$before_sidebar}": "", + "{$after_sidebar}": "", + "{$nav}": "", + "{$navCloser}": "", + "{$title}": "", + "{$dropdown_id}": "archives-dropdown-2", + "{$dropdown_type}": "monthly", + "{$dropdown_label}": "选择月份", + }) + return v +}() -var archiveArgs = map[string]string{ - "{$before_widget}": `", - "{$before_title}": `

`, - "{$after_title}": "

", - "{$before_sidebar}": "", - "{$after_sidebar}": "", - "{$nav}": "", - "{$navCloser}": "", - "{$title}": "", - "{$dropdown_id}": "archives-dropdown-2", - "{$dropdown_type}": "monthly", - "{$dropdown_label}": "选择月份", -} +var archiveArgs = safety.Var[map[string]string]{} var archiveTemplate = `{$before_widget} {$title} @@ -42,9 +48,9 @@ var archiveTemplate = `{$before_widget} ` func Archive(h *Handle) string { - args := GetComponentsArgs(h, components.ArchiveArgs, archiveArgs) - args = maps.Merge(archiveArgs, args) - conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig, int64(2)) + args := GetComponentsArgs(h, components.ArchiveArgs, archiveArgs.Load()) + args = maps.FilterZeroMerge(archiveArgs.Load(), args) + conf := wpconfig.GetPHPArrayVal("widget_archives", archivesConfig.Load(), int64(2)) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) s := archiveTemplate if int64(1) == conf["dropdown"].(int64) { diff --git a/internal/theme/wp/recentcomments.go b/internal/theme/wp/recentcomments.go index 0964800..0d37f38 100644 --- a/internal/theme/wp/recentcomments.go +++ b/internal/theme/wp/recentcomments.go @@ -9,26 +9,32 @@ import ( "github.com/fthvgb1/wp-go/internal/pkg/constraints/components" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/wpconfig" + "github.com/fthvgb1/wp-go/safety" "strings" ) -var recentCommentsArgs = map[string]string{ - "{$before_widget}": `", - "{$before_title}": `

`, - "{$after_title}": "

", - "{$before_sidebar}": "", - "{$after_sidebar}": "", - "{$nav}": "", - "{$navCloser}": "", - "{$title}": "", - "{$recent_comments_id}": "recentcomments", -} +var recentCommentsArgs = func() safety.Var[map[string]string] { + v := safety.Var[map[string]string]{} + v.Store(map[string]string{ + "{$before_widget}": `", + "{$before_title}": `

`, + "{$after_title}": "

", + "{$before_sidebar}": "", + "{$after_sidebar}": "", + "{$nav}": "", + "{$navCloser}": "", + "{$title}": "", + "{$recent_comments_id}": "recentcomments", + }) + recentCommentConf.Store(map[any]any{ + "number": int64(5), + "title": "近期评论", + }) + return v +}() -var recentCommentConf = map[any]any{ - "number": int64(5), - "title": "近期评论", -} +var recentCommentConf = safety.Var[map[any]any]{} var recentCommentsTemplate = `{$before_widget} {$nav} @@ -41,9 +47,10 @@ var recentCommentsTemplate = `{$before_widget} ` func RecentComments(h *Handle) string { - args := GetComponentsArgs(h, components.RecentCommentsArgs, recentCommentsArgs) - args = maps.Merge(recentCommentsArgs, args) - conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-comments", recentCommentConf, int64(2)) + args := GetComponentsArgs(h, components.RecentCommentsArgs, recentCommentsArgs.Load()) + args = maps.FilterZeroMerge(recentCommentsArgs.Load(), args) + conf := wpconfig.GetPHPArrayVal("widget_recent-comments", recentCommentConf.Load(), int64(2)) + conf = maps.FilterZeroMerge(recentCommentConf.Load(), conf) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") { args["{$nav}"] = fmt.Sprintf(`