From 58d3e4f645ebb041cac541ae7d0b2c8cf62b5122 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 21 Aug 2024 21:11:27 +0800 Subject: [PATCH] optimize code and update package version --- app/plugins/wphandle/apply/apply.go | 27 ++++++++++++++++++++++----- app/plugins/wphandle/handle.go | 2 +- app/theme/hook.go | 3 --- app/theme/wp/wp.go | 2 +- go.mod | 28 ++++++++++++++-------------- multipTemplate/multiptemplate.go | 6 ------ safety/rwmap.go | 2 +- 7 files changed, 39 insertions(+), 31 deletions(-) diff --git a/app/plugins/wphandle/apply/apply.go b/app/plugins/wphandle/apply/apply.go index 8dd418b..8d58ee8 100644 --- a/app/plugins/wphandle/apply/apply.go +++ b/app/plugins/wphandle/apply/apply.go @@ -2,12 +2,29 @@ package apply import "github.com/fthvgb1/wp-go/safety" -var fn safety.Var[any] +var contains = safety.NewMap[string, any]() -func SetFn(f any) { - fn.Store(f) +func SetVal(key string, val any) { + contains.Store(key, val) } -func UsePlugins() any { - return fn.Load() +func DelVal(key string) { + contains.Delete(key) +} + +func GetVal[V any](key string) (V, bool) { + v, ok := contains.Load(key) + if !ok { + var vv V + return vv, ok + } + return v.(V), ok +} +func GetRawVal(key string) (any, bool) { + return contains.Load(key) +} + +func GetPlugins() any { + v, _ := contains.Load("wp-plugins") + return v } diff --git a/app/plugins/wphandle/handle.go b/app/plugins/wphandle/handle.go index fe63baa..03ef816 100644 --- a/app/plugins/wphandle/handle.go +++ b/app/plugins/wphandle/handle.go @@ -66,7 +66,7 @@ func LoadPlugins() { } RegisterPlugin(name, plu) } - apply.SetFn(func(h *wp.Handle) { + apply.SetVal("wp-plugins", func(h *wp.Handle) { UsePlugins(h) }) } diff --git a/app/theme/hook.go b/app/theme/hook.go index 5acbcad..47914f6 100644 --- a/app/theme/hook.go +++ b/app/theme/hook.go @@ -9,9 +9,6 @@ import ( var themeMap = safety.NewMap[string, func(*wp.Handle)]() func AddThemeHookFunc(name string, fn func(handle *wp.Handle)) { - if _, ok := themeMap.Load(name); ok { - panic("exists same name theme") - } themeMap.Store(name, fn) } diff --git a/app/theme/wp/wp.go b/app/theme/wp/wp.go index c7eca6b..ecb119d 100644 --- a/app/theme/wp/wp.go +++ b/app/theme/wp/wp.go @@ -110,7 +110,7 @@ func SetConfigHandle(a ...any) Handle { h.C = hh.C h.theme = hh.theme configFn(h) - v := apply.UsePlugins() + v := apply.GetPlugins() pluginFn, ok := v.(func(*Handle)) if ok { pluginFn(h) diff --git a/go.mod b/go.mod index cac2a7a..d251ef1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/fthvgb1/wp-go go 1.20 require ( - github.com/dlclark/regexp2 v1.11.0 + github.com/dlclark/regexp2 v1.11.4 github.com/elliotchance/phpserialize v1.4.0 github.com/gin-contrib/gzip v1.0.1 github.com/gin-contrib/pprof v1.5.0 @@ -11,29 +11,29 @@ require ( github.com/gin-gonic/gin v1.10.0 github.com/go-playground/locales v0.14.1 github.com/go-playground/universal-translator v0.18.1 - github.com/go-playground/validator/v10 v10.21.0 + github.com/go-playground/validator/v10 v10.22.0 github.com/go-sql-driver/mysql v1.8.1 github.com/goccy/go-json v0.10.3 github.com/jmoiron/sqlx v1.4.0 - golang.org/x/crypto v0.24.0 - golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 + golang.org/x/crypto v0.26.0 + golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/yaml.v2 v2.4.0 ) require ( filippo.io/edwards25519 v1.1.0 // indirect - github.com/bytedance/sonic v1.11.8 // indirect - github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/bytedance/sonic v1.12.1 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect - github.com/gabriel-vasile/mimetype v1.4.4 // indirect + github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/gorilla/context v1.1.2 // indirect github.com/gorilla/securecookie v1.1.2 // indirect - github.com/gorilla/sessions v1.2.2 // indirect + github.com/gorilla/sessions v1.3.0 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect @@ -41,11 +41,11 @@ require ( github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect - golang.org/x/arch v0.8.0 // indirect - golang.org/x/net v0.26.0 // indirect - golang.org/x/sys v0.21.0 // indirect - golang.org/x/text v0.16.0 // indirect - google.golang.org/protobuf v1.34.1 // indirect + golang.org/x/arch v0.9.0 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/multipTemplate/multiptemplate.go b/multipTemplate/multiptemplate.go index 4aa3470..35064ea 100644 --- a/multipTemplate/multiptemplate.go +++ b/multipTemplate/multiptemplate.go @@ -71,9 +71,6 @@ func NewFsTemplates(f embed.FS, m maps) *MultipleFsTemplate { } func (t *MultipleFileTemplate) SetTemplate(name string, templ *template.Template) *MultipleFileTemplate { - if _, ok := t.Template.Load(name); ok { - panic("exists same template " + name) - } t.Template.Store(name, templ) return t } @@ -105,9 +102,6 @@ func (t *MultipleFsTemplate) AddTemplate(mainTemplatePattern string, fnMap templ panic(err) } for _, mainTemplate := range mainTemplates { - if _, ok := t.Template.Load(mainTemplate); ok { - panic("exists same Template " + mainTemplate) - } file := filepath.Base(mainTemplate) pattern := append([]string{mainTemplate}, layoutTemplatePattern...) t.Template.Store(mainTemplate, template.Must(template.New(file).Funcs(fnMap).ParseFS(t.Fs, pattern...))) diff --git a/safety/rwmap.go b/safety/rwmap.go index c1091c1..8a5ac56 100644 --- a/safety/rwmap.go +++ b/safety/rwmap.go @@ -30,7 +30,7 @@ func (v *RWMap[K, V]) Load(key K) (V, bool) { return val, ok } -func (v *RWMap[K, V]) Del(keys ...K) { +func (v *RWMap[K, V]) Delete(keys ...K) { v.mux.Lock() defer v.mux.Unlock() for _, key := range keys {