-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreplace.go
56 lines (46 loc) · 1.45 KB
/
replace.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
package sortedmap
import "errors"
func (sm *SortedMap) replace(key, val interface{}) {
sm.delete(key)
sm.insert(key, val)
}
// Replace uses the provided 'less than' function to insert sort.
// Even if the key already exists, the value will be inserted.
// Use Insert for the alternative functionality.
func (sm *SortedMap) Replace(key, val interface{}) {
sm.replace(key, val)
}
// BatchReplace adds all given records to the collection.
// Even if a key already exists, the value will be inserted.
// Use BatchInsert for the alternative functionality.
func (sm *SortedMap) BatchReplace(recs []Record) {
for _, rec := range recs {
sm.replace(rec.Key, rec.Val)
}
}
func (sm *SortedMap) batchReplaceMapInterfaceKeys(m map[interface{}]interface{}) {
for key, val := range m {
sm.replace(key, val)
}
}
func (sm *SortedMap) batchReplaceMapStringKeys(m map[string]interface{}) {
for key, val := range m {
sm.replace(key, val)
}
}
// BatchReplaceMap adds all map keys and values to the collection.
// Even if a key already exists, the value will be inserted.
// Use BatchInsertMap for the alternative functionality.
func (sm *SortedMap) BatchReplaceMap(v interface{}) error {
const unsupportedTypeErr = "Unsupported type."
switch m := v.(type) {
case map[interface{}]interface{}:
sm.batchReplaceMapInterfaceKeys(m)
return nil
case map[string]interface{}:
sm.batchReplaceMapStringKeys(m)
return nil
default:
return errors.New(unsupportedTypeErr)
}
}