-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringmap.go
63 lines (52 loc) · 1.67 KB
/
stringmap.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
package nkngomobile
import "errors"
// StringMapFunc is a wrapper type for gomobile compatibility.
type StringMapFunc interface{ OnVisit(string, string) bool }
// StringMap is a wrapper type for gomobile compatibility. StringMap is not
// protected by lock and should not be read and write at the same time.
type StringMap struct{ _map map[string]string }
// NewStringMap creates a StringMap from a map.
func NewStringMap(m map[string]string) *StringMap {
return &StringMap{m}
}
// NewStringMapWithSize creates an empty StringMap with a given size.
func NewStringMapWithSize(size int) *StringMap {
return &StringMap{make(map[string]string, size)}
}
func (sm *StringMap) Map() map[string]string {
if sm == nil {
return nil
}
return sm._map
}
// Get returns the value of a key, or ErrKeyNotInMap if key does not exist.
func (sm *StringMap) Get(key string) (string, error) {
if value, ok := sm._map[key]; ok {
return value, nil
}
return "", errors.New("key not in map")
}
// Set sets the value of a key to a value.
func (sm *StringMap) Set(key string, value string) {
sm._map[key] = value
}
// Delete deletes a key and its value from the map.
func (sm *StringMap) Delete(key string) {
delete(sm._map, key)
}
// Len returns the number of elements in the map.
func (sm *StringMap) Len() int {
return len(sm._map)
}
// Range iterates over the StringMap and call the OnVisit callback function with
// each element in the map. If the OnVisit function returns false, the iterator
// will stop and no longer visit the rest elements.
func (sm *StringMap) Range(cb StringMapFunc) {
if cb != nil {
for key, value := range sm._map {
if !cb.OnVisit(key, value) {
return
}
}
}
}