优化代码

This commit is contained in:
xing 2023-03-19 22:48:23 +08:00
parent 26bdcb44ac
commit 341b7197b8
5 changed files with 74 additions and 33 deletions

View File

@ -1,6 +1,7 @@
package helper package helper
import ( import (
"context"
"fmt" "fmt"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"net/url" "net/url"
@ -94,3 +95,15 @@ func ToBool[T comparable](t T) bool {
var vv T var vv T
return vv != t return vv != t
} }
func GetValFromContext[V, K any](ctx context.Context, k K, defaults V) V {
v := ctx.Value(k)
if v == nil {
return defaults
}
vv, ok := v.(V)
if !ok {
return defaults
}
return vv
}

View File

@ -1,6 +1,7 @@
package helper package helper
import ( import (
"context"
"fmt" "fmt"
"reflect" "reflect"
"testing" "testing"
@ -235,3 +236,34 @@ func TestIsZeros(t *testing.T) {
} }
}) })
} }
func TestGetValFromContext(t *testing.T) {
type args[K any, V any] struct {
ctx context.Context
k K
defaults V
}
type testCase[K any, V any] struct {
name string
args args[K, V]
want V
}
tests := []testCase[string, int]{
{
name: "t1",
args: args[string, int]{
ctx: context.WithValue(context.Background(), "kk", 1),
k: "kk",
defaults: 0,
},
want: 1,
},
}
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)
}
})
}
}

View File

@ -2,6 +2,7 @@ package dao
import ( import (
"context" "context"
"github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
@ -32,11 +33,17 @@ func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
in = []any{"post_tag"} in = []any{"post_tag"}
} }
} }
terms, err = model.Finds[models.TermsMy](ctx, model.Conditions( w := model.SqlBuilder{
model.Where(model.SqlBuilder{
{"tt.count", ">", "0", "int"},
{"tt.taxonomy", "in", ""}, {"tt.taxonomy", "in", ""},
}), }
if helper.GetValFromContext(ctx, "onlyTop", false) {
w = append(w, []string{"tt.parent", "=", "0", "int"})
}
if !helper.GetValFromContext(ctx, "showCountZero", false) {
w = append(w, []string{"tt.count", ">", "0", "int"})
}
terms, err = model.Finds[models.TermsMy](ctx, model.Conditions(
model.Where(w),
model.Fields("t.term_id"), model.Fields("t.term_id"),
model.Order(model.SqlBuilder{{"t.name", "asc"}}), model.Order(model.SqlBuilder{{"t.name", "asc"}}),
model.Join(model.SqlBuilder{ model.Join(model.SqlBuilder{

View File

@ -2,6 +2,7 @@ package model
import ( import (
"context" "context"
"github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
@ -64,20 +65,12 @@ func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition, page
return return
} }
q.Offset = offset q.Offset = offset
m := ctx.Value("handle=>toMap") m := helper.GetValFromContext[*[]map[string]string](ctx, "handle=>toMap", nil)
if m == nil { if m == nil {
r, err = finds[T](db, ctx, q) r, err = finds[T](db, ctx, q)
return return
} }
mm, ok := m.(*[]map[string]string) *m, err = findToStringMap[T](db, ctx, q)
if ok {
mx, er := findToStringMap[T](db, ctx, q)
if er != nil {
err = er
return
}
*mm = mx
}
return return
} }

View File

@ -3,6 +3,7 @@ package model
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper"
"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"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@ -35,11 +36,9 @@ 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 { func (r *SqlxQuery) Selects(ctx context.Context, dest any, sql string, params ...any) error {
v := ctx.Value("handle=>") v := helper.GetValFromContext(ctx, "handle=>", "")
if v != nil { if v != "" {
vv, ok := v.(string) switch v {
if ok && vv != "" {
switch vv {
case "string": case "string":
return ToMapSlice(r.sqlx, dest.(*[]map[string]string), sql, params...) return ToMapSlice(r.sqlx, dest.(*[]map[string]string), sql, params...)
case "scanner": case "scanner":
@ -47,21 +46,18 @@ func (r *SqlxQuery) Selects(ctx context.Context, dest any, sql string, params ..
return Scanner[any](r.sqlx, dest, sql, params...)(fn.(func(any))) return Scanner[any](r.sqlx, dest, sql, params...)(fn.(func(any)))
} }
} }
}
return r.sqlx.Select(dest, sql, params...) return r.sqlx.Select(dest, sql, params...)
} }
func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...any) error { func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...any) error {
v := ctx.Value("handle=>") v := helper.GetValFromContext(ctx, "handle=>", "")
if v != nil { if v != "" {
vv, ok := v.(string) switch v {
if ok && vv != "" {
switch vv {
case "string": case "string":
return GetToMap(r.sqlx, dest.(*map[string]string), sql, params...) return GetToMap(r.sqlx, dest.(*map[string]string), sql, params...)
} }
} }
}
return r.sqlx.Get(dest, sql, params...) return r.sqlx.Get(dest, sql, params...)
} }