-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhaowc-mac
committed
Dec 19, 2023
1 parent
4f48673
commit 9b01b90
Showing
2 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
} | ||
} |