-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagx.go
92 lines (70 loc) · 1.66 KB
/
tagx.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package tagx
import (
"reflect"
"strings"
)
type Tag struct {
Value string
Children []Tag
}
const privateFieldVal = "-"
func Extract(structure any, tagKey string) []Tag {
return extract(reflect.TypeOf(structure), tagKey, nil)
}
func extract(typ reflect.Type, tagKey string, seen func(p reflect.Type) bool) []Tag {
var tags []Tag
if typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return nil
}
// avoid recursive data structures
if seen != nil && seen(typ) {
return nil
}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
rawTag := field.Tag.Get(tagKey)
tagParts := strings.Split(rawTag, ",")
if len(tagParts) == 0 {
continue
}
if tagParts[0] == privateFieldVal {
continue
}
if strings.TrimSpace(tagParts[0]) == "" {
continue
}
tags = append(tags, Tag{
Value: tagParts[0],
Children: extract(field.Type, tagKey, func(childType reflect.Type) bool {
if childType.PkgPath() == "" || childType.Name() == "" {
return false
}
if childType.PkgPath() == typ.PkgPath() && childType.Name() == typ.Name() {
return true
}
return seen != nil && seen(childType)
}),
})
}
return tags
}
func ExtractFlat(structure any, tagKey, delimiter string) []string {
tags := Extract(structure, tagKey)
return extractFlat(tags, delimiter)
}
func extractFlat(tags []Tag, delimiter string) []string {
var tagVals []string
for _, t := range tags {
if len(t.Children) == 0 {
tagVals = append(tagVals, t.Value)
continue
}
for _, v := range extractFlat(t.Children, delimiter) {
tagVals = append(tagVals, t.Value+delimiter+v)
}
}
return tagVals
}