forked from worldline-go/struct2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
45 lines (34 loc) · 797 Bytes
/
fields.go
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
package struct2
import (
"reflect"
)
func (d *Decoder) GetFields(s interface{}) []string {
v := value2StructValue(reflect.ValueOf(s))
exportedFieldNames := []string{}
d.getFields(v, func(sf reflect.StructField) {
if isFieldExported(sf) {
tagName, _ := d.parseTag(sf)
if tagName == "" {
tagName = sf.Name
}
exportedFieldNames = append(exportedFieldNames, tagName)
}
})
return exportedFieldNames
}
func isFieldExported(f reflect.StructField) bool {
return f.PkgPath == ""
}
func (d *Decoder) getFields(v reflect.Value, fn func(reflect.StructField)) {
t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !field.IsExported() {
continue
}
if tagValue, _ := d.getTagValue(field); tagValue == "-" {
continue
}
fn(field)
}
}