2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-05-08 17:21:18 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with gm file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
//
|
|
|
|
|
|
|
|
package gmap
|
|
|
|
|
|
|
|
import (
|
2020-06-17 21:16:25 +08:00
|
|
|
"github.com/gogf/gf/internal/json"
|
2019-07-13 17:48:16 +08:00
|
|
|
|
2019-09-29 14:27:09 +08:00
|
|
|
"github.com/gogf/gf/internal/empty"
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/internal/rwmutex"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2019-05-08 17:21:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type StrIntMap struct {
|
2020-04-07 20:06:26 +08:00
|
|
|
mu rwmutex.RWMutex
|
2019-05-08 17:21:18 +08:00
|
|
|
data map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStrIntMap returns an empty StrIntMap object.
|
2021-09-16 20:57:59 +08:00
|
|
|
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
2019-07-23 23:20:27 +08:00
|
|
|
// which is false in default.
|
|
|
|
func NewStrIntMap(safe ...bool) *StrIntMap {
|
2019-05-08 17:21:18 +08:00
|
|
|
return &StrIntMap{
|
2020-04-07 20:06:26 +08:00
|
|
|
mu: rwmutex.Create(safe...),
|
2019-06-19 09:06:52 +08:00
|
|
|
data: make(map[string]int),
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// NewStrIntMapFrom creates and returns a hash map from given map `data`.
|
|
|
|
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
2019-05-08 17:21:18 +08:00
|
|
|
// there might be some concurrent-safe issues when changing the map outside.
|
2019-07-23 23:20:27 +08:00
|
|
|
func NewStrIntMapFrom(data map[string]int, safe ...bool) *StrIntMap {
|
2019-05-08 17:21:18 +08:00
|
|
|
return &StrIntMap{
|
2020-04-07 20:06:26 +08:00
|
|
|
mu: rwmutex.Create(safe...),
|
2019-06-19 09:06:52 +08:00
|
|
|
data: data,
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Iterator iterates the hash map readonly with custom callback function `f`.
|
|
|
|
// If `f` returns true, then it continues iterating; or false to stop.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) Iterator(f func(k string, v int) bool) {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
for k, v := range m.data {
|
|
|
|
if !f(k, v) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone returns a new hash map with copy of current map data.
|
|
|
|
func (m *StrIntMap) Clone() *StrIntMap {
|
2020-12-02 21:38:29 +08:00
|
|
|
return NewStrIntMapFrom(m.MapCopy(), m.mu.IsSafe())
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
2019-09-29 14:27:09 +08:00
|
|
|
// Map returns the underlying data map.
|
|
|
|
// Note that, if it's in concurrent-safe usage, it returns a copy of underlying data,
|
|
|
|
// or else a pointer to the underlying data.
|
|
|
|
func (m *StrIntMap) Map() map[string]int {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
if !m.mu.IsSafe() {
|
|
|
|
return m.data
|
|
|
|
}
|
|
|
|
data := make(map[string]int, len(m.data))
|
|
|
|
for k, v := range m.data {
|
|
|
|
data[k] = v
|
|
|
|
}
|
|
|
|
return data
|
2019-09-26 20:01:48 +08:00
|
|
|
}
|
|
|
|
|
2019-11-30 18:33:51 +08:00
|
|
|
// MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.
|
2019-09-30 17:23:23 +08:00
|
|
|
func (m *StrIntMap) MapStrAny() map[string]interface{} {
|
|
|
|
m.mu.RLock()
|
2020-04-07 20:06:26 +08:00
|
|
|
defer m.mu.RUnlock()
|
2019-09-30 17:23:23 +08:00
|
|
|
data := make(map[string]interface{}, len(m.data))
|
|
|
|
for k, v := range m.data {
|
|
|
|
data[k] = v
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2019-11-30 18:33:51 +08:00
|
|
|
// MapCopy returns a copy of the underlying data of the hash map.
|
2019-09-29 14:27:09 +08:00
|
|
|
func (m *StrIntMap) MapCopy() map[string]int {
|
2019-05-08 17:21:18 +08:00
|
|
|
m.mu.RLock()
|
2019-09-29 14:27:09 +08:00
|
|
|
defer m.mu.RUnlock()
|
2019-05-12 21:11:26 +08:00
|
|
|
data := make(map[string]int, len(m.data))
|
2019-05-08 17:21:18 +08:00
|
|
|
for k, v := range m.data {
|
|
|
|
data[k] = v
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2019-09-29 14:27:09 +08:00
|
|
|
// FilterEmpty deletes all key-value pair of which the value is empty.
|
2020-06-04 20:45:18 +08:00
|
|
|
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
2019-09-29 14:27:09 +08:00
|
|
|
func (m *StrIntMap) FilterEmpty() {
|
|
|
|
m.mu.Lock()
|
|
|
|
for k, v := range m.data {
|
|
|
|
if empty.IsEmpty(v) {
|
|
|
|
delete(m.data, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-05-08 17:21:18 +08:00
|
|
|
// Set sets key-value to the hash map.
|
|
|
|
func (m *StrIntMap) Set(key string, val int) {
|
|
|
|
m.mu.Lock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data == nil {
|
|
|
|
m.data = make(map[string]int)
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
m.data[key] = val
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets batch sets key-values to the hash map.
|
|
|
|
func (m *StrIntMap) Sets(data map[string]int) {
|
|
|
|
m.mu.Lock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data == nil {
|
|
|
|
m.data = data
|
|
|
|
} else {
|
|
|
|
for k, v := range data {
|
|
|
|
m.data[k] = v
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Search searches the map with given `key`.
|
|
|
|
// Second return parameter `found` is true if key was found, otherwise false.
|
2019-05-10 13:38:06 +08:00
|
|
|
func (m *StrIntMap) Search(key string) (value int, found bool) {
|
|
|
|
m.mu.RLock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data != nil {
|
|
|
|
value, found = m.data[key]
|
|
|
|
}
|
2019-05-10 13:38:06 +08:00
|
|
|
m.mu.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Get returns the value by given `key`.
|
2020-04-07 20:06:26 +08:00
|
|
|
func (m *StrIntMap) Get(key string) (value int) {
|
2019-05-08 17:21:18 +08:00
|
|
|
m.mu.RLock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data != nil {
|
|
|
|
value, _ = m.data[key]
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
m.mu.RUnlock()
|
2020-04-07 20:06:26 +08:00
|
|
|
return
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 23:33:06 +08:00
|
|
|
// Pop retrieves and deletes an item from the map.
|
|
|
|
func (m *StrIntMap) Pop() (key string, value int) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
for key, value = range m.data {
|
|
|
|
delete(m.data, key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Pops retrieves and deletes `size` items from the map.
|
2019-10-16 23:33:06 +08:00
|
|
|
// It returns all items if size == -1.
|
|
|
|
func (m *StrIntMap) Pops(size int) map[string]int {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if size > len(m.data) || size == -1 {
|
|
|
|
size = len(m.data)
|
|
|
|
}
|
|
|
|
if size == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-07 20:06:26 +08:00
|
|
|
var (
|
|
|
|
index = 0
|
|
|
|
newMap = make(map[string]int, size)
|
|
|
|
)
|
2019-10-16 23:33:06 +08:00
|
|
|
for k, v := range m.data {
|
|
|
|
delete(m.data, k)
|
|
|
|
newMap[k] = v
|
|
|
|
index++
|
|
|
|
if index == size {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newMap
|
|
|
|
}
|
|
|
|
|
2019-05-08 17:21:18 +08:00
|
|
|
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
2021-09-16 20:57:59 +08:00
|
|
|
// if not exists, set value to the map with given `key`,
|
2019-05-08 17:21:18 +08:00
|
|
|
// or else just return the existing value.
|
|
|
|
//
|
2021-09-16 20:57:59 +08:00
|
|
|
// It returns value with given `key`.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) doSetWithLockCheck(key string, value int) int {
|
|
|
|
m.mu.Lock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data == nil {
|
|
|
|
m.data = make(map[string]int)
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
if v, ok := m.data[key]; ok {
|
|
|
|
m.mu.Unlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
m.data[key] = value
|
|
|
|
m.mu.Unlock()
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSet returns the value by key,
|
2021-09-16 20:57:59 +08:00
|
|
|
// or sets value with given `value` if it does not exist and then returns this value.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) GetOrSet(key string, value int) int {
|
2019-05-10 13:38:06 +08:00
|
|
|
if v, ok := m.Search(key); !ok {
|
2019-05-08 17:21:18 +08:00
|
|
|
return m.doSetWithLockCheck(key, value)
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSetFunc returns the value by key,
|
2021-09-16 20:57:59 +08:00
|
|
|
// or sets value with returned value of callback function `f` if it does not exist
|
2019-11-06 20:22:20 +08:00
|
|
|
// and then returns this value.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) GetOrSetFunc(key string, f func() int) int {
|
2019-05-10 13:38:06 +08:00
|
|
|
if v, ok := m.Search(key); !ok {
|
2019-05-08 17:21:18 +08:00
|
|
|
return m.doSetWithLockCheck(key, f())
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSetFuncLock returns the value by key,
|
2021-09-16 20:57:59 +08:00
|
|
|
// or sets value with returned value of callback function `f` if it does not exist
|
2019-11-06 20:22:20 +08:00
|
|
|
// and then returns this value.
|
2019-05-08 17:21:18 +08:00
|
|
|
//
|
2021-09-16 20:57:59 +08:00
|
|
|
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
2019-05-08 17:21:18 +08:00
|
|
|
// with mutex.Lock of the hash map.
|
|
|
|
func (m *StrIntMap) GetOrSetFuncLock(key string, f func() int) int {
|
2019-05-10 13:38:06 +08:00
|
|
|
if v, ok := m.Search(key); !ok {
|
2019-05-08 17:21:18 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data == nil {
|
|
|
|
m.data = make(map[string]int)
|
|
|
|
}
|
2019-05-10 13:38:06 +08:00
|
|
|
if v, ok = m.data[key]; ok {
|
2019-05-08 17:21:18 +08:00
|
|
|
return v
|
|
|
|
}
|
2019-05-10 13:38:06 +08:00
|
|
|
v = f()
|
|
|
|
m.data[key] = v
|
|
|
|
return v
|
2019-05-08 17:21:18 +08:00
|
|
|
} else {
|
2019-05-10 13:38:06 +08:00
|
|
|
return v
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
|
|
|
// It returns false if <key> exists, and `value` would be ignored.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) SetIfNotExist(key string, value int) bool {
|
|
|
|
if !m.Contains(key) {
|
|
|
|
m.doSetWithLockCheck(key, value)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
|
|
|
// It returns false if <key> exists, and `value` would be ignored.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) SetIfNotExistFunc(key string, f func() int) bool {
|
|
|
|
if !m.Contains(key) {
|
|
|
|
m.doSetWithLockCheck(key, f())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
|
|
|
// It returns false if <key> exists, and `value` would be ignored.
|
2019-05-08 17:21:18 +08:00
|
|
|
//
|
|
|
|
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
2021-09-16 20:57:59 +08:00
|
|
|
// it executes function `f` with mutex.Lock of the hash map.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) SetIfNotExistFuncLock(key string, f func() int) bool {
|
|
|
|
if !m.Contains(key) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data == nil {
|
|
|
|
m.data = make(map[string]int)
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
if _, ok := m.data[key]; !ok {
|
|
|
|
m.data[key] = f()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes batch deletes values of the map by keys.
|
|
|
|
func (m *StrIntMap) Removes(keys []string) {
|
|
|
|
m.mu.Lock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data != nil {
|
|
|
|
for _, key := range keys {
|
|
|
|
delete(m.data, key)
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Remove deletes value from map by given `key`, and return this deleted value.
|
2020-04-07 20:06:26 +08:00
|
|
|
func (m *StrIntMap) Remove(key string) (value int) {
|
2019-05-08 17:21:18 +08:00
|
|
|
m.mu.Lock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data != nil {
|
|
|
|
var ok bool
|
|
|
|
if value, ok = m.data[key]; ok {
|
|
|
|
delete(m.data, key)
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
2020-04-07 20:06:26 +08:00
|
|
|
return
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keys returns all keys of the map as a slice.
|
|
|
|
func (m *StrIntMap) Keys() []string {
|
|
|
|
m.mu.RLock()
|
2020-04-07 20:06:26 +08:00
|
|
|
var (
|
|
|
|
keys = make([]string, len(m.data))
|
|
|
|
index = 0
|
|
|
|
)
|
2019-05-08 17:21:18 +08:00
|
|
|
for key := range m.data {
|
2019-05-12 21:11:26 +08:00
|
|
|
keys[index] = key
|
|
|
|
index++
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values returns all values of the map as a slice.
|
|
|
|
func (m *StrIntMap) Values() []int {
|
|
|
|
m.mu.RLock()
|
2020-04-07 20:06:26 +08:00
|
|
|
var (
|
|
|
|
values = make([]int, len(m.data))
|
|
|
|
index = 0
|
|
|
|
)
|
2019-05-08 17:21:18 +08:00
|
|
|
for _, value := range m.data {
|
2019-05-12 21:11:26 +08:00
|
|
|
values[index] = value
|
|
|
|
index++
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains checks whether a key exists.
|
2021-09-16 20:57:59 +08:00
|
|
|
// It returns true if the `key` exists, or else false.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) Contains(key string) bool {
|
2020-04-07 20:06:26 +08:00
|
|
|
var ok bool
|
2019-05-08 17:21:18 +08:00
|
|
|
m.mu.RLock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data != nil {
|
|
|
|
_, ok = m.data[key]
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
m.mu.RUnlock()
|
2020-04-07 20:06:26 +08:00
|
|
|
return ok
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the map.
|
|
|
|
func (m *StrIntMap) Size() int {
|
|
|
|
m.mu.RLock()
|
|
|
|
length := len(m.data)
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return length
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty checks whether the map is empty.
|
|
|
|
// It returns true if map is empty, or else false.
|
|
|
|
func (m *StrIntMap) IsEmpty() bool {
|
2019-10-16 23:33:06 +08:00
|
|
|
return m.Size() == 0
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear deletes all data of the map, it will remake a new underlying data map.
|
|
|
|
func (m *StrIntMap) Clear() {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.data = make(map[string]int)
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// Replace the data of the map with given `data`.
|
2019-11-04 21:26:16 +08:00
|
|
|
func (m *StrIntMap) Replace(data map[string]int) {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.data = data
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) LockFunc(f func(m map[string]int)) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
f(m.data)
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) RLockFunc(f func(m map[string]int)) {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
f(m.data)
|
|
|
|
}
|
|
|
|
|
2019-05-10 13:38:06 +08:00
|
|
|
// Flip exchanges key-value of the map to value-key.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) Flip() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
n := make(map[string]int, len(m.data))
|
|
|
|
for k, v := range m.data {
|
|
|
|
n[gconv.String(v)] = gconv.Int(k)
|
|
|
|
}
|
|
|
|
m.data = n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges two hash maps.
|
2021-09-16 20:57:59 +08:00
|
|
|
// The <other> map will be merged into the map `m`.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrIntMap) Merge(other *StrIntMap) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data == nil {
|
|
|
|
m.data = other.MapCopy()
|
|
|
|
return
|
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
if other != m {
|
|
|
|
other.mu.RLock()
|
|
|
|
defer other.mu.RUnlock()
|
|
|
|
}
|
|
|
|
for k, v := range other.data {
|
|
|
|
m.data[k] = v
|
|
|
|
}
|
|
|
|
}
|
2019-07-13 17:48:16 +08:00
|
|
|
|
2020-09-02 19:53:58 +08:00
|
|
|
// String returns the map as a string.
|
|
|
|
func (m *StrIntMap) String() string {
|
|
|
|
b, _ := m.MarshalJSON()
|
2021-09-14 19:30:20 +08:00
|
|
|
return string(b)
|
2020-09-02 19:53:58 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 17:48:16 +08:00
|
|
|
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
|
|
|
func (m *StrIntMap) MarshalJSON() ([]byte, error) {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
return json.Marshal(m.data)
|
|
|
|
}
|
2019-09-30 14:23:15 +08:00
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
|
|
|
func (m *StrIntMap) UnmarshalJSON(b []byte) error {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data == nil {
|
|
|
|
m.data = make(map[string]int)
|
|
|
|
}
|
2021-05-15 22:38:07 +08:00
|
|
|
if err := json.UnmarshalUseNumber(b, &m.data); err != nil {
|
2019-09-30 14:23:15 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-20 19:56:42 +08:00
|
|
|
|
|
|
|
// UnmarshalValue is an interface implement which sets any type of value for map.
|
|
|
|
func (m *StrIntMap) UnmarshalValue(value interface{}) (err error) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-04-07 20:06:26 +08:00
|
|
|
if m.data == nil {
|
|
|
|
m.data = make(map[string]int)
|
|
|
|
}
|
2020-01-20 19:56:42 +08:00
|
|
|
switch value.(type) {
|
|
|
|
case string, []byte:
|
2021-05-15 22:38:07 +08:00
|
|
|
return json.UnmarshalUseNumber(gconv.Bytes(value), &m.data)
|
2020-01-20 19:56:42 +08:00
|
|
|
default:
|
|
|
|
for k, v := range gconv.Map(value) {
|
|
|
|
m.data[k] = gconv.Int(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|