代码优化
This commit is contained in:
+22
-9
@@ -87,9 +87,9 @@ func ServeJsonWithExtra(c *gin.Context, code stderr.Code, data interface{}, extr
|
|||||||
log.Any("Code", code))
|
log.Any("Code", code))
|
||||||
}
|
}
|
||||||
if !IsNilOrEmpty(data) && code == stderr.Success {
|
if !IsNilOrEmpty(data) && code == stderr.Success {
|
||||||
t := reflect.TypeOf(data)
|
if kind, ok := isRespDataTypeValid(data); !ok {
|
||||||
if !(t.Kind() == reflect.Map || t.Kind() == reflect.Struct || t.Kind() == reflect.Slice) {
|
|
||||||
log.WarnX(c, "[===TypeError===] Return Data Type error is not struct or slice",
|
log.WarnX(c, "[===TypeError===] Return Data Type error is not struct or slice",
|
||||||
|
log.Any("kind", kind),
|
||||||
log.Any("path", c.Request.URL.Path))
|
log.Any("path", c.Request.URL.Path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -161,10 +161,9 @@ func ServeToJSON(c *gin.Context, code stderr.Code, data interface{}) {
|
|||||||
log.Any("Code", code))
|
log.Any("Code", code))
|
||||||
}
|
}
|
||||||
if !IsNilOrEmpty(data) && code == stderr.Success {
|
if !IsNilOrEmpty(data) && code == stderr.Success {
|
||||||
t := reflect.TypeOf(data)
|
if kind, ok := isRespDataTypeValid(data); !ok {
|
||||||
if !(t.Kind() == reflect.Map || t.Kind() == reflect.Struct || t.Kind() == reflect.Slice || t.Kind() == reflect.Ptr) {
|
|
||||||
log.WarnX(c, "[===TypeError===] Return Data Type error is not struct or slice",
|
log.WarnX(c, "[===TypeError===] Return Data Type error is not struct or slice",
|
||||||
log.Any("kind", t.Kind()),
|
log.Any("kind", kind),
|
||||||
log.Any("path", c.Request.URL.Path))
|
log.Any("path", c.Request.URL.Path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,9 +226,9 @@ func serveJsonLogic(c *gin.Context, code stderr.Code, data interface{}, extra ma
|
|||||||
log.Any("Code", code))
|
log.Any("Code", code))
|
||||||
}
|
}
|
||||||
if !IsNilOrEmpty(data) && code == stderr.Success {
|
if !IsNilOrEmpty(data) && code == stderr.Success {
|
||||||
t := reflect.TypeOf(data)
|
if kind, ok := isRespDataTypeValid(data); !ok {
|
||||||
if !(t.Kind() == reflect.Map || t.Kind() == reflect.Struct || t.Kind() == reflect.Slice) {
|
|
||||||
log.WarnX(c, "[===TypeError===] Return Data Type error is not struct or slice",
|
log.WarnX(c, "[===TypeError===] Return Data Type error is not struct or slice",
|
||||||
|
log.Any("kind", kind),
|
||||||
log.Any("path", c.Request.URL.Path))
|
log.Any("path", c.Request.URL.Path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -429,6 +428,20 @@ func IsNilOrEmpty(in interface{}) bool {
|
|||||||
return in == nil || in == ""
|
return in == nil || in == ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isRespDataTypeValid 返回数据是否为可直接下发的类型(map/结构体/切片),
|
||||||
|
// 指针先解引用再判断,避免返回 *struct 的接口被误报 TypeError。
|
||||||
|
func isRespDataTypeValid(data interface{}) (reflect.Kind, bool) {
|
||||||
|
t := reflect.TypeOf(data)
|
||||||
|
if t == nil {
|
||||||
|
return reflect.Invalid, false
|
||||||
|
}
|
||||||
|
for t.Kind() == reflect.Ptr {
|
||||||
|
t = t.Elem()
|
||||||
|
}
|
||||||
|
kind := t.Kind()
|
||||||
|
return kind, kind == reflect.Map || kind == reflect.Struct || kind == reflect.Slice
|
||||||
|
}
|
||||||
|
|
||||||
// GetIP 获取真实IP
|
// GetIP 获取真实IP
|
||||||
func GetIP(ctx *gin.Context) string {
|
func GetIP(ctx *gin.Context) string {
|
||||||
relIP, exists := ctx.Get(constant.CtxIP)
|
relIP, exists := ctx.Get(constant.CtxIP)
|
||||||
@@ -501,9 +514,9 @@ func ServeJsonWithExtraNoEncrypt(c *gin.Context, code stderr.Code, data interfac
|
|||||||
log.Any("Code", code))
|
log.Any("Code", code))
|
||||||
}
|
}
|
||||||
if !IsNilOrEmpty(data) && code == stderr.Success {
|
if !IsNilOrEmpty(data) && code == stderr.Success {
|
||||||
t := reflect.TypeOf(data)
|
if kind, ok := isRespDataTypeValid(data); !ok {
|
||||||
if !(t.Kind() == reflect.Map || t.Kind() == reflect.Struct || t.Kind() == reflect.Slice) {
|
|
||||||
log.WarnX(c, "[===TypeError===] Return Data Type error is not struct or slice",
|
log.WarnX(c, "[===TypeError===] Return Data Type error is not struct or slice",
|
||||||
|
log.Any("kind", kind),
|
||||||
log.Any("path", c.Request.URL.Path))
|
log.Any("path", c.Request.URL.Path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user