forked from jianfengye/collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_point_collection.go
244 lines (204 loc) · 5.51 KB
/
obj_point_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
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
package collection
import (
"encoding/json"
"fmt"
"reflect"
"github.com/pkg/errors"
)
// ObjCollection 代表数组集合
type ObjPointCollection struct {
AbsCollection
objs reflect.Value // 数组对象,是一个slice
typ reflect.Type // 数组对象每个元素类型
}
// NewObjCollection 根据对象数组创建
func NewObjPointCollection(objs interface{}) *ObjPointCollection {
vals := reflect.ValueOf(objs)
typ := reflect.TypeOf(objs).Elem()
arr := &ObjPointCollection{
objs: vals,
typ: typ,
}
arr.AbsCollection.Parent = arr
arr.AbsCollection.eleType = TYPE_OBJ_POINT
return arr
}
// Copy 复制到新的数组
func (arr *ObjPointCollection) Copy() ICollection {
objs2 := reflect.MakeSlice(arr.objs.Type(), arr.objs.Len(), arr.objs.Len())
reflect.Copy(objs2, arr.objs)
arr2 := &ObjPointCollection{
objs: objs2,
typ: arr.objs.Type(),
}
if arr.Err() != nil {
arr2.SetErr(arr.Err())
}
if arr.compare != nil {
arr2.compare = arr.compare
}
arr2.Parent = arr2
arr2.eleType = arr.eleType
return arr2
}
func (arr *ObjPointCollection) 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 *ObjPointCollection) Index(i int) IMix {
return NewMix(arr.objs.Index(i).Interface()).SetCompare(arr.compare)
}
func (arr *ObjPointCollection) SetIndex(i int, val interface{}) ICollection {
arr.objs.Index(i).Set(reflect.ValueOf(val))
return arr
}
func (arr *ObjPointCollection) NewEmpty(err ...error) ICollection {
objs2 := reflect.MakeSlice(arr.objs.Type(), 0, 0)
arr2 := &ObjPointCollection{
objs: objs2,
typ: arr.objs.Type(),
}
arr2.compare = arr.compare
arr2.eleType = arr.eleType
arr2.Parent = arr2
return arr2
}
func (arr *ObjPointCollection) 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 *ObjPointCollection) Count() int {
return arr.objs.Len()
}
func (arr *ObjPointCollection) DD() {
if arr.Err() != nil {
fmt.Println(arr.Err().Error())
}
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 *ObjPointCollection) Pluck(key string) ICollection {
if arr.Err() != nil {
return arr
}
var objs ICollection
field, found := arr.typ.Elem().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).Elem().FieldByName(key).Interface()
objs.Append(v)
}
return objs
}
// 按照某个字段进行排序
func (arr *ObjPointCollection) SortBy(key string) ICollection {
if arr.Err() != nil {
return arr
}
compare := func(a interface{}, b interface{}) int {
mixA := NewMix(reflect.ValueOf(a).Elem().FieldByName(key).Interface())
mixB := NewMix(reflect.ValueOf(b).Elem().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 *ObjPointCollection) SortByDesc(key string) ICollection {
if arr.Err() != nil {
return arr
}
compare := func(a interface{}, b interface{}) int {
mixA := NewMix(reflect.ValueOf(a).Elem().FieldByName(key).Interface())
mixB := NewMix(reflect.ValueOf(b).Elem().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 *ObjPointCollection) ToJson() ([]byte, error) {
return json.Marshal(arr.objs.Interface())
}
func (arr *ObjPointCollection) 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
}
func (arr *ObjPointCollection) ToObjs(objs interface{}) error {
arr.mustNotBeBaseType()
objVal := reflect.ValueOf(objs)
if objVal.Elem().CanSet() {
objVal.Elem().Set(arr.objs)
return nil
}
return errors.New("element should be can set")
}
// KeyByStrField 按照某个字段进行排序,倒序
func (arr *ObjPointCollection) KeyByStrField(key string) (map[string]interface{}, error) {
if arr.Err() != nil {
return nil, arr.Err()
}
ret := make(map[string]interface{})
if arr.Count() == 0 {
return ret, nil
}
for i := 0; i < arr.Count(); i++ {
o, err := arr.Index(i).ToInterface()
if err != nil {
return nil, errors.New("key by string field error: " + err.Error())
}
strVal := reflect.ValueOf(o).Elem().FieldByName(key).String()
ret[strVal] = o
}
return ret, nil
}