diff --git a/app/cmd/main.go b/app/cmd/main.go index eb18a4b..c0474ec 100644 --- a/app/cmd/main.go +++ b/app/cmd/main.go @@ -30,6 +30,12 @@ var address string var intReg = regexp.MustCompile(`^\d`) func init() { + defer func() { + if r := recover(); r != nil { + fmt.Println(r) + os.Exit(1) + } + }() flag.StringVar(&confPath, "c", "config.yaml", "config file support json,yaml or url") flag.StringVar(&address, "p", "", "listen address and port") flag.Parse() @@ -136,6 +142,12 @@ func signalNotify() { } func main() { + defer func() { + if r := recover(); r != nil { + fmt.Println(r) + os.Exit(1) + } + }() go signalNotify() Gin := route.SetupRouter() c := config.GetConfig() diff --git a/app/cmd/reload/reload.go b/app/cmd/reload/reload.go index 2943072..692679e 100644 --- a/app/cmd/reload/reload.go +++ b/app/cmd/reload/reload.go @@ -169,6 +169,27 @@ func VarsBy[T any](fn func() T) *safety.Var[T] { }) return ss } +func MapBy[K comparable, T any](fn func(*safety.Map[K, T])) *safety.Map[K, T] { + m := safety.NewMap[K, T]() + if fn != nil { + fn(m) + } + calls = append(calls, func() { + m.Flush() + if fn != nil { + fn(m) + } + }) + return m +} + +func SafeMap[K comparable, T any]() *safety.Map[K, T] { + m := safety.NewMap[K, T]() + calls = append(calls, func() { + m.Flush() + }) + return m +} func Push(fn ...func()) { calls = append(calls, fn...) diff --git a/app/mail/mail.go b/app/mail/mail.go index 13c9f64..111d16f 100644 --- a/app/mail/mail.go +++ b/app/mail/mail.go @@ -4,7 +4,7 @@ import ( "crypto/tls" "fmt" "github.com/fthvgb1/wp-go/app/pkg/config" - "github.com/soxfmr/gomail" + "gopkg.in/gomail.v2" "mime" "path" ) diff --git a/app/plugins/comment.go b/app/plugins/comment.go index 3e1ad8b..f6fa69e 100644 --- a/app/plugins/comment.go +++ b/app/plugins/comment.go @@ -6,7 +6,6 @@ import ( "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/gin-gonic/gin" - "net/url" "strconv" "strings" ) @@ -32,13 +31,12 @@ type CommentHtml interface { func FormatComments(c *gin.Context, i CommentHtml, comments []models.Comments, maxDepth int) string { tree := treeComments(comments) - u := c.Request.Header.Get("Referer") + var isTls bool - if u != "" { - uu, _ := url.Parse(u) - if uu.Scheme == "https" { - isTls = true - } + if c.Request.TLS != nil { + isTls = true + } else { + isTls = "https" == strings.ToLower(c.Request.Header.Get("X-Forwarded-Proto")) } h := CommentHandler{ Context: c, diff --git a/app/plugins/wphandle/hiddenlogin/hiddenlogin.go b/app/plugins/wphandle/hiddenlogin/hiddenlogin.go index 511c156..304334f 100644 --- a/app/plugins/wphandle/hiddenlogin/hiddenlogin.go +++ b/app/plugins/wphandle/hiddenlogin/hiddenlogin.go @@ -7,7 +7,7 @@ import ( ) func HiddenLogin(h *wp.Handle) { - h.PushComponentFilterFn(widgets.Meta, func(h *wp.Handle, s string, args ...any) string { + h.AddActionFilter(widgets.Meta, func(h *wp.Handle, s string, args ...any) string { return str.Replace(s, map[string]string{ `
  • 登录
  • `: "", `
  • 登录
  • `: "", diff --git a/app/theme/twentyfifteen/twentyfifteen.go b/app/theme/twentyfifteen/twentyfifteen.go index cec4cbf..7fc240b 100644 --- a/app/theme/twentyfifteen/twentyfifteen.go +++ b/app/theme/twentyfifteen/twentyfifteen.go @@ -18,12 +18,12 @@ func Hook(h *wp.Handle) { } func configs(h *wp.Handle) { - h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string { + h.AddActionFilter(widgets.Search, func(h *wp.Handle, s string, args ...any) string { return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`) }) wp.InitPipe(h) h.PushHandler(constraints.PipeMiddleware, constraints.Home, - wp.NewHandleFn(widget.IsCategory, 100, "widget.IsCategory")) + wp.NewHandleFn(widget.CheckCategory, 100, "widget.CheckCategory")) h.Index.SetPageEle(plugins.TwentyFifteenPagination()) h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10.005, CalCustomBackGround) diff --git a/app/theme/twentyseventeen/twentyseventeen.go b/app/theme/twentyseventeen/twentyseventeen.go index 0027cdd..244b2bd 100644 --- a/app/theme/twentyseventeen/twentyseventeen.go +++ b/app/theme/twentyseventeen/twentyseventeen.go @@ -37,13 +37,13 @@ func Hook(h *wp.Handle) { func configs(h *wp.Handle) { wp.InitPipe(h) h.PushHandler(constraints.PipeMiddleware, constraints.Home, - wp.NewHandleFn(widget.IsCategory, 100.006, "widget.IsCategory")) - h.PushComponentFilterFn("bodyClass", calClass) + wp.NewHandleFn(widget.CheckCategory, 100.006, "widget.CheckCategory")) + h.AddActionFilter("bodyClass", calClass) h.PushCacheGroupHeadScript(constraints.AllScene, "colorScheme-customHeader", 10, colorScheme, customHeader) components.WidgetArea(h) pushScripts(h) h.PushRender(constraints.AllStats, wp.NewHandleFn(calCustomHeader, 10.005, "calCustomHeader")) - h.SetComponentsArgs(widgets.Widget, map[string]string{ + wp.SetComponentsArgs(h, widgets.Widget, map[string]string{ "{$before_widget}": `
    `, "{$after_widget}": `
    `, }) @@ -176,7 +176,7 @@ func calClass(h *wp.Handle, s string, _ ...any) string { } func videoHeader(h *wp.Handle) { - h.PushComponentFilterFn("videoSetting", videoPlay) + h.AddActionFilter("videoSetting", videoPlay) wp.CustomVideo(h, constraints.Home) } diff --git a/app/theme/wp/calclass.go b/app/theme/wp/calclass.go index cd8e970..91c372e 100644 --- a/app/theme/wp/calclass.go +++ b/app/theme/wp/calclass.go @@ -77,7 +77,7 @@ func (h *Handle) BodyClass() string { if h.themeMods.ThemeSupport.ResponsiveEmbeds { class = append(class, "wp-embed-responsive") } - return h.ComponentFilterFnHook("bodyClass", strings.Join(class, " ")) + return h.DoActionFilter("bodyClass", strings.Join(class, " ")) } func postClass(h *Handle) func(posts models.Posts) string { @@ -113,7 +113,7 @@ func (h *Handle) PostClass(posts models.Posts) string { class = append(class, TermClass(term)) } - return h.ComponentFilterFnHook("postClass", strings.Join(class, " ")) + return h.DoActionFilter("postClass", strings.Join(class, " ")) } func TermClass(term models.TermsMy) string { diff --git a/app/theme/wp/components.go b/app/theme/wp/components.go index 8c59ae4..3cefd3f 100644 --- a/app/theme/wp/components.go +++ b/app/theme/wp/components.go @@ -190,7 +190,7 @@ func MergeComponentsArgsForMap[K comparable, V any](h *Handle, name string, m ma } } -func (h *Handle) SetComponentsArgs(key string, value any) { +func SetComponentsArgs(h *Handle, key string, value any) { h.componentsArgs[key] = value } @@ -199,10 +199,10 @@ func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) return fn, ok } -func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string, ...any) string) { +func (h *Handle) AddActionFilter(name string, fns ...func(*Handle, string, ...any) string) { h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...) } -func (h *Handle) ComponentFilterFnHook(name, s string, args ...any) string { +func (h *Handle) DoActionFilter(name, s string, args ...any) string { calls, ok := h.componentFilterFn[name] if ok { return slice.Reduce(calls, func(fn func(*Handle, string, ...any) string, r string) string { diff --git a/app/theme/wp/components/widget/archive.go b/app/theme/wp/components/widget/archive.go index 4bdfe78..08b03b6 100644 --- a/app/theme/wp/components/widget/archive.go +++ b/app/theme/wp/components/widget/archive.go @@ -62,7 +62,7 @@ func Archive(h *wp.Handle, id string) string { } else { s = strings.ReplaceAll(s, "{$html}", archiveUl(h, conf, args, cache.Archives(h.C))) } - return h.ComponentFilterFnHook(widgets.Archive, str.Replace(s, args)) + return h.DoActionFilter(widgets.Archive, str.Replace(s, args)) } var dropdownScript = ` diff --git a/app/theme/wp/components/widget/category.go b/app/theme/wp/components/widget/category.go index 486d59e..86318d9 100644 --- a/app/theme/wp/components/widget/category.go +++ b/app/theme/wp/components/widget/category.go @@ -69,7 +69,7 @@ func Category(h *wp.Handle, id string) string { } else { t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories)) } - return h.ComponentFilterFnHook(widgets.Categories, str.Replace(t, args)) + return h.DoActionFilter(widgets.Categories, str.Replace(t, args)) } func CategoryLi(h *wp.Handle, conf map[any]any, categories []models.TermsMy) string { @@ -232,10 +232,10 @@ func DropdownCategories(h *wp.Handle, args map[string]string, conf map[any]any, }) } s.WriteString(" \n") - return h.ComponentFilterFnHook("wp_dropdown_cats", s.String()) + return h.DoActionFilter("wp_dropdown_cats", s.String()) } -func IsCategory(h *wp.Handle) { +func CheckCategory(h *wp.Handle) { name, ok := parseDropdownCate(h) if ok { h.C.Redirect(http.StatusMovedPermanently, fmt.Sprintf("/p/category/%s", name)) @@ -267,3 +267,28 @@ func parseDropdownCate(h *wp.Handle) (cateName string, r bool) { cateName = cc.Name return } + +func IsCategory(h *wp.Handle) (category models.TermsMy, r bool) { + cate := wp.GetComponentsArgs[map[string]string](h, widgets.Categories, categoryArgs()) + name, ok := cate["{$name}"] + if !ok || name == "" { + return + } + cat := h.C.Query(name) + if cat == "" { + return + } + id := str.ToInteger[uint64](cat, 0) + if id < 1 { + return + } + i, cc := slice.SearchFirst(cache.CategoriesTags(h.C, constraints.Category), func(my models.TermsMy) bool { + return id == my.Terms.TermId + }) + if i < 0 { + return + } + r = true + category = cc + return +} diff --git a/app/theme/wp/components/widget/meta.go b/app/theme/wp/components/widget/meta.go index dc48f79..d37110d 100644 --- a/app/theme/wp/components/widget/meta.go +++ b/app/theme/wp/components/widget/meta.go @@ -57,5 +57,5 @@ func Meta(h *wp.Handle, id string) string { ss.Sprintf(`
  • 条目feed
  • `, "/feed") ss.Sprintf(`
  • 评论feed
  • `, "/comments/feed") s := strings.ReplaceAll(metaTemplate, "{$li}", ss.String()) - return h.ComponentFilterFnHook(widgets.Meta, str.Replace(s, args)) + return h.DoActionFilter(widgets.Meta, str.Replace(s, args)) } diff --git a/app/theme/wp/components/widget/recentcomments.go b/app/theme/wp/components/widget/recentcomments.go index 18b4982..61a68e7 100644 --- a/app/theme/wp/components/widget/recentcomments.go +++ b/app/theme/wp/components/widget/recentcomments.go @@ -64,5 +64,5 @@ func RecentComments(h *wp.Handle, id string) string { `, t.CommentAuthor, t.CommentPostId, t.CommentId, t.PostTitle) }) s := strings.ReplaceAll(recentCommentsTemplate, "{$li}", strings.Join(comments, "\n")) - return h.ComponentFilterFnHook(widgets.RecentComments, str.Replace(s, args)) + return h.DoActionFilter(widgets.RecentComments, str.Replace(s, args)) } diff --git a/app/theme/wp/components/widget/recentposts.go b/app/theme/wp/components/widget/recentposts.go index 82b96bd..d348d8b 100644 --- a/app/theme/wp/components/widget/recentposts.go +++ b/app/theme/wp/components/widget/recentposts.go @@ -85,5 +85,5 @@ func RecentPosts(h *wp.Handle, id string) string { `, t.Id, ariaCurrent, t.PostTitle, date) }) s := strings.ReplaceAll(recentPostsTemplate, "{$li}", strings.Join(posts, "\n")) - return h.ComponentFilterFnHook(widgets.RecentPosts, str.Replace(s, args)) + return h.DoActionFilter(widgets.RecentPosts, str.Replace(s, args)) } diff --git a/app/theme/wp/components/widget/search.go b/app/theme/wp/components/widget/search.go index 70ed862..4b2ff9e 100644 --- a/app/theme/wp/components/widget/search.go +++ b/app/theme/wp/components/widget/search.go @@ -77,5 +77,5 @@ func Search(h *wp.Handle, id string) string { val = html.SpecialChars(h.Index.Param.Search) } s = strings.ReplaceAll(s, "{$value}", val) - return h.ComponentFilterFnHook(widgets.Search, str.Replace(s, args)) + return h.DoActionFilter(widgets.Search, str.Replace(s, args)) } diff --git a/app/theme/wp/customheader.go b/app/theme/wp/customheader.go index abe362c..5b7fbd5 100644 --- a/app/theme/wp/customheader.go +++ b/app/theme/wp/customheader.go @@ -85,7 +85,7 @@ func GetVideoSetting(h *Handle, u string) (string, error) { if is := videoReg.FindString(u); is != "" { v.MimeType = "video/x-youtube" } - _ = h.ComponentFilterFnHook("videoSetting", "", &v) + _ = h.DoActionFilter("videoSetting", "", &v) s, err := json.Marshal(v) if err != nil { return "", err @@ -134,13 +134,14 @@ func CustomVideo(h *Handle, scene ...string) (ok bool) { } scripts = slice.Map(scripts, func(t string) string { return fmt.Sprintf(` -`, t, str.Replaces(t, [][]string{ - {"/wp-includes/js/dist/vendor/"}, - {"/wp-includes/js/dist/"}, - {"/wp-includes/js/"}, - {".min.js"}, - {".js"}, - {"wp-", ""}, +`, t, str.Replaces(t, []string{ + "/wp-includes/js/dist/vendor/", + "/wp-includes/js/dist/", + "/wp-includes/js/", + ".min.js", + ".js", + "wp-", + "", })) }) diff --git a/app/theme/wp/pipe.go b/app/theme/wp/pipe.go index 3aab760..b0fd493 100644 --- a/app/theme/wp/pipe.go +++ b/app/theme/wp/pipe.go @@ -100,7 +100,7 @@ func BuildPipe(pipeScene string, keyFn func(*Handle, string) string, fn func(*Ha func PipeKey(h *Handle, pipScene string) string { key := str.Join("pipekey", "-", pipScene, "-", h.scene, "-", h.Stats) - return h.ComponentFilterFnHook("pipeKey", key, pipScene) + return h.DoActionFilter("pipeKey", key, pipScene) } func Run(h *Handle, conf func(*Handle)) { @@ -133,7 +133,7 @@ func Run(h *Handle, conf func(*Handle)) { } func MiddlewareKey(h *Handle, pipScene string) string { - return h.ComponentFilterFnHook("middleware", "middleware", pipScene) + return h.DoActionFilter("middleware", "middleware", pipScene) } func PipeMiddlewareHandle(h *Handle, middlewares map[string][]HandleCall, key string) (handlers []HandleCall) { diff --git a/app/theme/wp/siteicon.go b/app/theme/wp/siteicon.go index 1fdc3ca..164342a 100644 --- a/app/theme/wp/siteicon.go +++ b/app/theme/wp/siteicon.go @@ -9,7 +9,7 @@ import ( "strings" ) -var sizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"} +var iconSizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"} func CalSiteIcon(h *Handle) (r string) { id := str.ToInteger[uint64](wpconfig.GetOption("site_icon"), 0) @@ -21,7 +21,7 @@ func CalSiteIcon(h *Handle) (r string) { return } m := strings.Join(strings.Split(icon.AttachmentMetadata.File, "/")[:2], "/") - size := slice.FilterAndMap(sizes, func(t string) (string, bool) { + size := slice.FilterAndMap(iconSizes, func(t string) (string, bool) { s, ok := icon.AttachmentMetadata.Sizes[t] if !ok { return "", false diff --git a/app/theme/wp/wp.go b/app/theme/wp/wp.go index 7af924d..e0f0536 100644 --- a/app/theme/wp/wp.go +++ b/app/theme/wp/wp.go @@ -1,6 +1,7 @@ package wp import ( + "errors" "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/constraints" @@ -14,6 +15,7 @@ import ( "html/template" "net/http" "os" + "strings" ) type Handle struct { @@ -41,6 +43,10 @@ type Handle struct { template *template.Template } +func (h *Handle) Theme() string { + return h.theme +} + func (h *Handle) GinH() gin.H { return h.ginH } @@ -152,7 +158,7 @@ func (h *Handle) Err() error { } func (h *Handle) SetErr(err error) { - h.err = err + h.err = errors.Join(err) } func (h *Handle) SetTempl(templ string) { @@ -281,3 +287,10 @@ func NewHandleFn(fn HandleFn[*Handle], order float64, name string) HandleCall { func NothingToDo(h *Handle) { h.Abort() } + +func (h *Handle) IsHttps() bool { + if h.C.Request.TLS != nil { + return true + } + return "https" == strings.ToLower(h.C.Request.Header.Get("X-Forwarded-Proto")) +} diff --git a/app/wpconfig/options.go b/app/wpconfig/options.go index f3e02bb..c82fd81 100644 --- a/app/wpconfig/options.go +++ b/app/wpconfig/options.go @@ -39,7 +39,13 @@ func GetOption(k string) string { if ok { return v } - vv, err := model.GetField[models.Options](ctx, "option_value", model.Conditions(model.Where(model.SqlBuilder{{"option_name", k}}))) + vv, err := model.GetField[models.Options](ctx, "option_value", + model.Conditions( + model.Where( + model.SqlBuilder{{"option_name", k}}, + ), + ), + ) options.Store(k, vv) if err != nil { return "" diff --git a/go.mod b/go.mod index 81a3897..eff3a33 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.7.0 + github.com/dlclark/regexp2 v1.10.0 github.com/elliotchance/phpserialize v1.3.3 github.com/gin-contrib/gzip v0.0.6 github.com/gin-contrib/pprof v1.4.0 @@ -11,39 +11,39 @@ require ( github.com/gin-gonic/gin v1.9.1 github.com/go-playground/locales v0.14.1 github.com/go-playground/universal-translator v0.18.1 - github.com/go-playground/validator/v10 v10.14.0 - github.com/go-sql-driver/mysql v1.6.0 + github.com/go-playground/validator/v10 v10.15.1 + github.com/go-sql-driver/mysql v1.7.1 github.com/goccy/go-json v0.10.2 github.com/jmoiron/sqlx v1.3.5 - github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0 - golang.org/x/crypto v0.9.0 - golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 + golang.org/x/crypto v0.12.0 + golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/yaml.v2 v2.4.0 ) require ( - github.com/bytedance/sonic v1.9.1 // indirect - github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/bytedance/sonic v1.10.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/chenzhuoyu/iasm v0.9.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/sessions v1.2.1 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/pelletier/go-toml/v2 v2.0.9 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect - golang.org/x/arch v0.3.0 // indirect - golang.org/x/net v0.10.0 // indirect - golang.org/x/sys v0.8.0 // indirect - golang.org/x/text v0.9.0 // indirect - google.golang.org/protobuf v1.30.0 // indirect + golang.org/x/arch v0.4.0 // indirect + golang.org/x/net v0.14.0 // indirect + golang.org/x/sys v0.11.0 // indirect + golang.org/x/text v0.12.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect - gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 6ee05f1..863d5f4 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,19 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= -github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= -github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= +github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk= +github.com/bytedance/sonic v1.10.0/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= -github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= +github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= +github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo= -github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0= +github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/elliotchance/phpserialize v1.3.3 h1:hV4QVmGdCiYgoBbw+ADt6fNgyZ2mYX0OgpnON1adTCM= github.com/elliotchance/phpserialize v1.3.3/go.mod h1:gt7XX9+ETUcLXbtTKEuyrqW3lcLUAeS/AnGZ2e49TZs= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= @@ -34,10 +38,11 @@ github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= -github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= -github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= -github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-playground/validator/v10 v10.15.1 h1:BSe8uhN+xQ4r5guV/ywQI4gO59C2raYcGffYWZEjZzM= +github.com/go-playground/validator/v10 v10.15.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= @@ -56,8 +61,9 @@ github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Cc github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= -github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= +github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= @@ -82,16 +88,14 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= -github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= -github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= +github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0= +github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= -github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0 h1:eVzIpz9oWTxss+/w5zHkUvCKNCt+1djBoTVlm2lgGQA= -github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0/go.mod h1:FInbtXuM/O1SXBIDwtDMgV8JDa4fHNoZ4QjItNNwA5g= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -102,8 +106,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= -github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= @@ -111,35 +115,35 @@ github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95 github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= -golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc= +golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= -golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= -golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 h1:frX3nT9RkKybPnjyI+yvZh6ZucTZatCCEm9D47sZ2zo= -golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= +golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ= +golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= +golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= +golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -155,4 +159,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/helper/func.go b/helper/func.go index 5493b87..801065b 100644 --- a/helper/func.go +++ b/helper/func.go @@ -5,6 +5,7 @@ import ( "fmt" str "github.com/fthvgb1/wp-go/helper/strings" "net/url" + "os" "reflect" "strconv" "strings" @@ -120,3 +121,8 @@ func IsImplements[T, A any](i A) (T, bool) { t, ok := a.(T) return t, ok } + +func FileExist(path string) bool { + _, err := os.Stat(path) + return err == nil +} diff --git a/helper/maps/function.go b/helper/maps/function.go index e8f6279..2323a7a 100644 --- a/helper/maps/function.go +++ b/helper/maps/function.go @@ -1,6 +1,10 @@ package maps -import "strings" +import ( + "github.com/fthvgb1/wp-go/helper/slice" + "golang.org/x/exp/constraints" + "strings" +) func GetStrAnyVal[T any](m map[string]any, key string, delimiter ...string) (r T, o bool) { separator := "." @@ -40,14 +44,14 @@ func GetStrAnyValWithDefaults[T any](m map[string]any, key string, defaults T) ( } // GetStrMapAnyValWithAny 使用"." 分隔层级 -func GetStrMapAnyValWithAny(key string, v map[string]any) (r any, o bool) { +func GetStrMapAnyValWithAny(v map[string]any, key string) (r any, o bool) { k := strings.Split(key, ".") if len(k) > 1 { val, ok := v[k[0]] if ok { vx, ok := val.(map[string]any) if ok { - r, o = GetStrMapAnyValWithAny(strings.Join(k[1:], "."), vx) + r, o = GetStrMapAnyValWithAny(vx, strings.Join(k[1:], ".")) } } } else { @@ -111,7 +115,7 @@ func GetAnyAnyValWithDefaults[T any](m map[any]any, defaults T, key ...any) (r T return } -func RecursiveSetStrVal[T any](m map[string]any, k string, v T, delimiter ...string) { +func SetStrAnyVal[T any](m map[string]any, k string, v T, delimiter ...string) { del := "." if len(delimiter) > 0 && delimiter[0] != "" { del = delimiter[0] @@ -148,7 +152,7 @@ func RecursiveSetStrVal[T any](m map[string]any, k string, v T, delimiter ...str mx[kk[len(kk)-1]] = v } -func RecursiveSetAnyVal[T any](m map[any]any, v T, k ...any) { +func SetAnyAnyVal[T any](m map[any]any, v T, k ...any) { if len(k) < 1 { return } else if len(k) == 1 { @@ -162,10 +166,10 @@ func RecursiveSetAnyVal[T any](m map[any]any, v T, k ...any) { mm = map[any]any{} preKey := k[0:i] if len(preKey) == 0 { - RecursiveSetAnyVal(m, mm, key...) + SetAnyAnyVal(m, mm, key...) } else { m, _ := GetAnyAnyMapVal[map[any]any](m, preKey...) - RecursiveSetAnyVal(m, mm, k[i]) + SetAnyAnyVal(m, mm, k[i]) } } } @@ -173,3 +177,26 @@ func RecursiveSetAnyVal[T any](m map[any]any, v T, k ...any) { mm, _ := GetAnyAnyMapVal[map[any]any](m, key...) mm[k[len(k)-1]] = v } + +func AscEahByKey[K constraints.Ordered, V any](m map[K]V, fn func(K, V)) { + orderedEahByKey(m, slice.ASC, fn) +} +func DescEahByKey[K constraints.Ordered, V any](m map[K]V, fn func(K, V)) { + orderedEahByKey(m, slice.ASC, fn) +} + +func orderedEahByKey[K constraints.Ordered, V any](m map[K]V, ordered int, fn func(K, V)) { + keys := Keys(m) + slice.Sorts(keys, ordered) + for _, key := range keys { + fn(key, m[key]) + } +} + +func Flip[K, V comparable](m map[K]V) map[V]K { + mm := make(map[V]K, len(m)) + for k, v := range m { + mm[v] = k + } + return mm +} diff --git a/helper/number/number.go b/helper/number/number.go index 40bbeee..7e22ce4 100644 --- a/helper/number/number.go +++ b/helper/number/number.go @@ -106,3 +106,8 @@ func Counters[T constraints.Integer]() func() T { return count } } + +func Round(f float64, precision int) float64 { + p := math.Pow10(precision) + return math.Floor(f*p+0.5) / p +} diff --git a/helper/strings/strings.go b/helper/strings/strings.go index b2219b9..c2077cf 100644 --- a/helper/strings/strings.go +++ b/helper/strings/strings.go @@ -93,14 +93,20 @@ func Replace(s string, replace map[string]string) string { } return s } -func Replaces(s string, replace [][]string) string { + +// Replaces replace string by slice as order +// +// []string sub []string like {old1,old2,old3,....,newString} all old[x] will be replaced by lasted newString +func Replaces(s string, replace ...[]string) string { for _, v := range replace { if len(v) < 1 { continue } else if len(v) == 1 { s = strings.ReplaceAll(s, v[0], "") } else { - s = strings.ReplaceAll(s, v[0], v[1]) + for _, s2 := range v[0 : len(v)-1] { + s = strings.ReplaceAll(s, s2, v[len(v)-1]) + } } } return s diff --git a/model/sqxquery.go b/model/sqxquery.go index cfc4dcb..c4ba7c4 100644 --- a/model/sqxquery.go +++ b/model/sqxquery.go @@ -82,6 +82,9 @@ func Scanner[T any](db *sqlx.DB, v T, s string, params ...any) func(func(T)) err func ToMapSlice[V any](db *sqlx.DB, dest *[]map[string]V, sql string, params ...any) (err error) { rows, err := db.Query(sql, params...) + if err != nil { + return err + } columns, err := rows.Columns() if err != nil { return err diff --git a/safety/safemap.go b/safety/safemap.go index 06a54bf..fcf9b6c 100644 --- a/safety/safemap.go +++ b/safety/safemap.go @@ -390,6 +390,15 @@ func (m *Map[K, V]) Range(f func(key K, value V) bool) { } } +func (m *Map[K, V]) Keys() []K { + var r []K + m.Range(func(key K, _ V) bool { + r = append(r, key) + return true + }) + return r +} + func (m *Map[K, V]) missLocked() { m.misses++ if m.misses < len(m.dirty) {