Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion container/garray/garray_normal_t.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions container/garray/garray_sorted_t.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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()
Expand Down
33 changes: 22 additions & 11 deletions container/gmap/gmap_hash_k_v_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ 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
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]
}

Expand Down Expand Up @@ -58,28 +61,28 @@ func NewKVMapFrom[K comparable, V any](data map[K]V, safe ...bool) *KVMap[K, V]
// 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.RegisterNilChecker(checker)
m.SetNilChecker(checker)
return m
}

// RegisterNilChecker registers a custom nil checker function for the map values.
// 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]) RegisterNilChecker(nilChecker NilChecker[V]) {
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 performs a standard nil check using any(v) == nil.
// 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 any(v) == nil
return empty.IsNil(v)
}

// Iterator iterates the hash map readonly with custom callback function `f`.
Expand Down Expand Up @@ -242,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 is a helper function for GetOrSet* functions.
//
// It returns value with given `key`.
// 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()
Expand Down Expand Up @@ -274,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
Expand All @@ -285,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()
Expand Down Expand Up @@ -524,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()))
}
Expand Down
19 changes: 14 additions & 5 deletions container/gmap/gmap_list_k_v_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewListKVMap[K comparable, V any](safe ...bool) *ListKVMap[K, V] {
// 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.RegisterNilChecker(checker)
m.SetNilChecker(checker)
return m
}

Expand All @@ -81,24 +81,24 @@ func NewListKVMapWithCheckerFrom[K comparable, V any](data map[K]V, nilChecker N
return m
}

// RegisterNilChecker registers a custom nil checker function for the map values.
// 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]) RegisterNilChecker(nilChecker NilChecker[V]) {
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 performs a standard nil check using any(v) == nil.
// 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 any(v) == nil
return empty.IsNil(v)
}

// Iterator is alias of IteratorAsc.
Expand Down Expand Up @@ -402,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()
Expand All @@ -421,6 +423,8 @@ func (m *ListKVMap[K, V]) SetIfNotExist(key K, value V) bool {

// 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()
Expand All @@ -444,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()
Expand Down Expand Up @@ -609,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
Expand Down
15 changes: 12 additions & 3 deletions container/gmap/gmap_z_unit_k_v_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,13 @@ func Test_KVMap_MarshalJSON(t *testing.T) {
t.Assert(data["a"], 1)
t.Assert(data["b"], 2)
})
gtest.C(t, func(t *gtest.T) {
var m gmap.KVMap[int, int]
m.Set(1, 10)
b, err := json.Marshal(m)
t.AssertNil(err)
t.Assert(string(b), `{"1":10}`)
})
}

func Test_KVMap_UnmarshalJSON(t *testing.T) {
Expand Down Expand Up @@ -1647,9 +1654,10 @@ func Test_KVMap_TypedNil(t *testing.T) {
return nil
})
}
t.Assert(m1.Size(), 10)
t.Assert(m1.Size(), 5)

m2 := gmap.NewKVMap[int, *Student](true)
m2.RegisterNilChecker(func(student *Student) bool {
m2.SetNilChecker(func(student *Student) bool {
return student == nil
})
for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -1679,7 +1687,8 @@ func Test_NewKVMapWithChecker_TypedNil(t *testing.T) {
return nil
})
}
t.Assert(m1.Size(), 10)
t.Assert(m1.Size(), 5)

m2 := gmap.NewKVMapWithChecker[int, *Student](func(student *Student) bool {
return student == nil
}, true)
Expand Down
71 changes: 0 additions & 71 deletions container/gmap/gmap_z_unit_list_k_v_map_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 12 additions & 3 deletions container/gmap/gmap_z_unit_list_k_v_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1358,9 +1365,10 @@ func Test_ListKVMap_TypedNil(t *testing.T) {
return nil
})
}
t.Assert(m1.Size(), 10)
t.Assert(m1.Size(), 5)

m2 := gmap.NewListKVMap[int, *Student](true)
m2.RegisterNilChecker(func(student *Student) bool {
m2.SetNilChecker(func(student *Student) bool {
return student == nil
})
for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -1390,7 +1398,8 @@ func Test_NewListKVMapWithChecker_TypedNil(t *testing.T) {
return nil
})
}
t.Assert(m1.Size(), 10)
t.Assert(m1.Size(), 5)

m2 := gmap.NewListKVMapWithChecker[int, *Student](func(student *Student) bool {
return student == nil
}, true)
Expand Down
Loading
Loading