From ff01ecd393ae4fae38effb2d06d4ebeb8159366f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 31 Jul 2023 13:42:02 +0000 Subject: [PATCH 1/3] add SyncLockMap ctor func --- maps/synclock_map.go | 12 ++++++++++++ maps/synclock_map_test.go | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/maps/synclock_map.go b/maps/synclock_map.go index 10cbf49..fd0b08e 100644 --- a/maps/synclock_map.go +++ b/maps/synclock_map.go @@ -18,6 +18,18 @@ type SyncLockMap[K, V comparable] struct { Map Map[K, V] } +// NewSyncLockMap creates a new SyncLockMap. +// If an existing map is provided, it is used; otherwise, a new map is created. +func NewSyncLockMap[K, V comparable](m Map[K, V]) *SyncLockMap[K, V] { + if m == nil { + m = make(Map[K, V]) + } + + return &SyncLockMap[K, V]{ + Map: m, + } +} + // Lock the current map to read-only mode func (s *SyncLockMap[K, V]) Lock() { s.ReadOnly.Store(true) diff --git a/maps/synclock_map_test.go b/maps/synclock_map_test.go index 69c727b..44afb3e 100644 --- a/maps/synclock_map_test.go +++ b/maps/synclock_map_test.go @@ -18,6 +18,27 @@ func TestSyncLockMap(t *testing.T) { }, } + t.Run("Test NewSyncLockMap with map ", func(t *testing.T) { + m := NewSyncLockMap[string, string](Map[string, string]{ + "key1": "value1", + "key2": "value2", + }) + + if !m.Has("key1") || !m.Has("key2") { + t.Error("couldn't init SyncLockMap with NewSyncLockMap") + } + }) + + t.Run("Test NewSyncLockMap without map", func(t *testing.T) { + m := NewSyncLockMap[string, string](nil) + m.Set("key1", "value1") + m.Set("key2", "value2") + + if !m.Has("key1") || !m.Has("key2") { + t.Error("couldn't init SyncLockMap with NewSyncLockMap") + } + }) + t.Run("Test lock", func(t *testing.T) { m.Lock() if m.ReadOnly.Load() != true { From 66f7624c540cd156a61b24cf19ede37c43175ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 31 Jul 2023 13:55:30 +0000 Subject: [PATCH 2/3] fix lint --- maps/synclock_map_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maps/synclock_map_test.go b/maps/synclock_map_test.go index 44afb3e..a139e32 100644 --- a/maps/synclock_map_test.go +++ b/maps/synclock_map_test.go @@ -31,8 +31,8 @@ func TestSyncLockMap(t *testing.T) { t.Run("Test NewSyncLockMap without map", func(t *testing.T) { m := NewSyncLockMap[string, string](nil) - m.Set("key1", "value1") - m.Set("key2", "value2") + _ = m.Set("key1", "value1") + _ = m.Set("key2", "value2") if !m.Has("key1") || !m.Has("key2") { t.Error("couldn't init SyncLockMap with NewSyncLockMap") From 2cec588fdc126f62edc951c21df8bb771c96de31 Mon Sep 17 00:00:00 2001 From: mzack Date: Mon, 31 Jul 2023 17:48:23 +0200 Subject: [PATCH 3/3] using variadic options --- maps/synclock_map.go | 22 +++++++++++++++++----- maps/synclock_map_test.go | 6 +++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/maps/synclock_map.go b/maps/synclock_map.go index fd0b08e..7050d7d 100644 --- a/maps/synclock_map.go +++ b/maps/synclock_map.go @@ -18,16 +18,28 @@ type SyncLockMap[K, V comparable] struct { Map Map[K, V] } +type SyncLockMapOption[K, V comparable] func(slm *SyncLockMap[K, V]) + +func WithMap[K, V comparable](m Map[K, V]) SyncLockMapOption[K, V] { + return func(slm *SyncLockMap[K, V]) { + slm.Map = m + } +} + // NewSyncLockMap creates a new SyncLockMap. // If an existing map is provided, it is used; otherwise, a new map is created. -func NewSyncLockMap[K, V comparable](m Map[K, V]) *SyncLockMap[K, V] { - if m == nil { - m = make(Map[K, V]) +func NewSyncLockMap[K, V comparable](options ...SyncLockMapOption[K, V]) *SyncLockMap[K, V] { + slm := &SyncLockMap[K, V]{} + + for _, option := range options { + option(slm) } - return &SyncLockMap[K, V]{ - Map: m, + if slm.Map == nil { + slm.Map = make(Map[K, V]) } + + return slm } // Lock the current map to read-only mode diff --git a/maps/synclock_map_test.go b/maps/synclock_map_test.go index a139e32..f985a4e 100644 --- a/maps/synclock_map_test.go +++ b/maps/synclock_map_test.go @@ -19,10 +19,10 @@ func TestSyncLockMap(t *testing.T) { } t.Run("Test NewSyncLockMap with map ", func(t *testing.T) { - m := NewSyncLockMap[string, string](Map[string, string]{ + m := NewSyncLockMap[string, string](WithMap(Map[string, string]{ "key1": "value1", "key2": "value2", - }) + })) if !m.Has("key1") || !m.Has("key2") { t.Error("couldn't init SyncLockMap with NewSyncLockMap") @@ -30,7 +30,7 @@ func TestSyncLockMap(t *testing.T) { }) t.Run("Test NewSyncLockMap without map", func(t *testing.T) { - m := NewSyncLockMap[string, string](nil) + m := NewSyncLockMap[string, string]() _ = m.Set("key1", "value1") _ = m.Set("key2", "value2")