-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcol.go
121 lines (91 loc) · 1.97 KB
/
col.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package col
import (
"fmt"
"reflect"
"sort"
)
type field struct {
Index []int
Name string
}
type fields []field
func (s fields) Len() int {
return len(s)
}
func (s fields) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s fields) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
func walkFields(fs fields, v reflect.Value, index []int) fields {
t := v.Type()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
fieldValue := v.FieldByIndex(f.Index)
index := append(index, i)
if f.Anonymous {
fs = walkFields(fs, fieldValue, index)
} else {
name := f.Tag.Get("col")
if name == "" {
name = f.Name
}
fs = append(fs, field{index, name})
}
}
return fs
}
type Index [][]int
type column struct {
Position int
Name string
}
type columns []column
func (s columns) Len() int {
return len(s)
}
func (s columns) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s columns) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
func NewIndex(typ interface{}, names ...string) Index {
if len(names) == 0 {
return make(Index, 0)
}
cols := make(columns, 0, len(names))
for i, n := range names {
cols = append(cols, column{i, n})
}
index := make(Index, len(names))
v := reflect.ValueOf(typ)
fs := walkFields(make(fields, 0, v.NumField()), v, []int{})
sort.Sort(fs)
sort.Sort(cols)
for _, f := range fs {
if col := cols[0]; f.Name == col.Name {
index[col.Position] = f.Index
// try to match next column
cols = cols[1:]
}
if len(cols) == 0 {
// all done
return index
}
}
panic(fmt.Sprintf("can't find field %s", cols[0].Name))
}
func (index Index) Values(src interface{}) []interface{} {
v := reflect.ValueOf(src)
res := make([]interface{}, 0, len(index))
for _, fieldIndex := range index {
fieldValue := v.FieldByIndex(fieldIndex)
res = append(res, fieldValue.Interface())
}
return res
}
func Values(src interface{}, names ...string) []interface{} {
return NewIndex(src, names...).Values(src)
}