Skip to content

Commit

Permalink
collect error
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaowc-mac committed Dec 19, 2023
1 parent 4f48673 commit 9b01b90
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
7 changes: 5 additions & 2 deletions core/middleware/error.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package middleware

import (
"github.com/gin-gonic/gin"
"log"
"net/http"
"runtime/debug"

"github.com/gin-gonic/gin"
)

func GinErrorHttp(c *gin.Context) {
Expand All @@ -12,8 +14,9 @@ func GinErrorHttp(c *gin.Context) {
//打印错误堆栈信息
debug.PrintStack()
//封装通用json返回
log.Println("recover err ", r)
c.JSON(http.StatusInternalServerError, gin.H{
"message": r.(string),
"message": "服务器异常",
"code": http.StatusInternalServerError,
})
}
Expand Down
110 changes: 110 additions & 0 deletions core/utils/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package utils

import (
"reflect"
)

type CollectData struct {
Arr interface{}
}

// CollectStruct 声明集合
func CollectStruct(arr interface{}) *CollectData {
return &CollectData{Arr: arr}
}

// interface 转 maps
func interFaceToMaps(v interface{}) []map[string]interface{} {
val := reflect.ValueOf(v)

if val.Kind() == reflect.Ptr {
val = val.Elem()
}

if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
panic("The passed argument is not a slice or array")
}

result := make([]map[string]interface{}, val.Len())

for i := 0; i < val.Len(); i++ {
result[i] = make(map[string]interface{})
result[i] = interFaceToMap(val.Index(i).Interface())
}

return result
}

// interface 转 map
func interFaceToMap(v interface{}) map[string]interface{} {
val := reflect.ValueOf(v)
t := reflect.TypeOf(v)

if val.Kind() == reflect.Ptr {
val = val.Elem()
}
result := make(map[string]interface{})

for j := 0; j < val.NumField(); j++ {
valInfo := val.Field(j)

if valInfo.Kind() == reflect.Ptr {
valInfo = valInfo.Elem()
}
if t.Field(j).Anonymous {
for i, v := range interFaceToMap(valInfo.Interface()) {
result[i] = v
}
} else {
result[val.Type().Field(j).Name] = valInfo.Interface()
}
}

return result
}

// PluckToArray Pluck 获取一列数据到数组
func (c *CollectData) PluckToArray(key string, result interface{}) {
arr := interFaceToMaps(c.Arr)

v := reflect.ValueOf(result)

if v.Kind() != reflect.Ptr {
panic("The passed argument is not a ptr")
}

typ := reflect.TypeOf(result).Elem()

if typ.Kind() != reflect.Slice {
panic("The passed argument is not a *slice")
}

val := reflect.MakeSlice(typ, 0, 0)

for _, i := range arr {
val = reflect.Append(val, reflect.ValueOf(i[key]))
}

v.Elem().Set(val)
}

// PluckToMap Pluck 获取一列数据到数组
func (c *CollectData) PluckToMap(key, value string, result interface{}) {
arr := interFaceToMaps(c.Arr)

v := reflect.ValueOf(result)

if v.Kind() != reflect.Ptr {
panic("The passed argument is not a ptr")
}

typ := reflect.TypeOf(result).Elem()

if typ.Kind() != reflect.Map {
panic("The passed argument is not a map")
}

for _, i := range arr {
v.Elem().SetMapIndex(reflect.ValueOf(i[key]), reflect.ValueOf(i[value]))
}
}

0 comments on commit 9b01b90

Please sign in to comment.