github.com/jxskiss/gopkg@v0.17.3/gemap/map.go (about) 1 package gemap 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strconv" 7 "sync" 8 "time" 9 ) 10 11 // Map is a map of string key and interface{} value. 12 // It provides many useful methods to manipulate map[string]interface{}. 13 type Map map[string]interface{} 14 15 // SafeMap wraps a RWMutex with Map to provide concurrent safety. 16 type SafeMap struct { 17 sync.RWMutex 18 Map Map 19 } 20 21 // NewMap returns a new initialized Map instance. 22 func NewMap() Map { 23 return make(Map) 24 } 25 26 // NewSafeMap returns a new initialized SafeMap instance. 27 func NewSafeMap() *SafeMap { 28 return &SafeMap{Map: make(Map)} 29 } 30 31 // Set is used to store a new key/value pair exclusively in the map. 32 // It also lazily initializes the map if it was not used previously. 33 func (p *Map) Set(key string, value interface{}) { 34 if *p == nil { 35 *p = make(Map) 36 } 37 (*p)[key] = value 38 } 39 40 // Get returns the value for the given key, ie: (value, true). 41 // If the value does not exist it returns (nil, false) 42 func (p Map) Get(key string) (value interface{}, exists bool) { 43 value, exists = p[key] 44 return 45 } 46 47 // MustGet returns the value for the given key if it exists, otherwise it panics. 48 func (p Map) MustGet(key string) interface{} { 49 if val, ok := p[key]; ok { 50 return val 51 } 52 panic(fmt.Sprintf("key %q not exists", key)) 53 } 54 55 // GetString returns the value associated with the key as a string. 56 func (p Map) GetString(key string) string { 57 v := p[key] 58 if val, ok := v.(string); ok { 59 return val 60 } 61 if val, ok := v.([]byte); ok { 62 return string(val) 63 } 64 return "" 65 } 66 67 // GetBytes returns the value associated with the key as bytes. 68 func (p Map) GetBytes(key string) []byte { 69 v := p[key] 70 if val, ok := v.([]byte); ok { 71 return val 72 } 73 if val, ok := v.(string); ok { 74 return []byte(val) 75 } 76 return nil 77 } 78 79 // GetBool returns the value associated with the key as a boolean value. 80 func (p Map) GetBool(key string) bool { 81 val, _ := p[key].(bool) 82 return val 83 } 84 85 // GetInt returns the value associated with the key as an integer. 86 func (p Map) GetInt(key string) int { 87 val, ok := p[key] 88 if ok { 89 switch v := val.(type) { 90 case int: 91 return v 92 case json.Number: 93 num, _ := v.Int64() 94 return int(num) 95 case string: 96 num, _ := strconv.ParseInt(v, 10, 64) 97 return int(num) 98 } 99 } 100 return 0 101 } 102 103 // GetInt64 returns the value associated with the key as an int64. 104 func (p Map) GetInt64(key string) int64 { 105 val, ok := p[key] 106 if ok { 107 switch v := val.(type) { 108 case int64: 109 return v 110 case json.Number: 111 num, _ := v.Int64() 112 return num 113 case string: 114 num, _ := strconv.ParseInt(v, 10, 64) 115 return num 116 } 117 } 118 return 0 119 } 120 121 // GetInt32 returns the value associated with the key as an int32. 122 func (p Map) GetInt32(key string) int32 { 123 val, ok := p[key] 124 if ok { 125 switch v := val.(type) { 126 case int32: 127 return v 128 case json.Number: 129 num, _ := v.Int64() 130 return int32(num) 131 case string: 132 num, _ := strconv.ParseInt(v, 10, 64) 133 return int32(num) 134 } 135 } 136 return 0 137 } 138 139 // GetFloat64 returns the value associated with the key as a float64. 140 func (p Map) GetFloat64(key string) float64 { 141 val, ok := p[key] 142 if ok { 143 switch v := val.(type) { 144 case float64: 145 return v 146 case json.Number: 147 num, _ := v.Float64() 148 return num 149 case string: 150 num, _ := strconv.ParseFloat(v, 64) 151 return num 152 } 153 } 154 return 0 155 } 156 157 // GetTime returns the value associated with the key as time. 158 func (p Map) GetTime(key string) time.Time { 159 val, _ := p[key].(time.Time) 160 return val 161 } 162 163 // GetDuration returns the value associated with the key as a duration. 164 func (p Map) GetDuration(key string) time.Duration { 165 val, _ := p[key].(time.Duration) 166 return val 167 } 168 169 // GetInt64s returns the value associated with the key as a slice of int64. 170 func (p Map) GetInt64s(key string) []int64 { 171 val, ok := p[key] 172 if ok { 173 switch val := val.(type) { 174 case []int64: 175 return val 176 } 177 } 178 return nil 179 } 180 181 // GetInt32s returns the value associated with the key as a slice of int32. 182 func (p Map) GetInt32s(key string) []int32 { 183 val, ok := p[key] 184 if ok { 185 switch val := val.(type) { 186 case []int32: 187 return val 188 } 189 } 190 return nil 191 } 192 193 // GetStrings returns the value associated with the key as a slice of strings. 194 func (p Map) GetStrings(key string) []string { 195 val, ok := p[key] 196 if ok { 197 switch val := val.(type) { 198 case []string: 199 return val 200 } 201 } 202 return nil 203 } 204 205 // GetSlice returns the value associated with the key as []interface{}. 206 // It returns nil if key does not present in Map or the value's type 207 // is not []interface{}. 208 func (p Map) GetSlice(key string) []interface{} { 209 val, _ := p[key].([]interface{}) 210 return val 211 } 212 213 // GetMap returns the value associated with the key as a Map (map[string]interface{}). 214 func (p Map) GetMap(key string) Map { 215 val, ok := p[key] 216 if ok { 217 switch val := val.(type) { 218 case Map: 219 return val 220 case map[string]interface{}: 221 return val 222 } 223 } 224 return nil 225 } 226 227 // GetStrstrMap returns the value associated with the key as a map of (map[string]string). 228 func (p Map) GetStrstrMap(key string) map[string]string { 229 if val, ok := p[key].(map[string]string); ok { 230 return val 231 } 232 return nil 233 } 234 235 // Iterate iterates the map in unspecified order, the given function fn 236 // will be called for each key value pair. 237 // The iteration can be aborted by returning a non-zero value from fn. 238 func (p Map) Iterate(fn func(k string, v interface{}) int) { 239 for k, v := range p { 240 if fn(k, v) != 0 { 241 return 242 } 243 } 244 }