From f28720ab672c751f82b7d606209e2b99c8d7d0ca Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Wed, 23 Apr 2025 13:58:16 +0800 Subject: [PATCH] refactor: optimize type assertions and simplify sort option checks in utils.go - Streamlined type assertions in convertFilterValue function for improved clarity and performance. - Removed unnecessary nil checks for sorts in GetSortOptionFromString and GetSortsOption functions, enhancing code readability. --- core/controllers/utils.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/core/controllers/utils.go b/core/controllers/utils.go index 960542d1..5d87d5c7 100644 --- a/core/controllers/utils.go +++ b/core/controllers/utils.go @@ -113,21 +113,18 @@ func convertFilterValue(value interface{}) interface{} { } case reflect.Float64: // JSON numbers are decoded as float64 by default - switch value.(type) { + switch typed := value.(type) { case float64: - num := value.(float64) // Check if it's a whole number - if num == float64(int64(num)) { - return int64(num) + if typed == float64(int64(typed)) { + return int64(typed) } else { - return num + return typed } case int: - num := value.(int) - return int64(num) + return int64(typed) case int64: - num := value.(int64) - return num + return typed } case reflect.Bool: return value.(bool) @@ -229,7 +226,7 @@ func GetSortOptionFromString(sortStr string) (sort bson.D, err error) { if err != nil { return nil, err } - if sorts == nil || len(sorts) == 0 { + if len(sorts) == 0 { return bson.D{{"_id", -1}}, nil } return SortsToOption(sorts) @@ -252,7 +249,7 @@ func GetSortsOption(c *gin.Context) (sort bson.D, err error) { return nil, err } - if sorts == nil || len(sorts) == 0 { + if len(sorts) == 0 { return bson.D{{"_id", -1}}, nil }