forked from dollarkillerx/async_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflect.go
More file actions
46 lines (37 loc) · 785 Bytes
/
reflect.go
File metadata and controls
46 lines (37 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package async_utils
import (
"fmt"
"reflect"
)
type StructItem struct {
Data interface{}
Type string
}
func GetStructVal(st interface{}, field ...string) (result map[string]StructItem, err error) {
result = map[string]StructItem{}
t := reflect.TypeOf(st)
exs := sliceToSet(field)
if t.Kind().String() != "struct" {
return nil, fmt.Errorf("Not Struct")
}
v := reflect.ValueOf(st)
for i := 0; i < t.NumField(); i++ {
key := t.Field(i)
value := v.Field(i).Interface()
_, ex := exs[key.Name]
if ex {
result[key.Name] = StructItem{
Data: value,
Type: key.Type.String(),
}
}
}
return result, nil
}
func sliceToSet(field []string) map[string]bool {
output := map[string]bool{}
for _, v := range field {
output[v] = true
}
return output
}