From cf5408751ef63b68a3e95fe362d39d5ff4ca58bd Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Mon, 21 Oct 2024 12:11:02 +0800 Subject: [PATCH] add a helper function (#443) --- cmd/dashboard/controller/jwt.go | 9 +++++---- cmd/dashboard/controller/server_group.go | 2 +- go.mod | 2 +- pkg/utils/utils.go | 14 ++++++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/cmd/dashboard/controller/jwt.go b/cmd/dashboard/controller/jwt.go index de41342..e975b2e 100644 --- a/cmd/dashboard/controller/jwt.go +++ b/cmd/dashboard/controller/jwt.go @@ -2,15 +2,16 @@ package controller import ( "encoding/json" - "fmt" "net/http" "time" jwt "github.com/appleboy/gin-jwt/v2" "github.com/gin-gonic/gin" - "github.com/naiba/nezha/model" - "github.com/naiba/nezha/service/singleton" "golang.org/x/crypto/bcrypt" + + "github.com/naiba/nezha/model" + "github.com/naiba/nezha/pkg/utils" + "github.com/naiba/nezha/service/singleton" ) func initParams() *jwt.GinJWTMiddleware { @@ -96,7 +97,7 @@ func authenticator() func(c *gin.Context) (interface{}, error) { return nil, jwt.ErrFailedAuthentication } - return fmt.Sprintf("%d", user.ID), nil + return utils.Itoa(user.ID), nil } } diff --git a/cmd/dashboard/controller/server_group.go b/cmd/dashboard/controller/server_group.go index 5950b56..4ea70d8 100644 --- a/cmd/dashboard/controller/server_group.go +++ b/cmd/dashboard/controller/server_group.go @@ -27,7 +27,7 @@ func listServerGroup(c *gin.Context) { c.JSON(http.StatusOK, model.CommonResponse[[]model.ServerGroup]{ Success: err == nil, Data: sg, - Error: utils.IfOrFn[string](err == nil, func() string { + Error: utils.IfOrFn(err == nil, func() string { return err.Error() }, func() string { return "" diff --git a/go.mod b/go.mod index 55696e7..03ddb93 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( github.com/swaggo/swag v1.16.4 github.com/tidwall/gjson v1.18.0 golang.org/x/crypto v0.26.0 + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 golang.org/x/net v0.28.0 golang.org/x/sync v0.8.0 google.golang.org/grpc v1.63.0 @@ -86,7 +87,6 @@ require ( go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/arch v0.9.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/mod v0.18.0 // indirect golang.org/x/sys v0.24.0 // indirect golang.org/x/text v0.17.0 // indirect diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index c47a4c9..b2d2ffa 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -5,8 +5,11 @@ import ( "math/big" "os" "regexp" + "strconv" "strings" + "golang.org/x/exp/constraints" + jsoniter "github.com/json-iterator/go" ) @@ -104,3 +107,14 @@ func IfOrFn[T any](a bool, x, y func() T) T { } return y() } + +func Itoa[T constraints.Integer](i T) string { + switch any(i).(type) { + case int, int8, int16, int32, int64: + return strconv.FormatInt(int64(i), 10) + case uint, uint8, uint16, uint32, uint64: + return strconv.FormatUint(uint64(i), 10) + default: + return "" + } +}