Compare commits

...

2 Commits

Author SHA1 Message Date
e1c1da6083 小优化 2023-03-20 23:41:57 +08:00
ab56f45790 函数重命名 2023-03-20 13:02:40 +08:00
6 changed files with 25 additions and 15 deletions

View File

@ -96,7 +96,7 @@ func ToBool[T comparable](t T) bool {
return vv != t
}
func GetValFromContext[V, K any](ctx context.Context, k K, defaults V) V {
func GetContextVal[V, K any](ctx context.Context, k K, defaults V) V {
v := ctx.Value(k)
if v == nil {
return defaults

View File

@ -261,8 +261,8 @@ func TestGetValFromContext(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetValFromContext(tt.args.ctx, tt.args.k, tt.args.defaults); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetValFromContext() = %v, want %v", got, tt.want)
if got := GetContextVal(tt.args.ctx, tt.args.k, tt.args.defaults); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetContextVal() = %v, want %v", got, tt.want)
}
})
}

View File

@ -36,16 +36,21 @@ func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
w := model.SqlBuilder{
{"tt.taxonomy", "in", ""},
}
if helper.GetValFromContext(ctx, "onlyTop", false) {
if helper.GetContextVal(ctx, "onlyTop", false) {
w = append(w, []string{"tt.parent", "=", "0", "int"})
}
if !helper.GetValFromContext(ctx, "showCountZero", false) {
if !helper.GetContextVal(ctx, "showCountZero", false) {
w = append(w, []string{"tt.count", ">", "0", "int"})
}
order := []string{"name", "asc"}
ord := helper.GetContextVal[[]string](ctx, "order", nil)
if ord != nil {
order = ord
}
terms, err = model.Finds[models.TermsMy](ctx, model.Conditions(
model.Where(w),
model.Fields("t.term_id"),
model.Order(model.SqlBuilder{{"t.name", "asc"}}),
model.Order(model.SqlBuilder{order}),
model.Join(model.SqlBuilder{
{"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"},
}),

View File

@ -76,13 +76,8 @@ func Category(h *wp.Handle) string {
return h.ComponentFilterFnHook(widgets.Categories, str.Replace(t, args))
}
func categoryUL(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string {
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{title}"])
args["{$navCloser}"] = "</nav>"
}
func CategoryLi(h *wp.Handle, conf map[any]any, categories []models.TermsMy) string {
s := str.NewBuilder()
s.WriteString("<ul>\n")
isCount := conf["count"].(int64)
currentCate := models.TermsMy{}
if h.Scene() == constraints.Category {
@ -118,7 +113,17 @@ func categoryUL(h *wp.Handle, args map[string]string, conf map[any]any, categori
r := m[0]
categoryLi(r, cate, tree.Ancestor(m, 0, cate), isCount, s)
}
return s.String()
}
func categoryUL(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string {
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{title}"])
args["{$navCloser}"] = "</nav>"
}
s := str.NewBuilder()
s.WriteString("<ul>\n")
s.WriteString(CategoryLi(h, conf, categories))
s.WriteString("</ul>")
return s.String()
}

View File

@ -65,7 +65,7 @@ func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition, page
return
}
q.Offset = offset
m := helper.GetValFromContext[*[]map[string]string](ctx, "handle=>toMap", nil)
m := helper.GetContextVal[*[]map[string]string](ctx, "handle=>toMap", nil)
if m == nil {
r, err = finds[T](db, ctx, q)
return

View File

@ -36,7 +36,7 @@ func SetGet(db *SqlxQuery, fn func(context.Context, any, string, ...any) error)
}
func (r *SqlxQuery) Selects(ctx context.Context, dest any, sql string, params ...any) error {
v := helper.GetValFromContext(ctx, "handle=>", "")
v := helper.GetContextVal(ctx, "handle=>", "")
if v != "" {
switch v {
case "string":
@ -51,7 +51,7 @@ func (r *SqlxQuery) Selects(ctx context.Context, dest any, sql string, params ..
}
func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...any) error {
v := helper.GetValFromContext(ctx, "handle=>", "")
v := helper.GetContextVal(ctx, "handle=>", "")
if v != "" {
switch v {
case "string":