forked from thoas/go-funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.go
67 lines (55 loc) · 1.39 KB
/
union.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
package funk
import (
"reflect"
)
// Union returns the union between two collections.
func Union(collections ...interface{}) interface{} {
// shortcut zero/single argument
if len(collections) == 0 {
return nil
} else if len(collections) == 1 {
return collections[0]
}
if !IsIteratee(collections[0]) {
panic("Parameter must be a collection")
}
cType := reflect.TypeOf(collections[0])
zLen := 0
for i, x := range collections {
xValue := reflect.ValueOf(x)
xType := xValue.Type()
if i > 0 && NotEqual(cType, xType) {
panic("Parameters must have the same type")
}
zLen += xValue.Len()
}
if cType.Kind() == reflect.Map {
zType := reflect.MapOf(cType.Key(), cType.Elem())
zMap := reflect.MakeMap(zType)
for _, x := range collections {
xIter := reflect.ValueOf(x).MapRange()
for xIter.Next() {
zMap.SetMapIndex(xIter.Key(), xIter.Value())
}
}
return zMap.Interface()
} else {
zType := reflect.SliceOf(cType.Elem())
zSlice := reflect.MakeSlice(zType, 0, 0)
for _, x := range collections {
xValue := reflect.ValueOf(x)
zSlice = reflect.AppendSlice(zSlice, xValue)
}
return zSlice.Interface()
}
}
// UnionStringMap returns the union between multiple string maps
func UnionStringMap(x ...map[string]string) map[string]string {
zMap := map[string]string{}
for _, xMap := range x {
for k, v := range xMap {
zMap[k] = v
}
}
return zMap
}