-
Notifications
You must be signed in to change notification settings - Fork 10
/
slice.go
197 lines (171 loc) · 4.28 KB
/
slice.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
package slice
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/2637309949/dolphin/platform/util/snaker"
)
// StrSliceContains check string exists in array
func StrSliceContains(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}
// CamelToSnake change array string to snaker
func CamelToSnake(ss []string) []string {
result := make([]string, 0)
for _, s := range ss {
result = append(result, snaker.CamelToSnake(s))
}
return result
}
// GetFieldSliceByName defined
func GetFieldSliceByName(arr interface{}, name string, format ...string) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
var (
ft = "%v"
)
if len(format) > 0 {
ft = format[0]
}
sm := []map[string]interface{}{}
bte, err := json.Marshal(arr)
if err != nil {
return nil
}
if err = json.Unmarshal(bte, &sm); err != nil || len(sm) == 0 || sm[0][name] == nil {
return nil
}
var (
arrElemType = reflect.TypeOf(sm[0][name])
resultSliceType = reflect.SliceOf(arrElemType)
resultSlice = reflect.MakeSlice(resultSliceType, 0, 0)
)
for i := 0; i < len(sm); i++ {
item := sm[i][name]
switch item.(type) {
case string:
item = fmt.Sprintf(ft, item)
}
iv := reflect.ValueOf(item)
resultSlice = reflect.Append(resultSlice, iv)
}
return resultSlice.Interface()
}
// PatchSliceByField defined
func PatchSliceByField(target interface{}, source interface{}, targetMatchKey string, sourceMatchKey string, key ...string) ([]map[string]interface{}, error) {
if !IsIteratee(target) {
panic("First parameter must be an iteratee")
}
if !IsIteratee(source) {
panic("Second parameter must be an iteratee")
}
tsm := []map[string]interface{}{}
bte, err := json.Marshal(target)
if err != nil {
return []map[string]interface{}{}, err
}
if err = json.Unmarshal(bte, &tsm); err != nil || len(tsm) == 0 {
return []map[string]interface{}{}, err
}
ssm := []map[string]interface{}{}
ste, err := json.Marshal(source)
if err != nil {
return []map[string]interface{}{}, err
}
if err = json.Unmarshal(ste, &ssm); err != nil {
return []map[string]interface{}{}, err
}
for i := range tsm {
for i2 := range ssm {
if tsm[i][targetMatchKey] == ssm[i2][sourceMatchKey] {
for i3 := range key {
keys := strings.Split(key[i3], "#")
if len(keys) > 1 {
tsm[i][keys[0]] = ssm[i2][keys[1]]
} else {
tsm[i][keys[0]] = ssm[i2][keys[0]]
}
}
}
}
}
return tsm, err
}
// IsIteratee returns if the argument is an iteratee.
func IsIteratee(in interface{}) bool {
if in == nil {
return false
}
arrType := reflect.TypeOf(in)
kind := arrType.Kind()
return kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map
}
// StringInSlice defined
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// RemoveStringDuplicates defined
func RemoveStringDuplicates(s []string) ([]string, []string) {
encountered := make(map[string]struct{})
result := make([]string, 0)
duplicate := make([]string, 0)
for _, v := range s {
if _, ok := encountered[v]; ok {
duplicate = append(duplicate, v)
continue
} else {
encountered[v] = struct{}{}
result = append(result, v)
}
}
return result, duplicate
}
func redirectValue(value reflect.Value) reflect.Value {
for {
if !value.IsValid() || value.Kind() != reflect.Ptr {
return value
}
res := reflect.Indirect(value)
// Test for a circular type.
if res.Kind() == reflect.Ptr && value.Pointer() == res.Pointer() {
return value
}
value = res
}
}
func sliceElem(rtype reflect.Type) reflect.Type {
for {
if rtype.Kind() != reflect.Slice && rtype.Kind() != reflect.Array {
return rtype
}
rtype = rtype.Elem()
}
}
// Chunk defined
func Chunk(slice interface{}, chunkSize int) interface{} {
arrValue := redirectValue(reflect.ValueOf(slice))
if !IsIteratee(arrValue.Interface()) {
panic("First parameter must be an iteratee")
}
accValue := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(slice)), 0, 0)
for i := 0; i < arrValue.Len(); i += chunkSize {
end := i + chunkSize
if end > arrValue.Len() {
end = arrValue.Len()
}
accValue = reflect.Append(accValue, arrValue.Slice(i, end))
}
return accValue.Interface()
}