forked from chenhg5/collection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmuti_dimensional_array_collection.go
88 lines (73 loc) · 1.98 KB
/
muti_dimensional_array_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
package collection
import "encoding/json"
type MultiDimensionalArrayCollection struct {
value [][]interface{}
BaseCollection
}
func (c MultiDimensionalArrayCollection) Value() interface{} {
return c.value
}
func (c MultiDimensionalArrayCollection) ToMultiDimensionalArray() [][]interface{} {
return c.value
}
func (c MultiDimensionalArrayCollection) ToMultiDimensionalArrayE() ([][]interface{}, error) {
return c.value, c.err
}
// Collapse collapses a collection of arrays into a single, flat collection.
func (c MultiDimensionalArrayCollection) Collapse() Collection {
if len(c.value[0]) == 0 {
return Collect([]interface{}{})
}
length := 0
for _, v := range c.value {
length += len(v)
}
d := make([]interface{}, length)
index := 0
for _, innerSlice := range c.value {
for _, v := range innerSlice {
d[index] = v
index++
}
}
return Collect(d)
}
// Concat appends the given array or collection values onto the end of the collection.
func (c MultiDimensionalArrayCollection) Concat(value interface{}) Collection {
return MultiDimensionalArrayCollection{
value: append(c.value, value.([][]interface{})...),
BaseCollection: BaseCollection{length: c.length + len(value.([][]interface{}))},
}
}
// Dd dumps the collection's items and ends execution of the script.
func (c MultiDimensionalArrayCollection) Dd() {
dd(c)
}
func (c MultiDimensionalArrayCollection) DdE() error {
dd(c)
return c.err
}
// Dump dumps the collection's items.
func (c MultiDimensionalArrayCollection) Dump() {
dump(c)
}
func (c MultiDimensionalArrayCollection) DumpE() error {
dump(c)
return c.err
}
// ToJson converts the collection into a json string.
func (c MultiDimensionalArrayCollection) ToJson() string {
s, err := json.Marshal(c.value)
if err != nil {
return ""
}
return string(s)
}
func (c MultiDimensionalArrayCollection) ToJsonE() (string, error) {
s, err := json.Marshal(c.value)
if err != nil {
c.errorHandle(err.Error())
return "", err
}
return string(s), c.err
}