mirror of
https://gitee.com/johng/gf
synced 2026-06-09 02:57:43 +08:00
Compare commits
12 Commits
fix/contai
...
fix/4193-2
| Author | SHA1 | Date | |
|---|---|---|---|
| 3f1c2bcd29 | |||
| 6a7b6e729b | |||
| 73211707fb | |||
| 609f44c5fe | |||
| 07662ce9a8 | |||
| 50c5c33367 | |||
| 91c6f25dd1 | |||
| 15bbcc8f53 | |||
| 53f6697d4b | |||
| c9939fcf7f | |||
| a9953a4729 | |||
| 59941167ea |
@ -238,3 +238,48 @@ func Test_Gen_Service_PackagesFilter(t *testing.T) {
|
||||
t.Assert(files[0], dstFolder+filepath.FromSlash("/user.go"))
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/4242
|
||||
// Test that versioned imports and aliased imports are correctly preserved.
|
||||
// The issue is that imports like "github.com/minio/minio-go/v7" were being
|
||||
// incorrectly handled because the package name (minio) differs from
|
||||
// the directory name (minio-go).
|
||||
func Test_Issue4242(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
path = gfile.Temp(guid.S())
|
||||
dstFolder = path + filepath.FromSlash("/service")
|
||||
srvFolder = gtest.DataPath("issue", "4242", "logic")
|
||||
in = genservice.CGenServiceInput{
|
||||
SrcFolder: srvFolder,
|
||||
DstFolder: dstFolder,
|
||||
DstFileNameCase: "Snake",
|
||||
WatchFile: "",
|
||||
StPattern: "",
|
||||
Packages: nil,
|
||||
ImportPrefix: "",
|
||||
Clear: false,
|
||||
}
|
||||
)
|
||||
err := gutil.FillStructWithDefault(&in)
|
||||
t.AssertNil(err)
|
||||
|
||||
err = gfile.Mkdir(path)
|
||||
t.AssertNil(err)
|
||||
defer gfile.Remove(path)
|
||||
|
||||
_, err = genservice.CGenService{}.Service(ctx, in)
|
||||
t.AssertNil(err)
|
||||
|
||||
// Test versioned imports
|
||||
t.Assert(
|
||||
gfile.GetContents(dstFolder+filepath.FromSlash("/issue_4242.go")),
|
||||
gfile.GetContents(gtest.DataPath("issue", "4242", "service", "issue_4242.go")),
|
||||
)
|
||||
// Test aliased imports
|
||||
t.Assert(
|
||||
gfile.GetContents(dstFolder+filepath.FromSlash("/issue_4242_alias.go")),
|
||||
gfile.GetContents(gtest.DataPath("issue", "4242", "service", "issue_4242_alias.go")),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
|
||||
@ -37,21 +36,14 @@ func (c CGenService) calculateImportedItems(
|
||||
}
|
||||
|
||||
for _, item := range pkgItems {
|
||||
alias := item.Alias
|
||||
|
||||
// If the alias is _, it means that the package is not generated.
|
||||
if alias == "_" {
|
||||
// Skip anonymous imports
|
||||
if item.Alias == "_" {
|
||||
mlog.Debugf(`ignore anonymous package: %s`, item.RawImport)
|
||||
continue
|
||||
}
|
||||
// If the alias is empty, it will use the package name as the alias.
|
||||
if alias == "" {
|
||||
alias = gfile.Basename(gstr.Trim(item.Path, `"`))
|
||||
}
|
||||
if !gstr.Contains(allFuncParamType.String(), alias) {
|
||||
mlog.Debugf(`ignore unused package: %s`, item.RawImport)
|
||||
continue
|
||||
}
|
||||
// Keep all imports, let gofmt clean up unused ones.
|
||||
// We cannot accurately infer package name from import path
|
||||
// (e.g., path "minio-go" but package name is "minio").
|
||||
srcImportedPackages.Add(item.RawImport)
|
||||
}
|
||||
return nil
|
||||
|
||||
34
cmd/gf/internal/cmd/testdata/issue/4242/logic/issue4242/issue4242.go
vendored
Normal file
34
cmd/gf/internal/cmd/testdata/issue/4242/logic/issue4242/issue4242.go
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
package issue4242
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/service"
|
||||
|
||||
"github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterIssue4242(New())
|
||||
}
|
||||
|
||||
type sIssue4242 struct {
|
||||
}
|
||||
|
||||
func New() *sIssue4242 {
|
||||
return &sIssue4242{}
|
||||
}
|
||||
|
||||
// GetDriver tests versioned import path is preserved.
|
||||
func (s *sIssue4242) GetDriver(ctx context.Context) (d mysql.Driver, err error) {
|
||||
return mysql.Driver{}, nil
|
||||
}
|
||||
|
||||
// GetRequest tests another versioned import.
|
||||
func (s *sIssue4242) GetRequest(ctx context.Context) (*ghttp.Request, error) {
|
||||
g.Log().Info(ctx, "getting request")
|
||||
return nil, nil
|
||||
}
|
||||
37
cmd/gf/internal/cmd/testdata/issue/4242/logic/issue4242alias/issue4242alias.go
vendored
Normal file
37
cmd/gf/internal/cmd/testdata/issue/4242/logic/issue4242alias/issue4242alias.go
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
package issue4242alias
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
// Anonymous import (should be skipped)
|
||||
_ "github.com/gogf/gf/v2/os/gres"
|
||||
|
||||
// Versioned import without alias
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/service"
|
||||
|
||||
// Explicit alias import
|
||||
mysqlDriver "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterIssue4242Alias(New())
|
||||
}
|
||||
|
||||
type sIssue4242Alias struct {
|
||||
}
|
||||
|
||||
func New() *sIssue4242Alias {
|
||||
return &sIssue4242Alias{}
|
||||
}
|
||||
|
||||
// GetDriver tests explicit alias import.
|
||||
func (s *sIssue4242Alias) GetDriver(ctx context.Context) (d mysqlDriver.Driver, err error) {
|
||||
return mysqlDriver.Driver{}, nil
|
||||
}
|
||||
|
||||
// GetRequest tests versioned import.
|
||||
func (s *sIssue4242Alias) GetRequest(ctx context.Context) (*ghttp.Request, error) {
|
||||
return nil, nil
|
||||
}
|
||||
10
cmd/gf/internal/cmd/testdata/issue/4242/logic/logic.go
vendored
Normal file
10
cmd/gf/internal/cmd/testdata/issue/4242/logic/logic.go
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
// ==========================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/logic/issue4242"
|
||||
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/logic/issue4242alias"
|
||||
)
|
||||
37
cmd/gf/internal/cmd/testdata/issue/4242/service/issue_4242.go
vendored
Normal file
37
cmd/gf/internal/cmd/testdata/issue/4242/service/issue_4242.go
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
// ================================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
type (
|
||||
IIssue4242 interface {
|
||||
// GetDriver tests versioned import path is preserved.
|
||||
GetDriver(ctx context.Context) (d mysql.Driver, err error)
|
||||
// GetRequest tests another versioned import.
|
||||
GetRequest(ctx context.Context) (*ghttp.Request, error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
localIssue4242 IIssue4242
|
||||
)
|
||||
|
||||
func Issue4242() IIssue4242 {
|
||||
if localIssue4242 == nil {
|
||||
panic("implement not found for interface IIssue4242, forgot register?")
|
||||
}
|
||||
return localIssue4242
|
||||
}
|
||||
|
||||
func RegisterIssue4242(i IIssue4242) {
|
||||
localIssue4242 = i
|
||||
}
|
||||
37
cmd/gf/internal/cmd/testdata/issue/4242/service/issue_4242_alias.go
vendored
Normal file
37
cmd/gf/internal/cmd/testdata/issue/4242/service/issue_4242_alias.go
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
// ================================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
mysqlDriver "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
type (
|
||||
IIssue4242Alias interface {
|
||||
// GetDriver tests explicit alias import.
|
||||
GetDriver(ctx context.Context) (d mysqlDriver.Driver, err error)
|
||||
// GetRequest tests versioned import.
|
||||
GetRequest(ctx context.Context) (*ghttp.Request, error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
localIssue4242Alias IIssue4242Alias
|
||||
)
|
||||
|
||||
func Issue4242Alias() IIssue4242Alias {
|
||||
if localIssue4242Alias == nil {
|
||||
panic("implement not found for interface IIssue4242Alias, forgot register?")
|
||||
}
|
||||
return localIssue4242Alias
|
||||
}
|
||||
|
||||
func RegisterIssue4242Alias(i IIssue4242Alias) {
|
||||
localIssue4242Alias = i
|
||||
}
|
||||
@ -749,7 +749,9 @@ func (a *TArray[T]) String() string {
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// Note that do not use pointer as its receiver here.
|
||||
// DO NOT change this receiver to pointer type, as the TArray can be used as a var defined variable, like:
|
||||
// var a TArray[int]
|
||||
// Please refer to corresponding tests for more details.
|
||||
func (a TArray[T]) MarshalJSON() ([]byte, error) {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
|
||||
@ -46,7 +46,7 @@ func NewSortedTArray[T comparable](comparator func(a, b T) int, safe ...bool) *S
|
||||
return NewSortedTArraySize(0, comparator, safe...)
|
||||
}
|
||||
|
||||
// NewSortedTArraySize create and returns an sorted array with given size and cap.
|
||||
// NewSortedTArraySize create and returns a sorted array with given size and cap.
|
||||
// The parameter `safe` is used to specify whether using array in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewSortedTArraySize[T comparable](cap int, comparator func(a, b T) int, safe ...bool) *SortedTArray[T] {
|
||||
@ -718,7 +718,9 @@ func (a *SortedTArray[T]) String() string {
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// Note that do not use pointer as its receiver here.
|
||||
// DO NOT change this receiver to pointer type, as the TArray can be used as a var defined variable, like:
|
||||
// var a SortedTArray[int]
|
||||
// Please refer to corresponding tests for more details.
|
||||
func (a SortedTArray[T]) MarshalJSON() ([]byte, error) {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
|
||||
@ -17,10 +17,17 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// NilChecker is a function that checks whether the given value is nil.
|
||||
type NilChecker[V any] func(V) bool
|
||||
|
||||
// KVMap wraps map type `map[K]V` and provides more map features.
|
||||
type KVMap[K comparable, V any] struct {
|
||||
mu rwmutex.RWMutex
|
||||
data map[K]V
|
||||
|
||||
// nilChecker is the custom nil checker function.
|
||||
// It uses empty.IsNil if it's nil.
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
// NewKVMap creates and returns an empty hash map.
|
||||
@ -29,6 +36,13 @@ func NewKVMap[K comparable, V any](safe ...bool) *KVMap[K, V] {
|
||||
return NewKVMapFrom(make(map[K]V), safe...)
|
||||
}
|
||||
|
||||
// NewKVMapWithChecker creates and returns an empty hash map with a custom nil checker.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode, which is false by default.
|
||||
func NewKVMapWithChecker[K comparable, V any](checker NilChecker[V], safe ...bool) *KVMap[K, V] {
|
||||
return NewKVMapWithCheckerFrom(make(map[K]V), checker, safe...)
|
||||
}
|
||||
|
||||
// NewKVMapFrom 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),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
@ -40,6 +54,37 @@ func NewKVMapFrom[K comparable, V any](data map[K]V, safe ...bool) *KVMap[K, V]
|
||||
return m
|
||||
}
|
||||
|
||||
// NewKVMapWithCheckerFrom creates and returns a hash map from given map `data` with a custom nil checker.
|
||||
// Note that, the param `data` map will be set as the underlying data map (no deep copy),
|
||||
// and there might be some concurrent-safe issues when changing the map outside.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode, which is false by default.
|
||||
func NewKVMapWithCheckerFrom[K comparable, V any](data map[K]V, checker NilChecker[V], safe ...bool) *KVMap[K, V] {
|
||||
m := NewKVMapFrom[K, V](data, safe...)
|
||||
m.SetNilChecker(checker)
|
||||
return m
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (m *KVMap[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (m *KVMap[K, V]) isNil(v V) bool {
|
||||
if m.nilChecker != nil {
|
||||
return m.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *KVMap[K, V]) Iterator(f func(k K, v V) bool) {
|
||||
@ -200,11 +245,12 @@ func (m *KVMap[K, V]) Pops(size int) map[K]V {
|
||||
return newMap
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
// doSetWithLockCheck sets value with given `value` if it does not exist,
|
||||
// and then returns this value and whether it exists.
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
// It is a helper function for GetOrSet* functions.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the given `value` is nil.
|
||||
func (m *KVMap[K, V]) doSetWithLockCheck(key K, value V) (val V, ok bool) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -216,7 +262,9 @@ func (m *KVMap[K, V]) doSetWithLockCheck(key K, value V) (val V, ok bool) {
|
||||
if v, ok := m.data[key]; ok {
|
||||
return v, true
|
||||
}
|
||||
m.data[key] = value
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = value
|
||||
}
|
||||
return value, false
|
||||
}
|
||||
|
||||
@ -230,6 +278,8 @@ func (m *KVMap[K, V]) GetOrSet(key K, value V) V {
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the returned value of `f` is nil.
|
||||
func (m *KVMap[K, V]) GetOrSetFunc(key K, f func() V) V {
|
||||
v, _ := m.doSetWithLockCheck(key, f())
|
||||
return v
|
||||
@ -241,6 +291,8 @@ func (m *KVMap[K, V]) GetOrSetFunc(key K, f func() V) V {
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the returned value of `f` is nil.
|
||||
func (m *KVMap[K, V]) GetOrSetFuncLock(key K, f func() V) V {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -251,7 +303,9 @@ func (m *KVMap[K, V]) GetOrSetFuncLock(key K, f func() V) V {
|
||||
return v
|
||||
}
|
||||
value := f()
|
||||
m.data[key] = value
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = value
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
@ -478,6 +532,9 @@ func (m *KVMap[K, V]) String() string {
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// DO NOT change this receiver to pointer type, as the KVMap can be used as a var defined variable, like:
|
||||
// var m gmap.KVMap[int, string]
|
||||
// Please refer to corresponding tests for more details.
|
||||
func (m KVMap[K, V]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(gconv.Map(m.Map()))
|
||||
}
|
||||
|
||||
@ -27,9 +27,10 @@ import (
|
||||
//
|
||||
// Reference: http://en.wikipedia.org/wiki/Associative_array
|
||||
type ListKVMap[K comparable, V any] struct {
|
||||
mu rwmutex.RWMutex
|
||||
data map[K]*glist.TElement[*gListKVMapNode[K, V]]
|
||||
list *glist.TList[*gListKVMapNode[K, V]]
|
||||
mu rwmutex.RWMutex
|
||||
data map[K]*glist.TElement[*gListKVMapNode[K, V]]
|
||||
list *glist.TList[*gListKVMapNode[K, V]]
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
type gListKVMapNode[K comparable, V any] struct {
|
||||
@ -49,6 +50,16 @@ func NewListKVMap[K comparable, V any](safe ...bool) *ListKVMap[K, V] {
|
||||
}
|
||||
}
|
||||
|
||||
// NewListKVMapWithChecker creates and returns a new ListKVMap instance with a custom nil checker.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false by default.
|
||||
func NewListKVMapWithChecker[K comparable, V any](checker NilChecker[V], safe ...bool) *ListKVMap[K, V] {
|
||||
m := NewListKVMap[K, V](safe...)
|
||||
m.SetNilChecker(checker)
|
||||
return m
|
||||
}
|
||||
|
||||
// NewListKVMapFrom returns a link map from given map `data`.
|
||||
// Note that, the param `data` map will be copied to the underlying data structure,
|
||||
// so changes to the original map will not affect the link map.
|
||||
@ -58,6 +69,38 @@ func NewListKVMapFrom[K comparable, V any](data map[K]V, safe ...bool) *ListKVMa
|
||||
return m
|
||||
}
|
||||
|
||||
// NewListKVMapWithCheckerFrom returns a link map from given map `data` with a custom nil checker.
|
||||
// Note that, the param `data` map will be copied to the underlying data structure,
|
||||
// so changes to the original map will not affect the link map.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false by default.
|
||||
func NewListKVMapWithCheckerFrom[K comparable, V any](data map[K]V, nilChecker NilChecker[V], safe ...bool) *ListKVMap[K, V] {
|
||||
m := NewListKVMapWithChecker[K, V](nilChecker, safe...)
|
||||
m.Sets(data)
|
||||
return m
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (m *ListKVMap[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (m *ListKVMap[K, V]) isNil(v V) bool {
|
||||
if m.nilChecker != nil {
|
||||
return m.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Iterator is alias of IteratorAsc.
|
||||
func (m *ListKVMap[K, V]) Iterator(f func(key K, value V) bool) {
|
||||
m.IteratorAsc(f)
|
||||
@ -282,7 +325,9 @@ func (m *ListKVMap[K, V]) doSetWithLockCheckWithoutLock(key K, value V) V {
|
||||
if e, ok := m.data[key]; ok {
|
||||
return e.Value.value
|
||||
}
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
@ -325,7 +370,9 @@ func (m *ListKVMap[K, V]) GetOrSetFuncLock(key K, f func() V) V {
|
||||
return e.Value.value
|
||||
}
|
||||
value := f()
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
@ -355,6 +402,8 @@ func (m *ListKVMap[K, V]) GetVarOrSetFuncLock(key K, f func() V) *gvar.Var {
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Note that it does not add the value to the map if `value` is nil.
|
||||
func (m *ListKVMap[K, V]) SetIfNotExist(key K, value V) bool {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -366,12 +415,16 @@ func (m *ListKVMap[K, V]) SetIfNotExist(key K, value V) bool {
|
||||
if _, ok := m.data[key]; ok {
|
||||
return false
|
||||
}
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the returned value of `f` is nil.
|
||||
func (m *ListKVMap[K, V]) SetIfNotExistFunc(key K, f func() V) bool {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -384,7 +437,9 @@ func (m *ListKVMap[K, V]) SetIfNotExistFunc(key K, f func() V) bool {
|
||||
return false
|
||||
}
|
||||
value := f()
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@ -393,6 +448,8 @@ func (m *ListKVMap[K, V]) SetIfNotExistFunc(key K, f func() V) bool {
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the map.
|
||||
//
|
||||
// Note that, it does not add the value to the map if the returned value of `f` is nil.
|
||||
func (m *ListKVMap[K, V]) SetIfNotExistFuncLock(key K, f func() V) bool {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -405,7 +462,9 @@ func (m *ListKVMap[K, V]) SetIfNotExistFuncLock(key K, f func() V) bool {
|
||||
return false
|
||||
}
|
||||
value := f()
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
if !m.isNil(value) {
|
||||
m.data[key] = m.list.PushBack(&gListKVMapNode[K, V]{key, value})
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@ -556,6 +615,9 @@ func (m *ListKVMap[K, V]) String() string {
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// DO NOT change this receiver to pointer type, as the ListKVMap can be used as a var defined variable, like:
|
||||
// var m gmap.ListKVMap[string]string
|
||||
// Please refer to corresponding tests for more details.
|
||||
func (m ListKVMap[K, V]) MarshalJSON() (jsonBytes []byte, err error) {
|
||||
if m.data == nil {
|
||||
return []byte("{}"), nil
|
||||
|
||||
@ -898,7 +898,7 @@ func Test_KVMap_GetOrSet_NilValue(t *testing.T) {
|
||||
v := m.GetOrSet("a", nil)
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), true)
|
||||
t.Assert(m.Contains("a"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -910,7 +910,7 @@ func Test_KVMap_GetOrSetFunc_NilValue(t *testing.T) {
|
||||
v := m.GetOrSetFunc("a", func() any { return nil })
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), true)
|
||||
t.Assert(m.Contains("a"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -929,7 +929,7 @@ func Test_KVMap_GetOrSetFuncLock_NilData(t *testing.T) {
|
||||
v := m.GetOrSetFuncLock("a", func() any { return nil })
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), true)
|
||||
t.Assert(m.Contains("a"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1637,3 +1637,69 @@ func Test_KVMap_Flip_String(t *testing.T) {
|
||||
t.Assert(m.Get("val2"), "key2")
|
||||
})
|
||||
}
|
||||
|
||||
// Test TypedNil with custom nil checker for pointers
|
||||
func Test_KVMap_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
m1 := gmap.NewKVMap[int, *Student](true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m1.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m1.Size(), 5)
|
||||
|
||||
m2 := gmap.NewKVMap[int, *Student](true)
|
||||
m2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
m2.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewKVMapWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
m1 := gmap.NewKVMap[int, *Student](true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m1.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m1.Size(), 5)
|
||||
|
||||
m2 := gmap.NewKVMapWithChecker[int, *Student](func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m2.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
@ -183,77 +183,6 @@ func Test_ListKVMap_SetIfNotExistFuncLock_MultipleKeys(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Test_ListKVMap_GetOrSetFuncLock_NilValue tests that nil values are handled correctly.
|
||||
func Test_ListKVMap_GetOrSetFuncLock_NilValue(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListKVMap[string, *int](true)
|
||||
key := "nilKey"
|
||||
callCount := int32(0)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
goroutines := 50
|
||||
wg.Add(goroutines)
|
||||
|
||||
for i := 0; i < goroutines; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
m.GetOrSetFuncLock(key, func() *int {
|
||||
atomic.AddInt32(&callCount, 1)
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Callback should be called once
|
||||
t.Assert(atomic.LoadInt32(&callCount), 1)
|
||||
// Typed nil pointer (*int)(nil) is stored because any(value) != nil for typed nil
|
||||
// This is a Go language feature: typed nil is not the same as interface nil
|
||||
t.Assert(m.Contains(key), true)
|
||||
t.Assert(m.Get(key), (*int)(nil))
|
||||
t.Assert(m.Size(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_ListKVMap_SetIfNotExistFuncLock_NilValue tests that nil values are handled correctly.
|
||||
func Test_ListKVMap_SetIfNotExistFuncLock_NilValue(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewListKVMap[string, *string](true)
|
||||
key := "nilKey"
|
||||
callCount := int32(0)
|
||||
successCount := int32(0)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
goroutines := 50
|
||||
wg.Add(goroutines)
|
||||
|
||||
for i := 0; i < goroutines; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
success := m.SetIfNotExistFuncLock(key, func() *string {
|
||||
atomic.AddInt32(&callCount, 1)
|
||||
return nil
|
||||
})
|
||||
if success {
|
||||
atomic.AddInt32(&successCount, 1)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Callback should be called once
|
||||
t.Assert(atomic.LoadInt32(&callCount), 1)
|
||||
// Should report success once
|
||||
t.Assert(atomic.LoadInt32(&successCount), 1)
|
||||
// Typed nil pointer (*string)(nil) is stored because any(value) != nil for typed nil
|
||||
t.Assert(m.Contains(key), true)
|
||||
t.Assert(m.Get(key), (*string)(nil))
|
||||
t.Assert(m.Size(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
// Test_ListKVMap_GetOrSetFuncLock_ExistingKey tests behavior when key already exists.
|
||||
func Test_ListKVMap_GetOrSetFuncLock_ExistingKey(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
|
||||
@ -817,7 +817,7 @@ func Test_ListKVMap_GetOrSet_NilValue(t *testing.T) {
|
||||
v := m.GetOrSet("a", nil)
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), true)
|
||||
t.Assert(m.Contains("a"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1159,6 +1159,13 @@ func Test_ListKVMap_MarshalJSON_Error(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(string(b), `{"a":"1"}`)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var m gmap.ListKVMap[int, int]
|
||||
m.Set(1, 10)
|
||||
b, err := json.Marshal(m)
|
||||
t.AssertNil(err)
|
||||
t.Assert(string(b), `{"1":10}`)
|
||||
})
|
||||
}
|
||||
|
||||
// Test empty map operations
|
||||
@ -1292,7 +1299,7 @@ func Test_ListKVMap_GetOrSetFuncLock_NilData(t *testing.T) {
|
||||
v := m.GetOrSetFuncLock("a", func() any { return nil })
|
||||
t.Assert(v, nil)
|
||||
// nil interface value should not be stored
|
||||
t.Assert(m.Contains("a"), true)
|
||||
t.Assert(m.Contains("a"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1341,3 +1348,69 @@ func Test_ListKVMap_UnmarshalValue_NilData(t *testing.T) {
|
||||
t.Assert(m.Get("b"), "2")
|
||||
})
|
||||
}
|
||||
|
||||
// Test typed nil values
|
||||
func Test_ListKVMap_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
m1 := gmap.NewListKVMap[int, *Student](true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m1.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m1.Size(), 5)
|
||||
|
||||
m2 := gmap.NewListKVMap[int, *Student](true)
|
||||
m2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
m2.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewListKVMapWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
m1 := gmap.NewListKVMap[int, *Student](true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m1.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m1.Size(), 5)
|
||||
|
||||
m2 := gmap.NewListKVMapWithChecker[int, *Student](func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
for i := 0; i < 10; i++ {
|
||||
m2.GetOrSetFuncLock(i, func() *Student {
|
||||
if i%2 == 0 {
|
||||
return &Student{}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
t.Assert(m2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ func (set *Set) AddIfNotExistFunc(item any, f func() bool) bool {
|
||||
}
|
||||
|
||||
// AddIfNotExistFuncLock checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exist in the set and
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if `item` is nil, it does nothing and returns false. The function `f`
|
||||
|
||||
@ -9,16 +9,21 @@ package gset
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// TSet is a generic set implementation that holds unique items of type T.
|
||||
// NilChecker is a function that checks whether the given value is nil.
|
||||
type NilChecker[T any] func(T) bool
|
||||
|
||||
// TSet[T] is consisted of any items.
|
||||
type TSet[T comparable] struct {
|
||||
mu rwmutex.RWMutex
|
||||
data map[T]struct{}
|
||||
mu rwmutex.RWMutex
|
||||
data map[T]struct{}
|
||||
nilChecker NilChecker[T]
|
||||
}
|
||||
|
||||
// NewTSet creates and returns a new set, which contains un-repeated items.
|
||||
@ -30,6 +35,15 @@ func NewTSet[T comparable](safe ...bool) *TSet[T] {
|
||||
}
|
||||
}
|
||||
|
||||
// NewTSetWithChecker creates and returns a new set with a custom nil checker.
|
||||
// The parameter `nilChecker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety mode.
|
||||
func NewTSetWithChecker[T comparable](checker NilChecker[T], safe ...bool) *TSet[T] {
|
||||
s := NewTSet[T](safe...)
|
||||
s.SetNilChecker(checker)
|
||||
return s
|
||||
}
|
||||
|
||||
// NewTSetFrom returns a new set from `items`.
|
||||
// `items` - A slice of type T.
|
||||
func NewTSetFrom[T comparable](items []T, safe ...bool) *TSet[T] {
|
||||
@ -43,6 +57,36 @@ func NewTSetFrom[T comparable](items []T, safe ...bool) *TSet[T] {
|
||||
}
|
||||
}
|
||||
|
||||
// NewTSetWithCheckerFrom returns a new set from `items` with a custom nil checker.
|
||||
// The parameter `items` is a slice of elements to be added to the set.
|
||||
// The parameter `checker` is a function used to determine if a value is nil.
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety mode.
|
||||
func NewTSetWithCheckerFrom[T comparable](items []T, checker NilChecker[T], safe ...bool) *TSet[T] {
|
||||
set := NewTSetWithChecker[T](checker, safe...)
|
||||
set.Add(items...)
|
||||
return set
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the set elements.
|
||||
// This function is used to determine if an element should be considered as nil.
|
||||
// The nil checker function takes an element of type T and returns a boolean indicating
|
||||
// whether the element should be treated as nil.
|
||||
func (set *TSet[T]) SetNilChecker(nilChecker NilChecker[T]) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
set.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (set *TSet[T]) isNil(v T) bool {
|
||||
if set.nilChecker != nil {
|
||||
return set.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func (set *TSet[T]) Iterator(f func(v T) bool) {
|
||||
@ -66,11 +110,14 @@ func (set *TSet[T]) Add(items ...T) {
|
||||
}
|
||||
|
||||
// AddIfNotExist checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set,
|
||||
// it adds the item to set and returns true if it does not exist in the set,
|
||||
// or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if `item` is nil, it does nothing and returns false.
|
||||
func (set *TSet[T]) AddIfNotExist(item T) bool {
|
||||
if set.isNil(item) {
|
||||
return false
|
||||
}
|
||||
if !set.Contains(item) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
@ -92,6 +139,9 @@ func (set *TSet[T]) AddIfNotExist(item T) bool {
|
||||
// Note that, if `item` is nil, it does nothing and returns false. The function `f`
|
||||
// is executed without writing lock.
|
||||
func (set *TSet[T]) AddIfNotExistFunc(item T, f func() bool) bool {
|
||||
if set.isNil(item) {
|
||||
return false
|
||||
}
|
||||
if !set.Contains(item) {
|
||||
if f() {
|
||||
set.mu.Lock()
|
||||
@ -109,12 +159,15 @@ func (set *TSet[T]) AddIfNotExistFunc(item T, f func() bool) bool {
|
||||
}
|
||||
|
||||
// AddIfNotExistFuncLock checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exist in the set and
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if `item` is nil, it does nothing and returns false. The function `f`
|
||||
// is executed within writing lock.
|
||||
func (set *TSet[T]) AddIfNotExistFuncLock(item T, f func() bool) bool {
|
||||
if set.isNil(item) {
|
||||
return false
|
||||
}
|
||||
if !set.Contains(item) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
|
||||
@ -419,7 +419,6 @@ func TestSet_AddIfNotExist(t *testing.T) {
|
||||
t.Assert(s.AddIfNotExist(2), true)
|
||||
t.Assert(s.Contains(2), true)
|
||||
t.Assert(s.AddIfNotExist(2), false)
|
||||
t.Assert(s.AddIfNotExist(nil), true)
|
||||
t.Assert(s.AddIfNotExist(nil), false)
|
||||
t.Assert(s.Contains(2), true)
|
||||
})
|
||||
@ -498,18 +497,7 @@ func TestSet_AddIfNotExistFuncLock(t *testing.T) {
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s := gset.New(true)
|
||||
t.Assert(
|
||||
s.AddIfNotExistFuncLock(nil, func() bool {
|
||||
return true
|
||||
}),
|
||||
true,
|
||||
)
|
||||
t.Assert(
|
||||
s.AddIfNotExistFuncLock(nil, func() bool {
|
||||
return true
|
||||
}),
|
||||
false,
|
||||
)
|
||||
t.Assert(s.AddIfNotExistFuncLock(nil, func() bool { return true }), false)
|
||||
s1 := gset.Set{}
|
||||
t.Assert(s1.AddIfNotExistFuncLock(1, func() bool { return true }), true)
|
||||
})
|
||||
|
||||
@ -591,3 +591,42 @@ func TestTSet_RLockFunc(t *testing.T) {
|
||||
t.Assert(sum, 6)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TSet_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
set := gset.NewTSet[*Student](true)
|
||||
var s *Student = nil
|
||||
exist := set.AddIfNotExist(s)
|
||||
t.Assert(exist, false)
|
||||
|
||||
set2 := gset.NewTSet[*Student](true)
|
||||
set2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
exist2 := set2.AddIfNotExist(s)
|
||||
t.Assert(exist2, false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewTSetWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
set := gset.NewTSet[*Student](true)
|
||||
var s *Student = nil
|
||||
exist := set.AddIfNotExist(s)
|
||||
t.Assert(exist, false)
|
||||
|
||||
set2 := gset.NewTSetWithChecker[*Student](func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
exist2 := set2.AddIfNotExist(s)
|
||||
t.Assert(exist2, false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -12,17 +12,22 @@ import (
|
||||
"github.com/emirpasic/gods/v2/trees/avltree"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// NilChecker is a function that checks whether the given value is nil.
|
||||
type NilChecker[V any] func(V) bool
|
||||
|
||||
// AVLKVTree holds elements of the AVL tree.
|
||||
type AVLKVTree[K comparable, V any] struct {
|
||||
mu rwmutex.RWMutex
|
||||
comparator func(v1, v2 K) int
|
||||
tree *avltree.Tree[K, V]
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
// AVLKVTreeNode is a single element within the tree.
|
||||
@ -43,6 +48,15 @@ func NewAVLKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe ...bo
|
||||
}
|
||||
}
|
||||
|
||||
// NewAVLKVTreeWithChecker instantiates an AVL tree with the custom key comparator and nil checker.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewAVLKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V] {
|
||||
t := NewAVLKVTree[K, V](comparator, safe...)
|
||||
t.SetNilChecker(checker)
|
||||
return t
|
||||
}
|
||||
|
||||
// NewAVLKVTreeFrom instantiates an AVL tree with the custom key comparator and data map.
|
||||
//
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
@ -54,6 +68,37 @@ func NewAVLKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, data m
|
||||
return tree
|
||||
}
|
||||
|
||||
// NewAVLKVTreeWithCheckerFrom instantiates an AVL tree with the custom key comparator, nil checker and data map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewAVLKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *AVLKVTree[K, V] {
|
||||
tree := NewAVLKVTreeWithChecker[K, V](comparator, checker, safe...)
|
||||
for k, v := range data {
|
||||
tree.doSet(k, v)
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (tree *AVLKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
tree.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (tree *AVLKVTree[K, V]) isNil(v V) bool {
|
||||
if tree.nilChecker != nil {
|
||||
return tree.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Clone clones and returns a new tree from current tree.
|
||||
func (tree *AVLKVTree[K, V]) Clone() *AVLKVTree[K, V] {
|
||||
if tree == nil {
|
||||
@ -518,6 +563,9 @@ func (tree *AVLKVTree[K, V]) Flip(comparator ...func(v1, v2 K) int) {
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
func (tree *AVLKVTree[K, V]) doSet(key K, value V) V {
|
||||
if tree.isNil(value) {
|
||||
return value
|
||||
}
|
||||
tree.tree.Put(key, value)
|
||||
return value
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"github.com/emirpasic/gods/v2/trees/btree"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
@ -24,6 +25,7 @@ type BKVTree[K comparable, V any] struct {
|
||||
comparator func(v1, v2 K) int
|
||||
m int // order (maximum number of children)
|
||||
tree *btree.Tree[K, V]
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
// BKVTreeEntry represents the key-value pair contained within nodes.
|
||||
@ -45,6 +47,15 @@ func NewBKVTree[K comparable, V any](m int, comparator func(v1, v2 K) int, safe
|
||||
}
|
||||
}
|
||||
|
||||
// NewBKVTreeWithChecker instantiates a B-tree with `m` (maximum number of children), a custom key comparator and nil checker.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewBKVTreeWithChecker[K comparable, V any](m int, comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *BKVTree[K, V] {
|
||||
t := NewBKVTree[K, V](m, comparator, safe...)
|
||||
t.SetNilChecker(checker)
|
||||
return t
|
||||
}
|
||||
|
||||
// NewBKVTreeFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator and data map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
@ -56,6 +67,37 @@ func NewBKVTreeFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, d
|
||||
return tree
|
||||
}
|
||||
|
||||
// NewBKVTreeWithCheckerFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator, nil checker and data map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewBKVTreeWithCheckerFrom[K comparable, V any](m int, comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *BKVTree[K, V] {
|
||||
tree := NewBKVTreeWithChecker[K, V](m, comparator, checker, safe...)
|
||||
for k, v := range data {
|
||||
tree.doSet(k, v)
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (tree *BKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
tree.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (tree *BKVTree[K, V]) isNil(v V) bool {
|
||||
if tree.nilChecker != nil {
|
||||
return tree.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// Clone clones and returns a new tree from current tree.
|
||||
func (tree *BKVTree[K, V]) Clone() *BKVTree[K, V] {
|
||||
if tree == nil {
|
||||
@ -453,6 +495,9 @@ func (tree *BKVTree[K, V]) Right() *BKVTreeEntry[K, V] {
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
func (tree *BKVTree[K, V]) doSet(key K, value V) V {
|
||||
if tree.isNil(value) {
|
||||
return value
|
||||
}
|
||||
tree.tree.Put(key, value)
|
||||
return value
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"github.com/emirpasic/gods/v2/trees/redblacktree"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/internal/rwmutex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
@ -24,6 +25,7 @@ type RedBlackKVTree[K comparable, V any] struct {
|
||||
mu rwmutex.RWMutex
|
||||
comparator func(v1, v2 K) int
|
||||
tree *redblacktree.Tree[K, V]
|
||||
nilChecker NilChecker[V]
|
||||
}
|
||||
|
||||
// RedBlackKVTreeNode is a single element within the tree.
|
||||
@ -41,6 +43,15 @@ func NewRedBlackKVTree[K comparable, V any](comparator func(v1, v2 K) int, safe
|
||||
return &tree
|
||||
}
|
||||
|
||||
// NewRedBlackKVTreeWithChecker instantiates a red-black tree with the custom key comparator and `nilChecker`.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewRedBlackKVTreeWithChecker[K comparable, V any](comparator func(v1, v2 K) int, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V] {
|
||||
t := NewRedBlackKVTree[K, V](comparator, safe...)
|
||||
t.SetNilChecker(checker)
|
||||
return t
|
||||
}
|
||||
|
||||
// NewRedBlackKVTreeFrom instantiates a red-black tree with the custom key comparator and `data` map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
@ -50,6 +61,17 @@ func NewRedBlackKVTreeFrom[K comparable, V any](comparator func(v1, v2 K) int, d
|
||||
return &tree
|
||||
}
|
||||
|
||||
// NewRedBlackKVTreeWithCheckerFrom instantiates a red-black tree with the custom key comparator, `data` map and `nilChecker`.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.
|
||||
// The parameter `checker` is used to specify whether the given value is nil.
|
||||
func NewRedBlackKVTreeWithCheckerFrom[K comparable, V any](comparator func(v1, v2 K) int, data map[K]V, checker NilChecker[V], safe ...bool) *RedBlackKVTree[K, V] {
|
||||
t := NewRedBlackKVTreeWithChecker[K, V](comparator, checker, safe...)
|
||||
for k, v := range data {
|
||||
t.doSet(k, v)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// RedBlackKVTreeInit instantiates a red-black tree with the custom key comparator.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
@ -75,6 +97,26 @@ func RedBlackKVTreeInitFrom[K comparable, V any](tree *RedBlackKVTree[K, V], com
|
||||
}
|
||||
}
|
||||
|
||||
// SetNilChecker registers a custom nil checker function for the map values.
|
||||
// This function is used to determine if a value should be considered as nil.
|
||||
// The nil checker function takes a value of type V and returns a boolean indicating
|
||||
// whether the value should be treated as nil.
|
||||
func (tree *RedBlackKVTree[K, V]) SetNilChecker(nilChecker NilChecker[V]) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
tree.nilChecker = nilChecker
|
||||
}
|
||||
|
||||
// isNil checks whether the given value is nil.
|
||||
// It first checks if a custom nil checker function is registered and uses it if available,
|
||||
// otherwise it falls back to the default empty.IsNil function.
|
||||
func (tree *RedBlackKVTree[K, V]) isNil(v V) bool {
|
||||
if tree.nilChecker != nil {
|
||||
return tree.nilChecker(v)
|
||||
}
|
||||
return empty.IsNil(v)
|
||||
}
|
||||
|
||||
// SetComparator sets/changes the comparator for sorting.
|
||||
func (tree *RedBlackKVTree[K, V]) SetComparator(comparator func(a, b K) int) {
|
||||
tree.comparator = comparator
|
||||
@ -592,6 +634,9 @@ func (tree *RedBlackKVTree[K, V]) UnmarshalValue(value any) (err error) {
|
||||
//
|
||||
// It returns value with given `key`.
|
||||
func (tree *RedBlackKVTree[K, V]) doSet(key K, value V) (ret V) {
|
||||
if tree.isNil(value) {
|
||||
return
|
||||
}
|
||||
tree.tree.Put(key, value)
|
||||
return value
|
||||
}
|
||||
|
||||
215
container/gtree/gtree_z_k_v_tree_test.go
Normal file
215
container/gtree/gtree_z_k_v_tree_test.go
Normal file
@ -0,0 +1,215 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gtree_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gtree"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
)
|
||||
|
||||
func Test_KVAVLTree_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
avlTree := gtree.NewAVLKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
avlTree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
avlTree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(avlTree.Size(), 5)
|
||||
|
||||
avlTree2 := gtree.NewAVLKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
avlTree2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
avlTree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
avlTree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(avlTree2.Size(), 5)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_KVBTree_TypedNil(t *testing.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
btree := gtree.NewBKVTree[int, *Student](100, gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
btree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
btree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(btree.Size(), 5)
|
||||
|
||||
btree2 := gtree.NewBKVTree[int, *Student](100, gutil.ComparatorTStr[int], true)
|
||||
btree2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
btree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
btree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(btree2.Size(), 5)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_KVRedBlackTree_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
redBlackTree := gtree.NewRedBlackKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
redBlackTree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
redBlackTree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(redBlackTree.Size(), 5)
|
||||
|
||||
redBlackTree2 := gtree.NewRedBlackKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
redBlackTree2.SetNilChecker(func(student *Student) bool {
|
||||
return student == nil
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
redBlackTree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
redBlackTree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(redBlackTree2.Size(), 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewKVAVLTreeWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
avlTree := gtree.NewAVLKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
avlTree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
avlTree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(avlTree.Size(), 5)
|
||||
|
||||
avlTree2 := gtree.NewAVLKVTreeWithChecker[int, *Student](gutil.ComparatorTStr[int], func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
avlTree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
avlTree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(avlTree2.Size(), 5)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewKVBTreeWithChecker_TypedNil(t *testing.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
btree := gtree.NewBKVTree[int, *Student](100, gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
btree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
btree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(btree.Size(), 5)
|
||||
|
||||
btree2 := gtree.NewBKVTreeWithChecker[int, *Student](100, gutil.ComparatorTStr[int], func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
btree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
btree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(btree2.Size(), 5)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_NewRedBlackKVTreeWithChecker_TypedNil(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
redBlackTree := gtree.NewRedBlackKVTree[int, *Student](gutil.ComparatorTStr[int], true)
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
redBlackTree.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
redBlackTree.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(redBlackTree.Size(), 5)
|
||||
|
||||
redBlackTree2 := gtree.NewRedBlackKVTreeWithChecker[int, *Student](gutil.ComparatorTStr[int], func(student *Student) bool {
|
||||
return student == nil
|
||||
}, true)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
if i%2 == 0 {
|
||||
redBlackTree2.Set(i, &Student{})
|
||||
} else {
|
||||
var s *Student = nil
|
||||
redBlackTree2.Set(i, s)
|
||||
}
|
||||
}
|
||||
t.Assert(redBlackTree2.Size(), 5)
|
||||
})
|
||||
}
|
||||
@ -867,8 +867,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// checker is the checker function for instances map.
|
||||
checker = func(v DB) bool { return v == nil }
|
||||
// instances is the management map for instances.
|
||||
instances = gmap.NewKVMap[string, DB](true)
|
||||
instances = gmap.NewKVMapWithChecker[string, DB](checker, true)
|
||||
|
||||
// driverMap manages all custom registered driver.
|
||||
driverMap = map[string]Driver{}
|
||||
@ -942,6 +944,9 @@ func NewByGroup(group ...string) (db DB, err error) {
|
||||
)
|
||||
}
|
||||
|
||||
// linksChecker is the checker function for links map.
|
||||
var linksChecker = func(v *sql.DB) bool { return v == nil }
|
||||
|
||||
// newDBByConfigNode creates and returns an ORM object with given configuration node and group name.
|
||||
//
|
||||
// Very Note:
|
||||
@ -958,7 +963,7 @@ func newDBByConfigNode(node *ConfigNode, group string) (db DB, err error) {
|
||||
group: group,
|
||||
debug: gtype.NewBool(),
|
||||
cache: gcache.New(),
|
||||
links: gmap.NewKVMap[ConfigNode, *sql.DB](true),
|
||||
links: gmap.NewKVMapWithChecker[ConfigNode, *sql.DB](linksChecker, true),
|
||||
logger: glog.New(),
|
||||
config: node,
|
||||
localTypeMap: gmap.NewStrAnyMap(true),
|
||||
|
||||
@ -50,8 +50,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// configChecker checks whether the *Config is nil.
|
||||
configChecker = func(v *Config) bool { return v == nil }
|
||||
// Configuration groups.
|
||||
localConfigMap = gmap.NewKVMap[string, *Config](true)
|
||||
localConfigMap = gmap.NewKVMapWithChecker[string, *Config](configChecker, true)
|
||||
)
|
||||
|
||||
// SetConfig sets the global configuration for specified group.
|
||||
|
||||
@ -14,7 +14,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
localInstances = gmap.NewKVMap[string, *Redis](true)
|
||||
// checker is the checker function for instances map.
|
||||
checker = func(v *Redis) bool { return v == nil }
|
||||
localInstances = gmap.NewKVMapWithChecker[string, *Redis](checker, true)
|
||||
)
|
||||
|
||||
// Instance returns an instance of redis client with specified group.
|
||||
|
||||
1
go.sum
1
go.sum
@ -4,7 +4,6 @@ github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyM
|
||||
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/emirpasic/gods/v2 v2.0.0-alpha h1:dwFlh8pBg1VMOXWGipNMRt8v96dKAIvBehtCt6OtunU=
|
||||
github.com/emirpasic/gods/v2 v2.0.0-alpha/go.mod h1:W0y4M2dtBB9U5z3YlghmpuUhiaZT2h6yoeE+C1sCp6A=
|
||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||
|
||||
@ -14,9 +14,11 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// checker is used for checking whether the value is nil.
|
||||
checker = func(v *Manager) bool { return v == nil }
|
||||
// instances is the instances map for management
|
||||
// for multiple i18n instance by name.
|
||||
instances = gmap.NewKVMap[string, *Manager](true)
|
||||
instances = gmap.NewKVMapWithChecker[string, *Manager](checker, true)
|
||||
)
|
||||
|
||||
// Instance returns an instance of Resource.
|
||||
|
||||
@ -176,9 +176,12 @@ var (
|
||||
// It is used for quick HTTP method searching using map.
|
||||
methodsMap = make(map[string]struct{})
|
||||
|
||||
// checker is used for checking whether the value is nil.
|
||||
checker = func(v *Server) bool { return v == nil }
|
||||
|
||||
// serverMapping stores more than one server instances for current processes.
|
||||
// The key is the name of the server, and the value is its instance.
|
||||
serverMapping = gmap.NewKVMap[string, *Server](true)
|
||||
serverMapping = gmap.NewKVMapWithChecker[string, *Server](checker, true)
|
||||
|
||||
// serverRunning marks the running server counts.
|
||||
// If there is no successful server running or all servers' shutdown, this value is 0.
|
||||
|
||||
@ -727,6 +727,36 @@ func Test_Issue4093(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/4193
|
||||
func Test_Issue4193(t *testing.T) {
|
||||
type Req struct {
|
||||
g.Meta `method:"post" mime:"multipart/form-data"`
|
||||
File *ghttp.UploadFile `v:"required" type:"file"` // File is required
|
||||
}
|
||||
type Res struct{}
|
||||
|
||||
s := g.Server(guid.S())
|
||||
s.BindMiddlewareDefault(ghttp.MiddlewareHandlerResponse)
|
||||
s.BindHandler("/upload", func(ctx context.Context, req *Req) (res *Res, err error) {
|
||||
return
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.SetAccessLogEnabled(false)
|
||||
s.SetErrorLogEnabled(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
content := client.PostContent(ctx, "/upload", g.Map{
|
||||
"file": "",
|
||||
})
|
||||
t.Assert(content, `{"code":51,"message":"The File field is required","data":null}`)
|
||||
})
|
||||
}
|
||||
|
||||
// Issue4227Req
|
||||
type Issue4227Req struct {
|
||||
g.Meta `path:"/hello/:path_param" method:"post"`
|
||||
|
||||
@ -30,8 +30,9 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
poolChecker = func(v *gpool.Pool) bool { return v == nil }
|
||||
// addressPoolMap is a mapping for address to its pool object.
|
||||
addressPoolMap = gmap.NewKVMap[string, *gpool.Pool](true)
|
||||
addressPoolMap = gmap.NewKVMapWithChecker[string, *gpool.Pool](poolChecker, true)
|
||||
)
|
||||
|
||||
// NewPoolConn creates and returns a connection with pool feature.
|
||||
|
||||
@ -39,8 +39,10 @@ type Server struct {
|
||||
|
||||
// Map for name to server, for singleton purpose.
|
||||
var (
|
||||
// checker is used for checking whether the value is nil.
|
||||
checker = func(v *Server) bool { return v == nil }
|
||||
// serverMapping is the map for name to server.
|
||||
serverMapping = gmap.NewKVMap[any, *Server](true)
|
||||
serverMapping = gmap.NewKVMapWithChecker[any, *Server](checker, true)
|
||||
)
|
||||
|
||||
// GetServer returns the TCP server with specified `name`,
|
||||
|
||||
@ -47,8 +47,10 @@ type Server struct {
|
||||
type ServerHandler func(conn *ServerConn)
|
||||
|
||||
var (
|
||||
// checker is used for checking whether the value is nil.
|
||||
checker = func(v *Server) bool { return v == nil }
|
||||
// serverMapping is used for instance name to its UDP server mappings.
|
||||
serverMapping = gmap.NewKVMap[string, *Server](true)
|
||||
serverMapping = gmap.NewKVMapWithChecker[string, *Server](checker, true)
|
||||
)
|
||||
|
||||
// GetServer creates and returns an udp server instance with given name.
|
||||
|
||||
@ -13,6 +13,9 @@ import (
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
)
|
||||
|
||||
// checker is used to check if the value is nil.
|
||||
var checker = func(v *glist.Element) bool { return v == nil }
|
||||
|
||||
// memoryLru holds LRU info.
|
||||
// It uses list.List from stdlib for its underlying doubly linked list.
|
||||
type memoryLru struct {
|
||||
@ -26,7 +29,7 @@ type memoryLru struct {
|
||||
func newMemoryLru(cap int) *memoryLru {
|
||||
lru := &memoryLru{
|
||||
cap: cap,
|
||||
data: gmap.NewKVMap[any, *glist.Element](false),
|
||||
data: gmap.NewKVMapWithChecker[any, *glist.Element](checker, false),
|
||||
list: glist.New(false),
|
||||
}
|
||||
return lru
|
||||
|
||||
@ -46,8 +46,9 @@ const (
|
||||
|
||||
var (
|
||||
supportedFileTypes = []string{"toml", "yaml", "yml", "json", "ini", "xml", "properties"} // All supported file types suffixes.
|
||||
localInstances = gmap.NewKVMap[string, *Config](true) // Instances map containing configuration instances.
|
||||
customConfigContentMap = gmap.NewStrStrMap(true) // Customized configuration content.
|
||||
checker = func(v *Config) bool { return v == nil }
|
||||
localInstances = gmap.NewKVMapWithChecker[string, *Config](checker, true) // Instances map containing configuration instances.
|
||||
customConfigContentMap = gmap.NewStrStrMap(true) // Customized configuration content.
|
||||
|
||||
// Prefix array for trying searching in resource manager.
|
||||
resourceTryFolders = []string{
|
||||
@ -57,6 +58,9 @@ var (
|
||||
|
||||
// Prefix array for trying searching in the local system.
|
||||
localSystemTryFolders = []string{"", "config/", "manifest/config"}
|
||||
|
||||
// jsonMapChecker is the checker for JSON map.
|
||||
jsonMapChecker = func(v *gjson.Json) bool { return v == nil }
|
||||
)
|
||||
|
||||
// NewAdapterFile returns a new configuration management object.
|
||||
@ -77,7 +81,7 @@ func NewAdapterFile(fileNameOrPath ...string) (*AdapterFile, error) {
|
||||
config := &AdapterFile{
|
||||
defaultFileNameOrPath: gtype.NewString(usedFileNameOrPath),
|
||||
searchPaths: garray.NewStrArray(true),
|
||||
jsonMap: gmap.NewKVMap[string, *gjson.Json](true),
|
||||
jsonMap: gmap.NewKVMapWithChecker[string, *gjson.Json](jsonMapChecker, true),
|
||||
watchers: NewWatcherRegistry(),
|
||||
}
|
||||
// Customized dir path from env/cmd.
|
||||
|
||||
@ -36,6 +36,8 @@ type File struct {
|
||||
}
|
||||
|
||||
var (
|
||||
// checker is used for checking whether the value is nil.
|
||||
checker = func(v *Pool) bool { return v == nil }
|
||||
// Global file pointer pool.
|
||||
pools = gmap.NewKVMap[string, *Pool](true)
|
||||
pools = gmap.NewKVMapWithChecker[string, *Pool](checker, true)
|
||||
)
|
||||
|
||||
@ -82,10 +82,12 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
mu sync.Mutex // Mutex for concurrent safety of defaultWatcher.
|
||||
defaultWatcher *Watcher // Default watcher.
|
||||
callbackIdMap = gmap.NewKVMap[int, *Callback](true) // Global callback id to callback function mapping.
|
||||
callbackIdGenerator = gtype.NewInt() // Atomic id generator for callback.
|
||||
callBacksChecker = func(v *glist.TList[*Callback]) bool { return v == nil } // callBacksChecker checks whether the value is nil.
|
||||
callbackIdMapChecker = func(v *Callback) bool { return v == nil } // callbackIdMapChecker checks whether the value is nil.
|
||||
mu sync.Mutex // Mutex for concurrent safety of defaultWatcher.
|
||||
defaultWatcher *Watcher // Default watcher.
|
||||
callbackIdMap = gmap.NewKVMapWithChecker[int, *Callback](callbackIdMapChecker, true) // Global callback id to callback function mapping.
|
||||
callbackIdGenerator = gtype.NewInt() // Atomic id generator for callback.
|
||||
)
|
||||
|
||||
// New creates and returns a new watcher.
|
||||
@ -99,7 +101,7 @@ func New() (*Watcher, error) {
|
||||
events: gqueue.NewTQueue[*Event](),
|
||||
nameSet: gset.NewStrSet(true),
|
||||
closeChan: make(chan struct{}),
|
||||
callbacks: gmap.NewKVMap[string, *glist.TList[*Callback]](true),
|
||||
callbacks: gmap.NewKVMapWithChecker[string, *glist.TList[*Callback]](callBacksChecker, true),
|
||||
}
|
||||
if watcher, err := fsnotify.NewWatcher(); err == nil {
|
||||
w.watcher = watcher
|
||||
|
||||
@ -14,8 +14,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// Checker function for instances map.
|
||||
checker = func(v *Logger) bool { return v == nil }
|
||||
// Instances map.
|
||||
instances = gmap.NewKVMap[string, *Logger](true)
|
||||
instances = gmap.NewKVMapWithChecker[string, *Logger](checker, true)
|
||||
)
|
||||
|
||||
// Instance returns an instance of Logger with default settings.
|
||||
|
||||
@ -12,6 +12,8 @@ import (
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
)
|
||||
|
||||
var checker = func(v *sync.RWMutex) bool { return v == nil }
|
||||
|
||||
// Locker is a memory based locker.
|
||||
// Note that there's no cache expire mechanism for mutex in locker.
|
||||
// You need remove certain mutex manually when you do not want use it anymore.
|
||||
@ -23,7 +25,7 @@ type Locker struct {
|
||||
// A memory locker can lock/unlock with dynamic string key.
|
||||
func New() *Locker {
|
||||
return &Locker{
|
||||
m: gmap.NewKVMap[string, *sync.RWMutex](true),
|
||||
m: gmap.NewKVMapWithChecker[string, *sync.RWMutex](checker, true),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -43,9 +43,11 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// checker is used for checking whether the value is nil.
|
||||
checker = func(v *gqueue.TQueue[*MsgRequest]) bool { return v == nil }
|
||||
// commReceiveQueues is the group name to queue map for storing received data.
|
||||
// The value of the map is type of *gqueue.TQueue[*MsgRequest].
|
||||
commReceiveQueues = gmap.NewKVMap[string, *gqueue.TQueue[*MsgRequest]](true)
|
||||
commReceiveQueues = gmap.NewKVMapWithChecker[string, *gqueue.TQueue[*MsgRequest]](checker, true)
|
||||
|
||||
// commPidFolderPath specifies the folder path storing pid to port mapping files.
|
||||
commPidFolderPath string
|
||||
|
||||
@ -14,8 +14,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// checker checks whether the value is nil.
|
||||
checker = func(v *Resource) bool { return v == nil }
|
||||
// Instances map.
|
||||
instances = gmap.NewKVMap[string, *Resource](true)
|
||||
instances = gmap.NewKVMapWithChecker[string, *Resource](checker, true)
|
||||
)
|
||||
|
||||
// Instance returns an instance of Resource.
|
||||
|
||||
@ -39,8 +39,10 @@ type SPathCacheItem struct {
|
||||
}
|
||||
|
||||
var (
|
||||
// checker is the checking function for checking the value is nil or not.
|
||||
checker = func(v *SPath) bool { return v == nil }
|
||||
// Path to searching object mapping, used for instance management.
|
||||
pathsMap = gmap.NewKVMap[string, *SPath](true)
|
||||
pathsMap = gmap.NewKVMapWithChecker[string, *SPath](checker, true)
|
||||
)
|
||||
|
||||
// New creates and returns a new path searching manager.
|
||||
|
||||
@ -41,7 +41,8 @@ const (
|
||||
|
||||
var (
|
||||
// Default view object.
|
||||
defaultViewObj *View
|
||||
defaultViewObj *View
|
||||
fileCacheItemChecker = func(v *fileCacheItem) bool { return v == nil }
|
||||
)
|
||||
|
||||
// checkAndInitDefaultView checks and initializes the default view object.
|
||||
@ -69,7 +70,7 @@ func New(path ...string) *View {
|
||||
searchPaths: garray.NewStrArray(),
|
||||
data: make(map[string]any),
|
||||
funcMap: make(map[string]any),
|
||||
fileCacheMap: gmap.NewKVMap[string, *fileCacheItem](true),
|
||||
fileCacheMap: gmap.NewKVMapWithChecker[string, *fileCacheItem](fileCacheItemChecker, true),
|
||||
config: DefaultConfig(),
|
||||
}
|
||||
if len(path) > 0 && len(path[0]) > 0 {
|
||||
|
||||
@ -14,8 +14,9 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
checker = func(v *View) bool { return v == nil }
|
||||
// Instances map.
|
||||
instances = gmap.NewKVMap[string, *View](true)
|
||||
instances = gmap.NewKVMapWithChecker[string, *View](checker, true)
|
||||
)
|
||||
|
||||
// Instance returns an instance of View with default settings.
|
||||
|
||||
@ -18,7 +18,7 @@ package gconv
|
||||
// TODO: change `paramKeyToAttrMap` to `ScanOption` to be more scalable; add `DeepCopy` option for `ScanOption`.
|
||||
func Scan(srcValue any, dstPointer any, paramKeyToAttrMap ...map[string]string) (err error) {
|
||||
option := ScanOption{
|
||||
ContinueOnError: true,
|
||||
ContinueOnError: false,
|
||||
}
|
||||
if len(paramKeyToAttrMap) > 0 {
|
||||
option.ParamKeyToAttrMap = paramKeyToAttrMap[0]
|
||||
|
||||
@ -395,7 +395,11 @@ func (c *Converter) doConvertForDefault(in doConvertInput, option ConvertOption)
|
||||
}
|
||||
|
||||
// custom converter.
|
||||
dstReflectValue, ok, err := c.callCustomConverterWithRefer(fromReflectValue, referReflectValue)
|
||||
var (
|
||||
ok bool
|
||||
dstReflectValue reflect.Value
|
||||
)
|
||||
dstReflectValue, ok, err = c.callCustomConverterWithRefer(fromReflectValue, referReflectValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -415,7 +419,7 @@ func (c *Converter) doConvertForDefault(in doConvertInput, option ConvertOption)
|
||||
switch referReflectValue.Kind() {
|
||||
case reflect.Pointer:
|
||||
// Type converting for custom type pointers.
|
||||
// Eg:
|
||||
// Example:
|
||||
// type PayMode int
|
||||
// type Req struct{
|
||||
// Mode *PayMode
|
||||
|
||||
@ -45,7 +45,11 @@ func (c *Converter) Float32(anyInput any) (float32, error) {
|
||||
}
|
||||
return 0, nil
|
||||
case reflect.String:
|
||||
f, err := strconv.ParseFloat(rv.String(), 32)
|
||||
s := rv.String()
|
||||
if s == "" {
|
||||
return 0, nil
|
||||
}
|
||||
f, err := strconv.ParseFloat(s, 32)
|
||||
if err != nil {
|
||||
return 0, gerror.WrapCodef(
|
||||
gcode.CodeInvalidParameter, err, "converting string to float32 failed for: %v", anyInput,
|
||||
@ -68,6 +72,9 @@ func (c *Converter) Float32(anyInput any) (float32, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if s == "" {
|
||||
return 0, nil
|
||||
}
|
||||
v, err := strconv.ParseFloat(s, 32)
|
||||
if err != nil {
|
||||
return 0, gerror.WrapCodef(
|
||||
@ -112,7 +119,11 @@ func (c *Converter) Float64(anyInput any) (float64, error) {
|
||||
}
|
||||
return 0, nil
|
||||
case reflect.String:
|
||||
f, err := strconv.ParseFloat(rv.String(), 64)
|
||||
s := rv.String()
|
||||
if s == "" {
|
||||
return 0, nil
|
||||
}
|
||||
f, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil {
|
||||
return 0, gerror.WrapCodef(
|
||||
gcode.CodeInvalidParameter, err, "converting string to float64 failed for: %v", anyInput,
|
||||
@ -135,6 +146,9 @@ func (c *Converter) Float64(anyInput any) (float64, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if s == "" {
|
||||
return 0, nil
|
||||
}
|
||||
v, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil {
|
||||
return 0, gerror.WrapCodef(
|
||||
|
||||
@ -88,6 +88,9 @@ func (c *Converter) doMapConvert(
|
||||
value any, recursive RecursiveType, mustMapReturn bool, option MapOption,
|
||||
) (map[string]any, error) {
|
||||
if value == nil {
|
||||
if mustMapReturn {
|
||||
return map[string]any{}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
// It redirects to its underlying value if it has implemented interface iVal.
|
||||
@ -119,6 +122,10 @@ func (c *Converter) doMapConvert(
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if len(r) == 0 && mustMapReturn {
|
||||
return map[string]any{}, nil
|
||||
}
|
||||
// if r is not empty, which means it fails converting to map.
|
||||
return nil, nil
|
||||
}
|
||||
case []byte:
|
||||
@ -128,6 +135,10 @@ func (c *Converter) doMapConvert(
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if len(r) == 0 && mustMapReturn {
|
||||
return map[string]any{}, nil
|
||||
}
|
||||
// if r is not empty, which means it fails converting to map.
|
||||
return nil, nil
|
||||
}
|
||||
case map[any]any:
|
||||
@ -328,6 +339,7 @@ func (c *Converter) doMapConvert(
|
||||
return m, nil
|
||||
}
|
||||
return nil, nil
|
||||
|
||||
default:
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@ -169,10 +169,11 @@ func (c *Converter) Struct(params, pointer any, option ...StructOption) (err err
|
||||
return err
|
||||
}
|
||||
if paramsMap == nil {
|
||||
// fails converting params to map, so it cannot be converted to struct pointer.
|
||||
return gerror.NewCodef(
|
||||
gcode.CodeInvalidParameter,
|
||||
`convert params from "%#v" to "map[string]any" failed`,
|
||||
params,
|
||||
`convert params "%v" to "%s" failed`,
|
||||
params, pointerReflectValue.Type(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -521,8 +522,7 @@ func (c *Converter) bindVarToReflectValue(structFieldValue reflect.Value, value
|
||||
case reflect.Struct:
|
||||
// Recursively converting for struct attribute.
|
||||
if err = c.Struct(value, structFieldValue, option); err != nil {
|
||||
// Note there's reflect conversion mechanism here.
|
||||
structFieldValue.Set(reflect.ValueOf(value).Convert(structFieldValue.Type()))
|
||||
return err
|
||||
}
|
||||
|
||||
// Note that the slice element might be type of struct,
|
||||
@ -653,6 +653,8 @@ func (c *Converter) bindVarToReflectValue(structFieldValue reflect.Value, value
|
||||
elem := item.Elem()
|
||||
if err = c.bindVarToReflectValue(elem, value, option); err == nil {
|
||||
structFieldValue.Set(elem.Addr())
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Not empty pointer, it assigns values to it.
|
||||
|
||||
Reference in New Issue
Block a user