github.com/gogf/gf/v2@v2.7.4/container/gmap/gmap_hash_str_any_map.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/gogf/gf. 6 // 7 8 package gmap 9 10 import ( 11 "reflect" 12 13 "github.com/gogf/gf/v2/container/gvar" 14 "github.com/gogf/gf/v2/internal/deepcopy" 15 "github.com/gogf/gf/v2/internal/empty" 16 "github.com/gogf/gf/v2/internal/json" 17 "github.com/gogf/gf/v2/internal/rwmutex" 18 "github.com/gogf/gf/v2/util/gconv" 19 ) 20 21 // StrAnyMap implements map[string]interface{} with RWMutex that has switch. 22 type StrAnyMap struct { 23 mu rwmutex.RWMutex 24 data map[string]interface{} 25 } 26 27 // NewStrAnyMap returns an empty StrAnyMap object. 28 // The parameter `safe` is used to specify whether using map in concurrent-safety, 29 // which is false in default. 30 func NewStrAnyMap(safe ...bool) *StrAnyMap { 31 return &StrAnyMap{ 32 mu: rwmutex.Create(safe...), 33 data: make(map[string]interface{}), 34 } 35 } 36 37 // NewStrAnyMapFrom creates and returns a hash map from given map `data`. 38 // Note that, the param `data` map will be set as the underlying data map(no deep copy), 39 // there might be some concurrent-safe issues when changing the map outside. 40 func NewStrAnyMapFrom(data map[string]interface{}, safe ...bool) *StrAnyMap { 41 return &StrAnyMap{ 42 mu: rwmutex.Create(safe...), 43 data: data, 44 } 45 } 46 47 // Iterator iterates the hash map readonly with custom callback function `f`. 48 // If `f` returns true, then it continues iterating; or false to stop. 49 func (m *StrAnyMap) Iterator(f func(k string, v interface{}) bool) { 50 for k, v := range m.Map() { 51 if !f(k, v) { 52 break 53 } 54 } 55 } 56 57 // Clone returns a new hash map with copy of current map data. 58 func (m *StrAnyMap) Clone() *StrAnyMap { 59 return NewStrAnyMapFrom(m.MapCopy(), m.mu.IsSafe()) 60 } 61 62 // Map returns the underlying data map. 63 // Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, 64 // or else a pointer to the underlying data. 65 func (m *StrAnyMap) Map() map[string]interface{} { 66 m.mu.RLock() 67 defer m.mu.RUnlock() 68 if !m.mu.IsSafe() { 69 return m.data 70 } 71 data := make(map[string]interface{}, len(m.data)) 72 for k, v := range m.data { 73 data[k] = v 74 } 75 return data 76 } 77 78 // MapStrAny returns a copy of the underlying data of the map as map[string]interface{}. 79 func (m *StrAnyMap) MapStrAny() map[string]interface{} { 80 return m.Map() 81 } 82 83 // MapCopy returns a copy of the underlying data of the hash map. 84 func (m *StrAnyMap) MapCopy() map[string]interface{} { 85 m.mu.RLock() 86 defer m.mu.RUnlock() 87 data := make(map[string]interface{}, len(m.data)) 88 for k, v := range m.data { 89 data[k] = v 90 } 91 return data 92 } 93 94 // FilterEmpty deletes all key-value pair of which the value is empty. 95 // Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty. 96 func (m *StrAnyMap) FilterEmpty() { 97 m.mu.Lock() 98 for k, v := range m.data { 99 if empty.IsEmpty(v) { 100 delete(m.data, k) 101 } 102 } 103 m.mu.Unlock() 104 } 105 106 // FilterNil deletes all key-value pair of which the value is nil. 107 func (m *StrAnyMap) FilterNil() { 108 m.mu.Lock() 109 defer m.mu.Unlock() 110 for k, v := range m.data { 111 if empty.IsNil(v) { 112 delete(m.data, k) 113 } 114 } 115 } 116 117 // Set sets key-value to the hash map. 118 func (m *StrAnyMap) Set(key string, val interface{}) { 119 m.mu.Lock() 120 if m.data == nil { 121 m.data = make(map[string]interface{}) 122 } 123 m.data[key] = val 124 m.mu.Unlock() 125 } 126 127 // Sets batch sets key-values to the hash map. 128 func (m *StrAnyMap) Sets(data map[string]interface{}) { 129 m.mu.Lock() 130 if m.data == nil { 131 m.data = data 132 } else { 133 for k, v := range data { 134 m.data[k] = v 135 } 136 } 137 m.mu.Unlock() 138 } 139 140 // Search searches the map with given `key`. 141 // Second return parameter `found` is true if key was found, otherwise false. 142 func (m *StrAnyMap) Search(key string) (value interface{}, found bool) { 143 m.mu.RLock() 144 if m.data != nil { 145 value, found = m.data[key] 146 } 147 m.mu.RUnlock() 148 return 149 } 150 151 // Get returns the value by given `key`. 152 func (m *StrAnyMap) Get(key string) (value interface{}) { 153 m.mu.RLock() 154 if m.data != nil { 155 value = m.data[key] 156 } 157 m.mu.RUnlock() 158 return 159 } 160 161 // Pop retrieves and deletes an item from the map. 162 func (m *StrAnyMap) Pop() (key string, value interface{}) { 163 m.mu.Lock() 164 defer m.mu.Unlock() 165 for key, value = range m.data { 166 delete(m.data, key) 167 return 168 } 169 return 170 } 171 172 // Pops retrieves and deletes `size` items from the map. 173 // It returns all items if size == -1. 174 func (m *StrAnyMap) Pops(size int) map[string]interface{} { 175 m.mu.Lock() 176 defer m.mu.Unlock() 177 if size > len(m.data) || size == -1 { 178 size = len(m.data) 179 } 180 if size == 0 { 181 return nil 182 } 183 var ( 184 index = 0 185 newMap = make(map[string]interface{}, size) 186 ) 187 for k, v := range m.data { 188 delete(m.data, k) 189 newMap[k] = v 190 index++ 191 if index == size { 192 break 193 } 194 } 195 return newMap 196 } 197 198 // doSetWithLockCheck checks whether value of the key exists with mutex.Lock, 199 // if not exists, set value to the map with given `key`, 200 // or else just return the existing value. 201 // 202 // When setting value, if `value` is type of `func() interface {}`, 203 // it will be executed with mutex.Lock of the hash map, 204 // and its return value will be set to the map with `key`. 205 // 206 // It returns value with given `key`. 207 func (m *StrAnyMap) doSetWithLockCheck(key string, value interface{}) interface{} { 208 m.mu.Lock() 209 defer m.mu.Unlock() 210 if m.data == nil { 211 m.data = make(map[string]interface{}) 212 } 213 if v, ok := m.data[key]; ok { 214 return v 215 } 216 if f, ok := value.(func() interface{}); ok { 217 value = f() 218 } 219 if value != nil { 220 m.data[key] = value 221 } 222 return value 223 } 224 225 // GetOrSet returns the value by key, 226 // or sets value with given `value` if it does not exist and then returns this value. 227 func (m *StrAnyMap) GetOrSet(key string, value interface{}) interface{} { 228 if v, ok := m.Search(key); !ok { 229 return m.doSetWithLockCheck(key, value) 230 } else { 231 return v 232 } 233 } 234 235 // GetOrSetFunc returns the value by key, 236 // or sets value with returned value of callback function `f` if it does not exist 237 // and then returns this value. 238 func (m *StrAnyMap) GetOrSetFunc(key string, f func() interface{}) interface{} { 239 if v, ok := m.Search(key); !ok { 240 return m.doSetWithLockCheck(key, f()) 241 } else { 242 return v 243 } 244 } 245 246 // GetOrSetFuncLock returns the value by key, 247 // or sets value with returned value of callback function `f` if it does not exist 248 // and then returns this value. 249 // 250 // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` 251 // with mutex.Lock of the hash map. 252 func (m *StrAnyMap) GetOrSetFuncLock(key string, f func() interface{}) interface{} { 253 if v, ok := m.Search(key); !ok { 254 return m.doSetWithLockCheck(key, f) 255 } else { 256 return v 257 } 258 } 259 260 // GetVar returns a Var with the value by given `key`. 261 // The returned Var is un-concurrent safe. 262 func (m *StrAnyMap) GetVar(key string) *gvar.Var { 263 return gvar.New(m.Get(key)) 264 } 265 266 // GetVarOrSet returns a Var with result from GetVarOrSet. 267 // The returned Var is un-concurrent safe. 268 func (m *StrAnyMap) GetVarOrSet(key string, value interface{}) *gvar.Var { 269 return gvar.New(m.GetOrSet(key, value)) 270 } 271 272 // GetVarOrSetFunc returns a Var with result from GetOrSetFunc. 273 // The returned Var is un-concurrent safe. 274 func (m *StrAnyMap) GetVarOrSetFunc(key string, f func() interface{}) *gvar.Var { 275 return gvar.New(m.GetOrSetFunc(key, f)) 276 } 277 278 // GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock. 279 // The returned Var is un-concurrent safe. 280 func (m *StrAnyMap) GetVarOrSetFuncLock(key string, f func() interface{}) *gvar.Var { 281 return gvar.New(m.GetOrSetFuncLock(key, f)) 282 } 283 284 // SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. 285 // It returns false if `key` exists, and `value` would be ignored. 286 func (m *StrAnyMap) SetIfNotExist(key string, value interface{}) bool { 287 if !m.Contains(key) { 288 m.doSetWithLockCheck(key, value) 289 return true 290 } 291 return false 292 } 293 294 // SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. 295 // It returns false if `key` exists, and `value` would be ignored. 296 func (m *StrAnyMap) SetIfNotExistFunc(key string, f func() interface{}) bool { 297 if !m.Contains(key) { 298 m.doSetWithLockCheck(key, f()) 299 return true 300 } 301 return false 302 } 303 304 // SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. 305 // It returns false if `key` exists, and `value` would be ignored. 306 // 307 // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that 308 // it executes function `f` with mutex.Lock of the hash map. 309 func (m *StrAnyMap) SetIfNotExistFuncLock(key string, f func() interface{}) bool { 310 if !m.Contains(key) { 311 m.doSetWithLockCheck(key, f) 312 return true 313 } 314 return false 315 } 316 317 // Removes batch deletes values of the map by keys. 318 func (m *StrAnyMap) Removes(keys []string) { 319 m.mu.Lock() 320 if m.data != nil { 321 for _, key := range keys { 322 delete(m.data, key) 323 } 324 } 325 m.mu.Unlock() 326 } 327 328 // Remove deletes value from map by given `key`, and return this deleted value. 329 func (m *StrAnyMap) Remove(key string) (value interface{}) { 330 m.mu.Lock() 331 if m.data != nil { 332 var ok bool 333 if value, ok = m.data[key]; ok { 334 delete(m.data, key) 335 } 336 } 337 m.mu.Unlock() 338 return 339 } 340 341 // Keys returns all keys of the map as a slice. 342 func (m *StrAnyMap) Keys() []string { 343 m.mu.RLock() 344 var ( 345 keys = make([]string, len(m.data)) 346 index = 0 347 ) 348 for key := range m.data { 349 keys[index] = key 350 index++ 351 } 352 m.mu.RUnlock() 353 return keys 354 } 355 356 // Values returns all values of the map as a slice. 357 func (m *StrAnyMap) Values() []interface{} { 358 m.mu.RLock() 359 var ( 360 values = make([]interface{}, len(m.data)) 361 index = 0 362 ) 363 for _, value := range m.data { 364 values[index] = value 365 index++ 366 } 367 m.mu.RUnlock() 368 return values 369 } 370 371 // Contains checks whether a key exists. 372 // It returns true if the `key` exists, or else false. 373 func (m *StrAnyMap) Contains(key string) bool { 374 var ok bool 375 m.mu.RLock() 376 if m.data != nil { 377 _, ok = m.data[key] 378 } 379 m.mu.RUnlock() 380 return ok 381 } 382 383 // Size returns the size of the map. 384 func (m *StrAnyMap) Size() int { 385 m.mu.RLock() 386 length := len(m.data) 387 m.mu.RUnlock() 388 return length 389 } 390 391 // IsEmpty checks whether the map is empty. 392 // It returns true if map is empty, or else false. 393 func (m *StrAnyMap) IsEmpty() bool { 394 return m.Size() == 0 395 } 396 397 // Clear deletes all data of the map, it will remake a new underlying data map. 398 func (m *StrAnyMap) Clear() { 399 m.mu.Lock() 400 m.data = make(map[string]interface{}) 401 m.mu.Unlock() 402 } 403 404 // Replace the data of the map with given `data`. 405 func (m *StrAnyMap) Replace(data map[string]interface{}) { 406 m.mu.Lock() 407 m.data = data 408 m.mu.Unlock() 409 } 410 411 // LockFunc locks writing with given callback function `f` within RWMutex.Lock. 412 func (m *StrAnyMap) LockFunc(f func(m map[string]interface{})) { 413 m.mu.Lock() 414 defer m.mu.Unlock() 415 f(m.data) 416 } 417 418 // RLockFunc locks reading with given callback function `f` within RWMutex.RLock. 419 func (m *StrAnyMap) RLockFunc(f func(m map[string]interface{})) { 420 m.mu.RLock() 421 defer m.mu.RUnlock() 422 f(m.data) 423 } 424 425 // Flip exchanges key-value of the map to value-key. 426 func (m *StrAnyMap) Flip() { 427 m.mu.Lock() 428 defer m.mu.Unlock() 429 n := make(map[string]interface{}, len(m.data)) 430 for k, v := range m.data { 431 n[gconv.String(v)] = k 432 } 433 m.data = n 434 } 435 436 // Merge merges two hash maps. 437 // The `other` map will be merged into the map `m`. 438 func (m *StrAnyMap) Merge(other *StrAnyMap) { 439 m.mu.Lock() 440 defer m.mu.Unlock() 441 if m.data == nil { 442 m.data = other.MapCopy() 443 return 444 } 445 if other != m { 446 other.mu.RLock() 447 defer other.mu.RUnlock() 448 } 449 for k, v := range other.data { 450 m.data[k] = v 451 } 452 } 453 454 // String returns the map as a string. 455 func (m *StrAnyMap) String() string { 456 if m == nil { 457 return "" 458 } 459 b, _ := m.MarshalJSON() 460 return string(b) 461 } 462 463 // MarshalJSON implements the interface MarshalJSON for json.Marshal. 464 func (m StrAnyMap) MarshalJSON() ([]byte, error) { 465 m.mu.RLock() 466 defer m.mu.RUnlock() 467 return json.Marshal(m.data) 468 } 469 470 // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. 471 func (m *StrAnyMap) UnmarshalJSON(b []byte) error { 472 m.mu.Lock() 473 defer m.mu.Unlock() 474 if m.data == nil { 475 m.data = make(map[string]interface{}) 476 } 477 if err := json.UnmarshalUseNumber(b, &m.data); err != nil { 478 return err 479 } 480 return nil 481 } 482 483 // UnmarshalValue is an interface implement which sets any type of value for map. 484 func (m *StrAnyMap) UnmarshalValue(value interface{}) (err error) { 485 m.mu.Lock() 486 defer m.mu.Unlock() 487 m.data = gconv.Map(value) 488 return 489 } 490 491 // DeepCopy implements interface for deep copy of current type. 492 func (m *StrAnyMap) DeepCopy() interface{} { 493 if m == nil { 494 return nil 495 } 496 m.mu.RLock() 497 defer m.mu.RUnlock() 498 data := make(map[string]interface{}, len(m.data)) 499 for k, v := range m.data { 500 data[k] = deepcopy.Copy(v) 501 } 502 return NewStrAnyMapFrom(data, m.mu.IsSafe()) 503 } 504 505 // IsSubOf checks whether the current map is a sub-map of `other`. 506 func (m *StrAnyMap) IsSubOf(other *StrAnyMap) bool { 507 if m == other { 508 return true 509 } 510 m.mu.RLock() 511 defer m.mu.RUnlock() 512 other.mu.RLock() 513 defer other.mu.RUnlock() 514 for key, value := range m.data { 515 otherValue, ok := other.data[key] 516 if !ok { 517 return false 518 } 519 if otherValue != value { 520 return false 521 } 522 } 523 return true 524 } 525 526 // Diff compares current map `m` with map `other` and returns their different keys. 527 // The returned `addedKeys` are the keys that are in map `m` but not in map `other`. 528 // The returned `removedKeys` are the keys that are in map `other` but not in map `m`. 529 // The returned `updatedKeys` are the keys that are both in map `m` and `other` but their values and not equal (`!=`). 530 func (m *StrAnyMap) Diff(other *StrAnyMap) (addedKeys, removedKeys, updatedKeys []string) { 531 m.mu.RLock() 532 defer m.mu.RUnlock() 533 other.mu.RLock() 534 defer other.mu.RUnlock() 535 536 for key := range m.data { 537 if _, ok := other.data[key]; !ok { 538 removedKeys = append(removedKeys, key) 539 } else if !reflect.DeepEqual(m.data[key], other.data[key]) { 540 updatedKeys = append(updatedKeys, key) 541 } 542 } 543 for key := range other.data { 544 if _, ok := m.data[key]; !ok { 545 addedKeys = append(addedKeys, key) 546 } 547 } 548 return 549 }