WalkFields get the field data by tag
(t reflect.Type, filter func(field *reflect.StructField) bool)
| 23 | |
| 24 | // WalkFields get the field data by tag |
| 25 | func WalkFields(t reflect.Type, filter func(field *reflect.StructField) bool) (f []reflect.StructField) { |
| 26 | if t.Kind() == reflect.Ptr { |
| 27 | t = t.Elem() |
| 28 | } |
| 29 | |
| 30 | if filter == nil { |
| 31 | for i := 0; i < t.NumField(); i++ { |
| 32 | field := t.Field(i) |
| 33 | if field.Type.Kind() == reflect.Struct { |
| 34 | f = append(f, WalkFields(field.Type, filter)...) |
| 35 | } else { |
| 36 | f = append(f, field) |
| 37 | } |
| 38 | } |
| 39 | } else { |
| 40 | for i := 0; i < t.NumField(); i++ { |
| 41 | field := t.Field(i) |
| 42 | if filter(&field) { |
| 43 | f = append(f, field) |
| 44 | } else if field.Type.Kind() == reflect.Struct { |
| 45 | f = append(f, WalkFields(field.Type, filter)...) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return f |
| 51 | } |
no outgoing calls