From ab56f45790ebd958708e40b744bf1756dcb03676 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 20 Mar 2023 13:02:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=BD=E6=95=B0=E9=87=8D=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/func.go | 2 +- helper/func_test.go | 4 ++-- internal/pkg/dao/common.go | 4 ++-- model/query.go | 2 +- model/sqxquery.go | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/helper/func.go b/helper/func.go index b46c67b..5fd64d4 100644 --- a/helper/func.go +++ b/helper/func.go @@ -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 diff --git a/helper/func_test.go b/helper/func_test.go index 09ecc09..eca596f 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -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) } }) } diff --git a/internal/pkg/dao/common.go b/internal/pkg/dao/common.go index 3956838..7168424 100644 --- a/internal/pkg/dao/common.go +++ b/internal/pkg/dao/common.go @@ -36,10 +36,10 @@ 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"}) } terms, err = model.Finds[models.TermsMy](ctx, model.Conditions( diff --git a/model/query.go b/model/query.go index f34b99b..10a9bc5 100644 --- a/model/query.go +++ b/model/query.go @@ -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 diff --git a/model/sqxquery.go b/model/sqxquery.go index fc57f3c..b80e993 100644 --- a/model/sqxquery.go +++ b/model/sqxquery.go @@ -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":