OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / stretchr / testify / vendor / github.com / stretchr / objx / mutations.go
1 package objx
2
3 // Exclude returns a new Map with the keys in the specified []string
4 // excluded.
5 func (d Map) Exclude(exclude []string) Map {
6
7         excluded := make(Map)
8         for k, v := range d {
9                 var shouldInclude bool = true
10                 for _, toExclude := range exclude {
11                         if k == toExclude {
12                                 shouldInclude = false
13                                 break
14                         }
15                 }
16                 if shouldInclude {
17                         excluded[k] = v
18                 }
19         }
20
21         return excluded
22 }
23
24 // Copy creates a shallow copy of the Obj.
25 func (m Map) Copy() Map {
26         copied := make(map[string]interface{})
27         for k, v := range m {
28                 copied[k] = v
29         }
30         return New(copied)
31 }
32
33 // Merge blends the specified map with a copy of this map and returns the result.
34 //
35 // Keys that appear in both will be selected from the specified map.
36 // This method requires that the wrapped object be a map[string]interface{}
37 func (m Map) Merge(merge Map) Map {
38         return m.Copy().MergeHere(merge)
39 }
40
41 // Merge blends the specified map with this map and returns the current map.
42 //
43 // Keys that appear in both will be selected from the specified map.  The original map
44 // will be modified. This method requires that
45 // the wrapped object be a map[string]interface{}
46 func (m Map) MergeHere(merge Map) Map {
47
48         for k, v := range merge {
49                 m[k] = v
50         }
51
52         return m
53
54 }
55
56 // Transform builds a new Obj giving the transformer a chance
57 // to change the keys and values as it goes. This method requires that
58 // the wrapped object be a map[string]interface{}
59 func (m Map) Transform(transformer func(key string, value interface{}) (string, interface{})) Map {
60         newMap := make(map[string]interface{})
61         for k, v := range m {
62                 modifiedKey, modifiedVal := transformer(k, v)
63                 newMap[modifiedKey] = modifiedVal
64         }
65         return New(newMap)
66 }
67
68 // TransformKeys builds a new map using the specified key mapping.
69 //
70 // Unspecified keys will be unaltered.
71 // This method requires that the wrapped object be a map[string]interface{}
72 func (m Map) TransformKeys(mapping map[string]string) Map {
73         return m.Transform(func(key string, value interface{}) (string, interface{}) {
74
75                 if newKey, ok := mapping[key]; ok {
76                         return newKey, value
77                 }
78
79                 return key, value
80         })
81 }