forked from jianfengye/collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj_collection.go
208 lines (173 loc) · 4.59 KB
/
obj_collection.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
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
package collection
import (
"encoding/json"
"errors"
"fmt"
"reflect"
)
// ObjCollection 代表数组集合
type ObjCollection struct {
AbsCollection
objs reflect.Value // 数组对象,是一个slice
typ reflect.Type // 数组对象每个元素类型
}
// NewObjCollection 根据对象数组创建
func NewObjCollection(objs interface{}) *ObjCollection {
vals := reflect.ValueOf(objs)
typ := reflect.TypeOf(objs).Elem()
arr := &ObjCollection{
objs: vals,
typ: typ,
}
arr.AbsCollection.Parent = arr
return arr
}
// NewObjCollectionByType 根据类型创建一个空的数组, 传进来的Type是slice类型
func NewObjCollectionByType(typ reflect.Type) *ObjCollection {
vals := reflect.MakeSlice(typ, 0, 0)
eleTyp := typ.Elem()
arr := &ObjCollection{
objs: vals,
typ: eleTyp,
}
arr.AbsCollection.Parent = arr
return arr
}
// Copy 复制到新的数组
func (arr *ObjCollection) Copy() ICollection {
objs2 := reflect.MakeSlice(arr.objs.Type(), arr.objs.Len(), arr.objs.Len())
reflect.Copy(objs2, arr.objs)
arr.objs = objs2
return arr
}
func (arr *ObjCollection) Insert(index int, obj interface{}) ICollection {
if arr.Err() != nil {
return arr
}
ret := arr.objs.Slice(0, index)
length := arr.objs.Len()
tail := arr.objs.Slice(index, length)
tailNew := reflect.MakeSlice(arr.objs.Type(), length - index, length - index)
reflect.Copy(tailNew, tail)
ret = reflect.Append(ret, reflect.ValueOf(obj))
for i := 0; i < tail.Len(); i++ {
ret = reflect.Append(ret, tailNew.Index(i))
}
arr.objs = ret
arr.AbsCollection.Parent = arr
return arr
}
func (arr *ObjCollection) Index(i int) IMix {
return NewMix(arr.objs.Index(i).Interface()).SetCompare(arr.compare)
}
func (arr *ObjCollection) SetIndex(i int, val interface{}) ICollection {
arr.objs.Index(i).Set(reflect.ValueOf(val))
return arr
}
func (arr *ObjCollection) NewEmpty(err ...error) ICollection {
objs := reflect.MakeSlice(arr.objs.Type(), 0, 0)
ret := &ObjCollection{
objs: objs,
typ: arr.typ,
}
ret.AbsCollection.Parent = ret
if len(err) != 0 {
ret.SetErr(err[0])
}
return ret
}
func (arr *ObjCollection) Remove(i int) ICollection {
if arr.Err() != nil {
return arr
}
len := arr.Count()
if i >= len {
return arr.SetErr(errors.New("index exceeded"))
}
ret := arr.objs.Slice(0, i)
length := arr.objs.Len()
tail := arr.objs.Slice(i+1, length)
for i := 0; i < tail.Len(); i++ {
ret = reflect.Append(ret, tail.Index(i))
}
arr.objs = ret
arr.AbsCollection.Parent = arr
return arr
}
func (arr *ObjCollection) Count() int {
return arr.objs.Len()
}
func (arr *ObjCollection) DD() {
ret := fmt.Sprintf("ObjCollection(%d)(%s):{\n", arr.Count(), arr.typ.String())
for i := 0; i < arr.objs.Len(); i++ {
ret = ret + fmt.Sprintf("\t%d:\t%+v\n", i, arr.objs.Index(i))
}
ret = ret + "}\n"
fmt.Print(ret)
}
// 将对象的某个key作为Slice的value,作为slice返回
func (arr *ObjCollection) Pluck(key string) ICollection {
if arr.Err() != nil {
return arr
}
var objs ICollection
field, found := arr.typ.FieldByName(key)
if !found {
err := errors.New("ObjCollection.Pluck:key not found")
arr.SetErr(err)
return arr
}
objs = NewMixCollection(field.Type)
for i := 0; i < arr.objs.Len(); i++ {
v := arr.objs.Index(i).FieldByName(key).Interface()
objs.Append(v)
}
return objs
}
// 按照某个字段进行排序
func (arr *ObjCollection) SortBy(key string) ICollection {
if arr.Err() != nil {
return arr
}
compare := func(a interface{}, b interface{}) int {
mixA := NewMix(reflect.ValueOf(a).FieldByName(key).Interface())
mixB := NewMix(reflect.ValueOf(b).FieldByName(key).Interface())
ret, _ := mixA.Compare(mixB)
return ret
}
oldCompare := arr.compare
arr.compare = compare
newArr := arr.Sort()
newArr.SetCompare(oldCompare)
return newArr
}
// 按照某个字段进行排序,倒序
func (arr *ObjCollection) SortByDesc(key string) ICollection {
if arr.Err() != nil {
return arr
}
compare := func(a interface{}, b interface{}) int {
mixA := NewMix(reflect.ValueOf(a).FieldByName(key).Interface())
mixB := NewMix(reflect.ValueOf(b).FieldByName(key).Interface())
ret, _ := mixB.Compare(mixA)
return ret
}
oldCompare := arr.compare
arr.compare = compare
newArr := arr.Sort()
newArr.SetCompare(oldCompare)
return newArr
}
func (arr *ObjCollection) ToJson() ([]byte, error) {
return json.Marshal(arr.objs.Interface())
}
func (arr *ObjCollection) FromJson(data []byte) error {
vals := reflect.MakeSlice(reflect.SliceOf(arr.typ), 0, 0)
objs := vals.Interface()
err := json.Unmarshal(data, &objs)
if err != nil {
return err
}
arr.objs = reflect.ValueOf(objs)
return nil
}