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.
|
2025-11-21 14:12:56 +08:00
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-05-08 17:21:18 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
package gmap
|
|
|
|
|
|
2025-11-28 21:41:30 +08:00
|
|
|
import "sync"
|
2019-05-08 17:21:18 +08:00
|
|
|
|
2023-03-08 14:12:51 +08:00
|
|
|
// StrStrMap implements map[string]string with RWMutex that has switch.
|
2019-05-08 17:21:18 +08:00
|
|
|
type StrStrMap struct {
|
2025-11-28 21:41:30 +08:00
|
|
|
*KVMap[string, string]
|
|
|
|
|
once sync.Once
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewStrStrMap returns an empty StrStrMap 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 NewStrStrMap(safe ...bool) *StrStrMap {
|
2019-05-08 17:21:18 +08:00
|
|
|
return &StrStrMap{
|
2025-11-28 21:41:30 +08:00
|
|
|
KVMap: NewKVMap[string, string](safe...),
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// NewStrStrMapFrom 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 NewStrStrMapFrom(data map[string]string, safe ...bool) *StrStrMap {
|
2019-06-19 09:06:52 +08:00
|
|
|
return &StrStrMap{
|
2025-11-28 21:41:30 +08:00
|
|
|
KVMap: NewKVMapFrom(data, safe...),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 21:41:30 +08:00
|
|
|
// lazyInit lazily initializes the map.
|
|
|
|
|
func (m *StrStrMap) lazyInit() {
|
|
|
|
|
m.once.Do(func() {
|
|
|
|
|
if m.KVMap == nil {
|
|
|
|
|
m.KVMap = NewKVMap[string, string](false)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
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-06-19 09:06:52 +08:00
|
|
|
func (m *StrStrMap) Iterator(f func(k string, v string) bool) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.Iterator(f)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clone returns a new hash map with copy of current map data.
|
2025-11-28 21:41:30 +08:00
|
|
|
func (m *StrStrMap) Clone(safe ...bool) *StrStrMap {
|
|
|
|
|
m.lazyInit()
|
|
|
|
|
return &StrStrMap{KVMap: m.KVMap.Clone(safe...)}
|
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 *StrStrMap) Map() map[string]string {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Map()
|
2019-09-26 20:01:48 +08:00
|
|
|
}
|
|
|
|
|
|
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395)
This pull request standardizes the use of the Go 1.18+ `any` type alias
instead of `interface{}` throughout the codebase. The change improves
code readability and aligns with modern Go best practices. The update
touches many files, including core data structures, code generation
templates, logging utilities, and test data, ensuring consistency across
all usages.
**Type alias migration to `any`:**
* Replaced all instances of `interface{}` with `any` in core data
structures such as `garray` and in generated model structs (e.g.,
`TableUser`, `User1`, `User2`) to modernize type usage.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
* Updated function signatures, method parameters, and return types from
`interface{}` to `any` in various parts of the codebase, including code
generation, service logic, and logging utilities (e.g., `mlog`).
[[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74)
[[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
[[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107)
[[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121)
[[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112)
[[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36)
[[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74)
[[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96)
**Generated code and templates:**
* Adjusted generated files and code generation templates to output `any`
instead of `interface{}` for relevant struct fields and function
signatures, ensuring that new code generation aligns with the updated
convention.
[[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
[[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
**Container and utility updates:**
* Refactored the `garray` container implementation and related
constructors/methods to use `[]any` instead of `[]interface{}`, along
with corresponding function signatures.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52)
[[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62)
[[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86)
[[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97)
[[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114)
[[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124)
[[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143)
[[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167)
These changes collectively modernize the codebase and prepare it for
future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
|
|
|
// MapStrAny returns a copy of the underlying data of the map as map[string]any.
|
|
|
|
|
func (m *StrStrMap) MapStrAny() map[string]any {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.MapStrAny()
|
2019-09-30 17:23:23 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) MapCopy() map[string]string {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.MapCopy()
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) FilterEmpty() {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.FilterEmpty()
|
2019-09-29 14:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 17:21:18 +08:00
|
|
|
// Set sets key-value to the hash map.
|
|
|
|
|
func (m *StrStrMap) Set(key string, val string) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.Set(key, val)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sets batch sets key-values to the hash map.
|
|
|
|
|
func (m *StrStrMap) Sets(data map[string]string) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.Sets(data)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) Search(key string) (value string, found bool) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Search(key)
|
2019-05-10 13:38:06 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) Get(key string) (value string) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Get(key)
|
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 *StrStrMap) Pop() (key, value string) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Pop()
|
2019-10-16 23:33:06 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) Pops(size int) map[string]string {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Pops(size)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 *StrStrMap) GetOrSet(key string, value string) string {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.GetOrSet(key, value)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 *StrStrMap) GetOrSetFunc(key string, f func() string) string {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.GetOrSetFunc(key, f)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 *StrStrMap) GetOrSetFuncLock(key string, f func() string) string {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.GetOrSetFuncLock(key, f)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +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 *StrStrMap) SetIfNotExist(key string, value string) bool {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.SetIfNotExist(key, value)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
2021-10-21 18:22:47 +08:00
|
|
|
// It returns false if `key` exists, and `value` would be ignored.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrStrMap) SetIfNotExistFunc(key string, f func() string) bool {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.SetIfNotExistFunc(key, f)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
2021-10-21 18:22:47 +08:00
|
|
|
// 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 *StrStrMap) SetIfNotExistFuncLock(key string, f func() string) bool {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.SetIfNotExistFuncLock(key, f)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Removes batch deletes values of the map by keys.
|
|
|
|
|
func (m *StrStrMap) Removes(keys []string) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.Removes(keys)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) Remove(key string) (value string) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Remove(key)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Keys returns all keys of the map as a slice.
|
|
|
|
|
func (m *StrStrMap) Keys() []string {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Keys()
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Values returns all values of the map as a slice.
|
|
|
|
|
func (m *StrStrMap) Values() []string {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Values()
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 *StrStrMap) Contains(key string) bool {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Contains(key)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Size returns the size of the map.
|
|
|
|
|
func (m *StrStrMap) Size() int {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Size()
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEmpty checks whether the map is empty.
|
|
|
|
|
// It returns true if map is empty, or else false.
|
|
|
|
|
func (m *StrStrMap) IsEmpty() bool {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.IsEmpty()
|
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 *StrStrMap) Clear() {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.Clear()
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) Replace(data map[string]string) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.Replace(data)
|
2019-11-04 21:26:16 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) LockFunc(f func(m map[string]string)) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.LockFunc(f)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) RLockFunc(f func(m map[string]string)) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.RLockFunc(f)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
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 *StrStrMap) Flip() {
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
n := make(map[string]string, len(m.data))
|
|
|
|
|
for k, v := range m.data {
|
|
|
|
|
n[v] = k
|
|
|
|
|
}
|
|
|
|
|
m.data = n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Merge merges two hash maps.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The `other` map will be merged into the map `m`.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *StrStrMap) Merge(other *StrStrMap) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
m.KVMap.Merge(other.KVMap)
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
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 *StrStrMap) String() string {
|
2022-03-21 22:04:15 +08:00
|
|
|
if m == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.String()
|
2020-09-02 19:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-13 17:48:16 +08:00
|
|
|
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
2022-01-19 16:55:57 +08:00
|
|
|
func (m StrStrMap) MarshalJSON() ([]byte, error) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.MarshalJSON()
|
2019-07-13 17:48:16 +08:00
|
|
|
}
|
2019-09-30 14:23:15 +08:00
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
|
|
|
|
func (m *StrStrMap) UnmarshalJSON(b []byte) error {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.UnmarshalJSON(b)
|
2019-09-30 14:23:15 +08:00
|
|
|
}
|
2020-01-20 19:56:42 +08:00
|
|
|
|
|
|
|
|
// UnmarshalValue is an interface implement which sets any type of value for map.
|
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395)
This pull request standardizes the use of the Go 1.18+ `any` type alias
instead of `interface{}` throughout the codebase. The change improves
code readability and aligns with modern Go best practices. The update
touches many files, including core data structures, code generation
templates, logging utilities, and test data, ensuring consistency across
all usages.
**Type alias migration to `any`:**
* Replaced all instances of `interface{}` with `any` in core data
structures such as `garray` and in generated model structs (e.g.,
`TableUser`, `User1`, `User2`) to modernize type usage.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
* Updated function signatures, method parameters, and return types from
`interface{}` to `any` in various parts of the codebase, including code
generation, service logic, and logging utilities (e.g., `mlog`).
[[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74)
[[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
[[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107)
[[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121)
[[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112)
[[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36)
[[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74)
[[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96)
**Generated code and templates:**
* Adjusted generated files and code generation templates to output `any`
instead of `interface{}` for relevant struct fields and function
signatures, ensuring that new code generation aligns with the updated
convention.
[[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
[[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
**Container and utility updates:**
* Refactored the `garray` container implementation and related
constructors/methods to use `[]any` instead of `[]interface{}`, along
with corresponding function signatures.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52)
[[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62)
[[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86)
[[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97)
[[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114)
[[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124)
[[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143)
[[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167)
These changes collectively modernize the codebase and prepare it for
future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
|
|
|
func (m *StrStrMap) UnmarshalValue(value any) (err error) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.UnmarshalValue(value)
|
2020-01-20 19:56:42 +08:00
|
|
|
}
|
2022-05-23 16:51:10 +08:00
|
|
|
|
|
|
|
|
// DeepCopy implements interface for deep copy of current type.
|
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395)
This pull request standardizes the use of the Go 1.18+ `any` type alias
instead of `interface{}` throughout the codebase. The change improves
code readability and aligns with modern Go best practices. The update
touches many files, including core data structures, code generation
templates, logging utilities, and test data, ensuring consistency across
all usages.
**Type alias migration to `any`:**
* Replaced all instances of `interface{}` with `any` in core data
structures such as `garray` and in generated model structs (e.g.,
`TableUser`, `User1`, `User2`) to modernize type usage.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
* Updated function signatures, method parameters, and return types from
`interface{}` to `any` in various parts of the codebase, including code
generation, service logic, and logging utilities (e.g., `mlog`).
[[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74)
[[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
[[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107)
[[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121)
[[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112)
[[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36)
[[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74)
[[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96)
**Generated code and templates:**
* Adjusted generated files and code generation templates to output `any`
instead of `interface{}` for relevant struct fields and function
signatures, ensuring that new code generation aligns with the updated
convention.
[[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
[[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
**Container and utility updates:**
* Refactored the `garray` container implementation and related
constructors/methods to use `[]any` instead of `[]interface{}`, along
with corresponding function signatures.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52)
[[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62)
[[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86)
[[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97)
[[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114)
[[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124)
[[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143)
[[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167)
These changes collectively modernize the codebase and prepare it for
future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
|
|
|
func (m *StrStrMap) DeepCopy() any {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return &StrStrMap{
|
|
|
|
|
KVMap: m.KVMap.DeepCopy().(*KVMap[string, string]),
|
2022-05-23 16:51:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-08 14:12:51 +08:00
|
|
|
|
|
|
|
|
// IsSubOf checks whether the current map is a sub-map of `other`.
|
|
|
|
|
func (m *StrStrMap) IsSubOf(other *StrStrMap) bool {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.IsSubOf(other.KVMap)
|
2023-03-08 14:12:51 +08:00
|
|
|
}
|
2023-07-20 20:07:43 +08:00
|
|
|
|
|
|
|
|
// Diff compares current map `m` with map `other` and returns their different keys.
|
|
|
|
|
// The returned `addedKeys` are the keys that are in map `m` but not in map `other`.
|
|
|
|
|
// The returned `removedKeys` are the keys that are in map `other` but not in map `m`.
|
|
|
|
|
// The returned `updatedKeys` are the keys that are both in map `m` and `other` but their values and not equal (`!=`).
|
|
|
|
|
func (m *StrStrMap) Diff(other *StrStrMap) (addedKeys, removedKeys, updatedKeys []string) {
|
2025-11-28 21:41:30 +08:00
|
|
|
m.lazyInit()
|
|
|
|
|
return m.KVMap.Diff(other.KVMap)
|
2023-07-20 20:07:43 +08:00
|
|
|
}
|