-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.go
More file actions
338 lines (305 loc) · 6.83 KB
/
collection.go
File metadata and controls
338 lines (305 loc) · 6.83 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package collection
import (
"fmt"
"reflect"
"sort"
"strconv"
"sync"
)
var collection *Collection
type Collection struct {
data interface{}
field string
uniqueFlag bool
}
func NewWithValue(data interface{}) *Collection {
s := New()
s.Value(data)
return s
}
// New().Value(data).Field("id").IntSlice() // []int{1,2}
// New().Value([]*User)
// New().Value([]User)
func New() *Collection {
if collection == nil { // 单例模式
return &Collection{} // 默认unique = false
}
collection.Init()
return collection
}
func (s *Collection) Init() {
s.data = nil
s.field = ""
s.uniqueFlag = false
return
}
func (s *Collection) Value(data interface{}) *Collection {
vk := reflect.ValueOf(data).Kind()
if vk != reflect.Slice && vk != reflect.Map {
panic("must Slice or Map")
}
s.uniqueFlag = false // 每次重新设置值时, 默认不进行去重
s.data = data
return s
}
func (s *Collection) Field(field string) *Collection {
s.field = field
return s
}
// 去重
func (s *Collection) Unique() *Collection {
s.uniqueFlag = true
return s
}
func (s *Collection) Data() interface{} {
return s.data
}
// 返回根据Field取到的元素数组
func (s *Collection) Slice() []interface{} {
return s.buildSlice()
}
func (s *Collection) getValue(v reflect.Value) interface{} {
return getValueByKey(v, s.field)
}
// 类型转换
func (s *Collection) IntSlice() (ret []int) {
slice := s.buildSlice()
ret = make([]int, 0, len(slice))
for _, v := range slice {
if vint, err := interface2Int(v); err == nil {
ret = append(ret, vint)
}
}
if s.uniqueFlag {
ret = SliceIntUnique(ret)
}
return
}
func (s *Collection) StringSlice() (ret []string) {
slice := s.buildSlice()
ret = make([]string, 0, len(slice))
for _, v := range slice {
vstr := interface2String(v)
ret = append(ret, vstr)
}
if s.uniqueFlag {
ret = SliceStringUnique(ret)
}
return
}
// data_trun_key();
func (s *Collection) IntMapSlice() (ret map[int][]interface{}) {
mapdata := s.dataTrunMulti()
ret = make(map[int][]interface{}, len(mapdata))
for k, items := range mapdata {
if vint, err := interface2Int(k); err == nil {
ret[vint] = items
}
}
return
}
func interface2Int(v interface{}) (int, error) {
switch val := v.(type) {
case int:
return val, nil
case string:
return strconv.Atoi(val)
}
return 0, fmt.Errorf("can not ParseInt success")
}
// data_trun_key();
func (s *Collection) StringMapSlice() (ret map[string][]interface{}) {
mapdata := s.dataTrunMulti()
ret = make(map[string][]interface{}, len(mapdata))
for k, items := range mapdata {
ret[interface2String(k)] = items
}
return
}
func interface2String(v interface{}) string {
var key string
switch val := v.(type) {
case int:
key = strconv.Itoa(val)
case string:
key = val
default:
key = fmt.Sprintf("%v", val)
}
return key
}
// data_trun_key();
func (s *Collection) IntMap() (ret map[int]interface{}) {
mapdata := s.dataTrunValue()
ret = make(map[int]interface{}, len(mapdata))
for k, items := range mapdata {
if vint, err := interface2Int(k); err == nil {
ret[vint] = items
}
}
return
}
// data_trun_key();
func (s *Collection) StringMap() (ret map[string]interface{}) {
mapdata := s.dataTrunValue()
ret = make(map[string]interface{}, len(mapdata))
for k, items := range mapdata {
ret[interface2String(k)] = items
}
return
}
// 仅用于定义类型
// valKey 为Field("field") 取到的对应item的value值
// itemInterfaceValue 为对应item
type F func(valKey interface{}, itemInterfaceValue interface{})
func (s *Collection) commonDataTrun(f F) {
v := reflect.ValueOf(s.data)
vk := v.Kind()
l := v.Len()
if l == 0 {
return
}
switch vk {
case reflect.Slice:
for i := 0; i < l; i++ {
item := v.Index(i)
if item.CanInterface() {
valKey := s.getValue(item)
f(valKey, item.Interface()) // 利用map是指针传递这个特性
}
}
case reflect.Map:
mapkeys := v.MapKeys()
for _, idx := range mapkeys {
item := v.MapIndex(idx)
if item.CanInterface() {
valKey := s.getValue(item)
f(valKey, item.Interface())
}
}
}
return
}
func (s *Collection) dataTrunValue() (res map[interface{}]interface{}) {
lock := new(sync.Mutex)
res = make(map[interface{}]interface{}, 10)
var f F = func(valKey interface{}, itemInterfaceValue interface{}) {
if valKey == nil {
return
}
lock.Lock()
res[valKey] = itemInterfaceValue
lock.Unlock()
}
s.commonDataTrun(f)
return
}
func (s *Collection) dataTrunMulti() (res map[interface{}][]interface{}) {
lock := new(sync.Mutex)
res = make(map[interface{}][]interface{}, 10)
var f F = func(valKey interface{}, itemInterfaceValue interface{}) {
if valKey == nil {
return
}
lock.Lock()
_, ok := res[valKey]
if !ok {
res[valKey] = make([]interface{}, 0, 10) //减少分配内存
}
res[valKey] = append(res[valKey], itemInterfaceValue)
lock.Unlock()
}
s.commonDataTrun(f)
return
}
//commonBuild
func (s *Collection) buildSlice() (ret []interface{}) {
v := reflect.ValueOf(s.data)
vk := v.Kind()
l := v.Len()
if l == 0 {
return
}
ret = make([]interface{}, 0, l)
switch vk {
case reflect.Slice:
for i := 0; i < l; i++ {
item := v.Index(i)
val := s.getValue(item)
if val != nil {
ret = append(ret, val)
}
}
case reflect.Map:
mapkeys := v.MapKeys()
for _, idx := range mapkeys {
item := v.MapIndex(idx)
val := s.getValue(item)
if val != nil {
ret = append(ret, val)
}
}
}
return
}
// New().Value(data).Field("id").IntSlice() // []int{1,2}
// New().Value([]*User)
// New().Value([]User)
// New().Value([]string).Field("xxx");
func getValueByKey(item reflect.Value, key string) interface{} {
k := item.Kind()
if k == reflect.Ptr {
if item.IsNil() {
return nil // 取不到, 就不取了
}
item = item.Elem() //转换指针为结构
k = item.Kind()
}
if k != reflect.Struct {
return nil //不是结构体, 找不到
// panic(fmt.Errorf("must be struct , you input type is : %s ", k.String()))
}
_, ok := item.Type().FieldByName(key)
if !ok {
return nil
}
if !item.FieldByName(key).CanInterface() {
return nil
}
return item.FieldByName(key).Interface()
}
// 判断传入的元素是否是slice
// 支持检测 []string, []int , []int64 , []in32 , []interface{} , []float64 , []uint32 , []uint64 , ...
func IsSlice(v interface{}) bool {
return reflect.ValueOf(v).Type().Kind().String() == "slice"
}
func SliceIntUnique(slice []int) []int {
sort.Ints(slice)
i := 0
var j int
for {
if i >= len(slice)-1 {
break
}
for j = i + 1; j < len(slice) && slice[i] == slice[j]; j++ {
}
slice = append(slice[:i+1], slice[j:]...)
i++
}
return slice
}
func SliceStringUnique(slice []string) []string {
sort.Strings(slice)
i := 0
var j int
for {
if i >= len(slice)-1 {
break
}
for j = i + 1; j < len(slice) && slice[i] == slice[j]; j++ {
}
slice = append(slice[:i+1], slice[j:]...)
i++
}
return slice
}