forked from chenhg5/collection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_collection.go
357 lines (307 loc) · 7.81 KB
/
map_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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package collection
import (
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
)
type MapCollection struct {
value map[string]interface{}
BaseCollection
}
// Only returns the items in the collection with the specified keys.
func (c MapCollection) Only(keys []string) Collection {
var (
d MapCollection
m = make(map[string]interface{}, 0)
)
for _, k := range keys {
m[k] = c.value[k]
}
d.value = m
d.length = len(m)
return d
}
// ToStruct turn the collection to the specified struct using mapstructure.
// https://github.com/mitchellh/mapstructure
func (c MapCollection) ToStruct(dist interface{}) {
if err := mapstructure.Decode(c.value, dist); err != nil {
dist = nil
}
}
func (c MapCollection) ToStructE(dist interface{}) error {
if err := mapstructure.Decode(c.value, dist); err != nil {
c.errorHandle(err.Error())
dist = nil
}
return c.err
}
// Select select the keys of collection and delete others.
func (c MapCollection) Select(keys ...string) Collection {
n := copyMap(c.value)
for key := range n {
exist := false
for i := 0; i < len(keys); i++ {
if keys[i] == key {
exist = true
break
}
}
if !exist {
delete(n, key)
}
}
return MapCollection{
value: n,
}
}
// Prepend adds an item to the beginning of the collection.
func (c MapCollection) Prepend(values ...interface{}) Collection {
var m = copyMap(c.value)
m[values[0].(string)] = values[1]
return MapCollection{m, BaseCollection{length: len(m)}}
}
// ToMap converts the collection into a plain golang map.
func (c MapCollection) ToMap() map[string]interface{} {
return c.value
}
func (c MapCollection) ToMapE() (map[string]interface{}, error) {
return c.value, c.err
}
// Contains determines whether the collection contains a given item.
func (c MapCollection) Contains(value ...interface{}) bool {
if callback, ok := value[0].(CB); ok {
for k, v := range c.value {
if callback(k, v) {
return true
}
}
return false
}
for _, v := range c.value {
if v == value[0] {
return true
}
}
return false
}
func (c MapCollection) ContainsE(value ...interface{}) (bool, error) {
return c.Contains(), c.err
}
// Dd dumps the collection's items and ends execution of the script.
func (c MapCollection) Dd() {
dd(c)
}
func (c MapCollection) DdE() error {
dd(c)
return c.err
}
// Dump dumps the collection's items.
func (c MapCollection) Dump() {
dump(c)
}
func (c MapCollection) DumpE() error {
dump(c)
return c.err
}
// DiffAssoc compares the collection against another collection or a plain PHP array based on its keys and values.
// This method will return the key / value pairs in the original collection that are not present in the given collection.
func (c MapCollection) DiffAssoc(m map[string]interface{}) Collection {
var d = make(map[string]interface{}, 0)
for key, value := range m {
if v, ok := c.value[key]; ok {
if v != value {
d[key] = value
}
}
}
return MapCollection{
value: d,
}
}
// DiffKeys compares the collection against another collection or a plain PHP array based on its keys.
// This method will return the key / value pairs in the original collection that are not present in the given collection.
func (c MapCollection) DiffKeys(m map[string]interface{}) Collection {
var d = make(map[string]interface{}, 0)
for key, value := range c.value {
if _, ok := m[key]; !ok {
d[key] = value
}
}
return MapCollection{
value: d,
}
}
// Each iterates over the items in the collection and passes each item to a callback.
func (c MapCollection) Each(cb func(item, value interface{}) (interface{}, bool)) Collection {
var d = make(map[string]interface{}, 0)
var (
newValue interface{}
stop = false
)
for key, value := range c.value {
if !stop {
newValue, stop = cb(key, value)
d[key] = newValue
} else {
d[key] = value
}
}
return MapCollection{
value: d,
}
}
// Every may be used to verify that all elements of a collection pass a given truth test.
func (c MapCollection) Every(cb CB) bool {
for key, value := range c.value {
if !cb(key, value) {
return false
}
}
return true
}
func (c MapCollection) EveryE(cb CB) (bool, error) {
return c.Every(cb), c.err
}
// Except returns all items in the collection except for those with the specified keys.
func (c MapCollection) Except(keys []string) Collection {
var d = copyMap(c.value)
for _, key := range keys {
delete(d, key)
}
return MapCollection{
value: d,
}
}
// FlatMap iterates through the collection and passes each value to the given callback.
func (c MapCollection) FlatMap(cb func(value interface{}) interface{}) Collection {
var d = make(map[string]interface{}, 0)
for key, value := range c.value {
d[key] = cb(value)
}
return MapCollection{
value: d,
}
}
// Flip swaps the collection's keys with their corresponding values.
func (c MapCollection) Flip() Collection {
var d = make(map[string]interface{}, 0)
for key, value := range c.value {
d[fmt.Sprintf("%v", value)] = key
}
return MapCollection{
value: d,
}
}
// Forget removes an item from the collection by its key.
func (c MapCollection) Forget(k string) Collection {
var d = copyMap(c.value)
for key := range c.value {
if key == k {
delete(d, key)
}
}
return MapCollection{
value: d,
}
}
// Get returns the item at a given key. If the key does not exist, null is returned.
func (c MapCollection) Get(k string, v ...interface{}) interface{} {
if len(v) > 0 {
if value, ok := c.value[k]; ok {
return value
} else {
return v[0]
}
} else {
return c.value[k]
}
}
func (c MapCollection) GetE(k string, v ...interface{}) (interface{}, error) {
return c.Get(k, v...), c.err
}
// Has determines if a given key exists in the collection.
func (c MapCollection) Has(keys ...string) bool {
for _, key := range keys {
exist := false
for kk := range c.value {
if key == kk {
exist = true
break
}
}
if !exist {
return false
}
}
return true
}
func (c MapCollection) HasE(keys ...string) (bool, error) {
return c.Has(), c.err
}
// IntersectByKeys removes any keys from the original collection that are not present in the given array or collection.
func (c MapCollection) IntersectByKeys(m map[string]interface{}) Collection {
var d = make(map[string]interface{}, 0)
for key, value := range c.value {
for kk := range m {
if kk == key {
d[kk] = value
}
}
}
return MapCollection{
value: d,
}
}
// IsEmpty returns true if the collection is empty; otherwise, false is returned.
func (c MapCollection) IsEmpty() bool {
return len(c.value) == 0
}
func (c MapCollection) IsEmptyE() (bool, error) {
return c.IsEmpty(), c.err
}
// IsNotEmpty returns true if the collection is not empty; otherwise, false is returned.
func (c MapCollection) IsNotEmpty() bool {
return len(c.value) != 0
}
func (c MapCollection) IsNotEmptyE() (bool, error) {
return c.IsNotEmpty(), c.err
}
// Keys returns all of the collection's keys.
func (c MapCollection) Keys() Collection {
var d = make([]string, 0)
for key := range c.value {
d = append(d, key)
}
return StringArrayCollection{
value: d,
}
}
// Merge merges the given array or collection with the original collection. If a string key in the given items
// matches a string key in the original collection, the given items's value will overwrite the value in the
// original collection.
func (c MapCollection) Merge(i interface{}) Collection {
m := i.(map[string]interface{})
var d = copyMap(c.value)
for key, value := range m {
d[key] = value
}
return MapCollection{
value: d,
}
}
// ToJson converts the collection into a json string.
func (c MapCollection) ToJson() string {
s, err := json.Marshal(c.value)
if err != nil {
return ""
}
return string(s)
}
func (c MapCollection) ToJsonE() (string, error) {
s, err := json.Marshal(c.value)
if err != nil {
c.errorHandle(err.Error())
return "", c.err
}
return string(s), c.err
}