@@ -22,8 +22,11 @@ type NilChecker[V any] func(V) bool
2222
2323// KVMap wraps map type `map[K]V` and provides more map features.
2424type KVMap [K comparable , V any ] struct {
25- mu rwmutex.RWMutex
26- data map [K ]V
25+ mu rwmutex.RWMutex
26+ data map [K ]V
27+
28+ // nilChecker is the custom nil checker function.
29+ // It uses empty.IsNil if it's nil.
2730 nilChecker NilChecker [V ]
2831}
2932
@@ -58,28 +61,28 @@ func NewKVMapFrom[K comparable, V any](data map[K]V, safe ...bool) *KVMap[K, V]
5861// The parameter `safe` is used to specify whether to use the map in concurrent-safety mode, which is false by default.
5962func NewKVMapWithCheckerFrom [K comparable , V any ](data map [K ]V , checker NilChecker [V ], safe ... bool ) * KVMap [K , V ] {
6063 m := NewKVMapFrom [K , V ](data , safe ... )
61- m .RegisterNilChecker (checker )
64+ m .SetNilChecker (checker )
6265 return m
6366}
6467
65- // RegisterNilChecker registers a custom nil checker function for the map values.
68+ // SetNilChecker registers a custom nil checker function for the map values.
6669// This function is used to determine if a value should be considered as nil.
6770// The nil checker function takes a value of type V and returns a boolean indicating
6871// whether the value should be treated as nil.
69- func (m * KVMap [K , V ]) RegisterNilChecker (nilChecker NilChecker [V ]) {
72+ func (m * KVMap [K , V ]) SetNilChecker (nilChecker NilChecker [V ]) {
7073 m .mu .Lock ()
7174 defer m .mu .Unlock ()
7275 m .nilChecker = nilChecker
7376}
7477
7578// isNil checks whether the given value is nil.
7679// It first checks if a custom nil checker function is registered and uses it if available,
77- // otherwise it performs a standard nil check using any(v) == nil .
80+ // otherwise it falls back to the default empty.IsNil function .
7881func (m * KVMap [K , V ]) isNil (v V ) bool {
7982 if m .nilChecker != nil {
8083 return m .nilChecker (v )
8184 }
82- return any (v ) == nil
85+ return empty . IsNil (v )
8386}
8487
8588// Iterator iterates the hash map readonly with custom callback function `f`.
@@ -242,11 +245,12 @@ func (m *KVMap[K, V]) Pops(size int) map[K]V {
242245 return newMap
243246}
244247
245- // doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
246- // if not exists, set value to the map with given `key`,
247- // or else just return the existing value.
248+ // doSetWithLockCheck sets value with given `value` if it does not exist,
249+ // and then returns this value and whether it exists.
250+ //
251+ // It is a helper function for GetOrSet* functions.
248252//
249- // It returns value with given `key` .
253+ // Note that, it does not add the value to the map if the given `value` is nil .
250254func (m * KVMap [K , V ]) doSetWithLockCheck (key K , value V ) (val V , ok bool ) {
251255 m .mu .Lock ()
252256 defer m .mu .Unlock ()
@@ -274,6 +278,8 @@ func (m *KVMap[K, V]) GetOrSet(key K, value V) V {
274278// GetOrSetFunc returns the value by key,
275279// or sets value with returned value of callback function `f` if it does not exist
276280// and then returns this value.
281+ //
282+ // Note that, it does not add the value to the map if the returned value of `f` is nil.
277283func (m * KVMap [K , V ]) GetOrSetFunc (key K , f func () V ) V {
278284 v , _ := m .doSetWithLockCheck (key , f ())
279285 return v
@@ -285,6 +291,8 @@ func (m *KVMap[K, V]) GetOrSetFunc(key K, f func() V) V {
285291//
286292// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
287293// with mutex.Lock of the hash map.
294+ //
295+ // Note that, it does not add the value to the map if the returned value of `f` is nil.
288296func (m * KVMap [K , V ]) GetOrSetFuncLock (key K , f func () V ) V {
289297 m .mu .Lock ()
290298 defer m .mu .Unlock ()
@@ -524,6 +532,9 @@ func (m *KVMap[K, V]) String() string {
524532}
525533
526534// MarshalJSON implements the interface MarshalJSON for json.Marshal.
535+ // DO NOT change this receiver to pointer type, as the KVMap can be used as a var defined variable, like:
536+ // var m gmap.KVMap[int, string]
537+ // Please refer to corresponding tests for more details.
527538func (m KVMap [K , V ]) MarshalJSON () ([]byte , error ) {
528539 return json .Marshal (gconv .Map (m .Map ()))
529540}
0 commit comments