fusenapi/utils/collect/collect.go

281 lines
5.3 KiB
Go
Raw Normal View History

2023-06-26 10:19:51 +00:00
package collect
import (
"reflect"
"strconv"
)
func ArrayColumn[R any, T any](arr []T, column string) []R {
2023-06-27 10:09:56 +00:00
defer func() {
if err := recover(); err != nil {
panic(err)
}
}()
2023-06-26 10:19:51 +00:00
var result []R
s := reflect.ValueOf(arr)
for i := 0; i < s.Len(); i++ {
e := s.Index(i)
2023-06-27 10:09:56 +00:00
if e.Kind() == reflect.Ptr {
e = e.Elem()
}
2023-06-26 10:19:51 +00:00
k := e.FieldByName(column)
2023-06-27 10:09:56 +00:00
if k.Kind() == reflect.Ptr {
k = k.Elem()
}
2023-06-26 10:19:51 +00:00
result = append(result, k.Interface().(R))
}
return result
}
2023-06-26 11:51:59 +00:00
func ArrayColumnTag[R any, T any](arrSrc []T, tag string) []R {
2023-06-27 10:09:56 +00:00
defer func() {
if err := recover(); err != nil {
panic(err)
}
}()
2023-06-26 11:51:59 +00:00
var result []R
arr := reflect.ValueOf(arrSrc)
if arr.Len() == 0 {
return result
}
2023-06-27 10:09:56 +00:00
ele := arr.Index(0)
if ele.Kind() == reflect.Ptr {
ele = ele.Elem()
}
eleType := ele.Type()
2023-06-26 11:51:59 +00:00
for j := 0; j < eleType.NumField(); j++ {
if value, ok := eleType.Field(j).Tag.Lookup("json"); ok && value == tag {
for i := 0; i < arr.Len(); i++ {
srcv := arr.Index(i)
2023-06-27 10:09:56 +00:00
if srcv.Kind() == reflect.Ptr {
srcv = srcv.Elem()
}
fv := srcv.Field(j)
if fv.Kind() == reflect.Ptr {
fv = fv.Elem()
}
2023-06-26 11:51:59 +00:00
result = append(result, fv.Interface().(R))
}
return result
}
}
return result
}
2023-06-26 10:19:51 +00:00
func ArrayIndex[T any](arr []T, index int) (result T, ok bool) {
2023-06-27 10:09:56 +00:00
2023-06-26 10:19:51 +00:00
if index < len(arr) {
result = arr[index]
ok = true
return
}
ok = false
return
}
func ArrayString2Int(arr interface{}) (result []int64) {
2023-06-27 10:09:56 +00:00
defer func() {
if err := recover(); err != nil {
panic(err)
}
}()
2023-06-26 10:19:51 +00:00
for _, a := range arr.([]string) {
v, err := strconv.ParseInt(a, 10, 64)
if err != nil {
panic(err)
}
result = append(result, v)
}
return result
}
func Array2MapByKey[KEY comparable, VALUE any](arrSrc []VALUE, fieldName string) (result map[KEY]VALUE) {
2023-06-27 10:09:56 +00:00
defer func() {
if err := recover(); err != nil {
panic(err)
}
}()
2023-06-26 10:19:51 +00:00
result = make(map[KEY]VALUE)
arr := reflect.ValueOf(arrSrc)
for i := 0; i < arr.Len(); i++ {
srcv := arr.Index(i)
2023-06-28 11:32:41 +00:00
if srcv.Kind() == reflect.Ptr {
if srcv.IsNil() {
continue
}
srcv = srcv.Elem()
}
fv := srcv.FieldByName(fieldName)
2023-06-26 10:19:51 +00:00
k := fv.Interface().(KEY)
result[k] = srcv.Interface().(VALUE)
}
return result
}
2023-06-28 11:32:41 +00:00
// 一个数组slice转换成 map[tag]slice ,以slice元素的某个tag为map
2023-06-26 10:19:51 +00:00
func Array2MapByKeyTag[KEY comparable, VALUE any](arrSrc []VALUE, tag string) (result map[KEY]VALUE) {
2023-06-27 10:09:56 +00:00
defer func() {
if err := recover(); err != nil {
panic(err)
}
}()
2023-06-26 10:19:51 +00:00
arr := reflect.ValueOf(arrSrc)
if arr.Len() == 0 {
return result
}
result = make(map[KEY]VALUE)
2023-06-27 10:09:56 +00:00
ele := arr.Index(0)
if ele.Kind() == reflect.Ptr {
ele = ele.Elem()
}
eleType := ele.Type()
2023-06-26 10:19:51 +00:00
for j := 0; j < eleType.NumField(); j++ {
if value, ok := eleType.Field(j).Tag.Lookup("json"); ok && value == tag {
for i := 0; i < arr.Len(); i++ {
srcv := arr.Index(i)
2023-06-27 10:09:56 +00:00
var fv reflect.Value
if srcv.Kind() == reflect.Ptr {
2023-06-28 11:32:41 +00:00
if srcv.IsNil() {
continue
}
2023-06-27 10:09:56 +00:00
fv = srcv.Elem().Field(j)
} else {
fv = srcv.Field(j)
}
if fv.Kind() == reflect.Ptr {
fv = fv.Elem()
}
2023-06-26 10:19:51 +00:00
k := fv.Interface().(KEY)
result[k] = srcv.Interface().(VALUE)
}
return
}
}
return result
}
2023-06-27 10:09:56 +00:00
func StructJson2Map(s interface{}) map[string]interface{} {
t := reflect.TypeOf(s)
v := reflect.ValueOf(s)
2023-06-28 11:32:41 +00:00
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
2023-06-27 10:09:56 +00:00
var data = make(map[string]interface{})
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
2023-06-28 11:32:41 +00:00
if tag, ok := field.Tag.Lookup("json"); ok {
val := v.Field(i)
if val.Kind() == reflect.Ptr && !val.IsNil() {
val = val.Elem()
}
data[tag] = val.Interface()
2023-06-27 10:09:56 +00:00
}
}
return data
}
func StructSliceJson2Maps(s interface{}) []map[string]interface{} {
slice := reflect.ValueOf(s)
var maps []map[string]interface{}
for i := 0; i < slice.Len(); i++ {
s := slice.Index(i)
2023-06-28 11:32:41 +00:00
if s.Kind() == reflect.Ptr && !s.IsNil() {
2023-06-27 10:09:56 +00:00
s = s.Elem()
}
structValue := s.Interface()
m := StructJson2Map(structValue)
maps = append(maps, m)
}
return maps
}
2023-06-28 11:32:41 +00:00
2023-06-29 10:04:59 +00:00
// LoadJsonTag 根据loader的json tag 赋值 给拥有相同json tag
2023-06-28 11:32:41 +00:00
func LoadJsonTag(v interface{}, loaded interface{}) {
vtype := reflect.TypeOf(v)
2023-06-29 10:04:59 +00:00
if vtype.Kind() != reflect.Ptr {
panic("v must is a pointer")
}
vtype = vtype.Elem()
var vvalue reflect.Value
2023-06-28 11:32:41 +00:00
if vtype.Kind() == reflect.Ptr {
vtype = vtype.Elem()
2023-06-29 10:04:59 +00:00
vvalue = reflect.ValueOf(v).Elem().Elem()
} else {
vvalue = reflect.ValueOf(v).Elem()
2023-06-28 11:32:41 +00:00
}
ltype := reflect.TypeOf(loaded)
2023-06-29 10:04:59 +00:00
if ltype.Kind() != reflect.Ptr {
panic("loaded must is a pointer")
}
ltype = ltype.Elem()
var lvalue reflect.Value
2023-06-28 11:32:41 +00:00
if ltype.Kind() == reflect.Ptr {
ltype = ltype.Elem()
2023-06-29 10:04:59 +00:00
lvalue = reflect.ValueOf(loaded).Elem().Elem()
} else {
lvalue = reflect.ValueOf(loaded).Elem()
2023-06-28 11:32:41 +00:00
}
for i := 0; i < vtype.NumField(); i++ {
vfield := vtype.Field(i)
if vtag, ok := vfield.Tag.Lookup("json"); ok {
for j := 0; j < ltype.NumField(); j++ {
lfield := ltype.Field(j)
if ltag, ok := lfield.Tag.Lookup("json"); ok && vtag == ltag {
vv := vvalue.Field(i)
lv := lvalue.Field(j)
2023-06-29 10:04:59 +00:00
2023-06-28 11:32:41 +00:00
if vv.Kind() == reflect.Ptr {
2023-06-29 10:04:59 +00:00
2023-06-28 11:32:41 +00:00
if lv.Kind() == reflect.Ptr {
vv.Set(lv)
} else {
2023-06-29 10:04:59 +00:00
x := reflect.New(vv.Type().Elem())
x.Elem().Set(lv)
vv.Set(x)
2023-06-28 11:32:41 +00:00
}
} else {
2023-06-29 10:04:59 +00:00
2023-06-28 11:32:41 +00:00
if lv.Kind() != reflect.Ptr {
vv.Set(lv)
} else {
vv.Set(lv.Elem())
}
2023-06-29 10:04:59 +00:00
2023-06-28 11:32:41 +00:00
}
}
}
}
}
}