-
Notifications
You must be signed in to change notification settings - Fork 0
/
cols.go
78 lines (65 loc) · 1.45 KB
/
cols.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
package rows
import (
"go/ast"
"reflect"
"strings"
"sync"
)
var sm = sync.Map{}
// colAdjust Adjust col
func colAdjust(tt reflect.Type, key []string, fn func(reflect.StructField) string) [][]string {
pn := tt.PkgPath() + "." + tt.Name()
v, b := sm.Load(pn)
if b {
if ss, ok := v.([][]string); ok && ss != nil && len(key) == len(ss) {
return ss
}
}
m := map[string]int{}
for i, v := range key {
m[v] = i
}
rk := make([][]string, len(key))
colAdjustMap(tt, key, nil, m, rk, fn)
sm.Store(pn, rk)
return rk
}
// colAdjustMap Adjust col map
func colAdjustMap(tt reflect.Type, key []string, prefix []string, m map[string]int, rk [][]string,
ff func(reflect.StructField) string) bool {
b := false
nf := tt.NumField()
for i := 0; i != nf; i++ {
fi := tt.Field(i)
tv := ff(fi)
if tv == "" {
continue
}
if fi.Anonymous && fi.Type.Kind() == reflect.Struct &&
colAdjustMap(fi.Type, key, append(prefix, fi.Name), m, rk, ff) {
continue
}
if !ast.IsExported(fi.Name) {
continue
}
if k, ok := m[tv]; ok && len(rk[k]) == 0 {
rk[k] = append(prefix, fi.Name)
b = true
}
}
return b
}
// MakeFieldName returns a function that gets the value of a field
func MakeFieldName(tag string) func(fn reflect.StructField) string {
return func(fn reflect.StructField) string {
b := fn.Tag.Get(tag)
dd := strings.Split(b, ",")[0]
if dd == "-" {
return ""
}
if dd == "" {
return strings.ToLower(fn.Name)
}
return dd
}
}