-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflect.go
More file actions
38 lines (34 loc) · 753 Bytes
/
reflect.go
File metadata and controls
38 lines (34 loc) · 753 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
package helper
import "reflect"
func GetStructFields(item any) []reflect.StructField {
v := reflect.ValueOf(item)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
t := v.Type()
var ret []reflect.StructField
for i := 0; i < v.NumField(); i++ {
ret = append(ret, t.Field(i))
}
return ret
}
func GetStructFieldNames(item any) []string {
fields := GetStructFields(item)
ret := make([]string, 0, len(fields))
for _, field := range fields {
ret = append(ret, field.Name)
}
return ret
}
func GetStructJsons(item any) []string {
fields := GetStructFields(item)
ret := make([]string, 0, len(fields))
for _, field := range fields {
json := field.Tag.Get("json")
if json == "-" {
continue
}
ret = append(ret, json)
}
return ret
}