Skip to content

Commit fafa004

Browse files
authored
Merge pull request #2 from octu0/v1.7.1
v1.7.1
2 parents b7fe3f1 + 291c976 commit fafa004

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

map.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package armap
22

33
import (
4+
"fmt"
45
"unsafe"
56

67
"github.com/dolthub/maphash"
@@ -331,6 +332,9 @@ func (m *Map[K, V]) Clear() {
331332
}
332333

333334
func NewMap[K comparable, V any](arena Arena, funcs ...OptionFunc) *Map[K, V] {
335+
checkType[K](arena)
336+
checkType[V](arena)
337+
334338
opt := newOption()
335339
for _, fn := range funcs {
336340
fn(opt)
@@ -350,3 +354,15 @@ func NewMap[K comparable, V any](arena Arena, funcs ...OptionFunc) *Map[K, V] {
350354
m.resize(capacity)
351355
return m
352356
}
357+
358+
func checkType[T any](arena Arena) {
359+
defer func() {
360+
if r := recover(); r != nil {
361+
panic(fmt.Sprintf("armap: type %T cannot be used in Map (likely due to unexported fields preventing clone): %v", *new(T), r))
362+
}
363+
}()
364+
365+
ta := NewTypeArena[T](arena)
366+
var zero T
367+
_ = ta.Clone(zero)
368+
}

map_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strconv"
66
"testing"
7+
"time"
78
)
89

910
func TestMap(t *testing.T) {
@@ -196,4 +197,55 @@ func TestMap(t *testing.T) {
196197
tt.Errorf("count = %d, expected 1", count)
197198
}
198199
})
200+
201+
t.Run("string,time.Time", func(tt *testing.T) {
202+
a := NewArena(1024)
203+
defer a.Release()
204+
205+
defer func() {
206+
if r := recover(); r == nil {
207+
tt.Errorf("expected panic for time.Time value")
208+
}
209+
}()
210+
211+
NewMap[string, time.Time](a)
212+
})
213+
214+
t.Run("string,PublicStruct", func(tt *testing.T) {
215+
type PublicStruct struct {
216+
ID int
217+
Name string
218+
}
219+
a := NewArena(1024)
220+
defer a.Release()
221+
m := NewMap[string, PublicStruct](a)
222+
223+
val := PublicStruct{ID: 1, Name: "test"}
224+
key := "p1"
225+
226+
m.Set(key, val)
227+
228+
if v, ok := m.Get(key); !ok {
229+
tt.Errorf("key %s not found", key)
230+
} else if v != val {
231+
tt.Errorf("value mismatch: got %v, want %v", v, val)
232+
}
233+
})
234+
235+
t.Run("string,PrivateStruct", func(tt *testing.T) {
236+
type PrivateStruct struct {
237+
id int
238+
name string
239+
}
240+
a := NewArena(1024)
241+
defer a.Release()
242+
243+
defer func() {
244+
if r := recover(); r == nil {
245+
tt.Errorf("expected panic for PrivateStruct value")
246+
}
247+
}()
248+
249+
NewMap[string, PrivateStruct](a)
250+
})
199251
}

set_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,42 @@ func TestSet(t *testing.T) {
7474
tt.Errorf("test1 is deleted")
7575
}
7676
})
77+
78+
t.Run("PublicStruct", func(tt *testing.T) {
79+
type PublicStruct struct {
80+
ID int
81+
Name string
82+
}
83+
a := NewArena(1024)
84+
defer a.Release()
85+
s := NewSet[PublicStruct](a)
86+
87+
val := PublicStruct{ID: 1, Name: "test"}
88+
if ok := s.Add(val); ok {
89+
tt.Errorf("val is new key")
90+
}
91+
if ok := s.Contains(val); ok != true {
92+
tt.Errorf("val exists")
93+
}
94+
if ok := s.Delete(val); ok != true {
95+
tt.Errorf("val exists")
96+
}
97+
})
98+
99+
t.Run("PrivateStruct", func(tt *testing.T) {
100+
type PrivateStruct struct {
101+
id int
102+
name string
103+
}
104+
a := NewArena(1024)
105+
defer a.Release()
106+
107+
defer func() {
108+
if r := recover(); r == nil {
109+
tt.Errorf("expected panic for PrivateStruct key")
110+
}
111+
}()
112+
113+
NewSet[PrivateStruct](a)
114+
})
77115
}

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package armap
22

33
const (
44
AppName string = "armap"
5-
Version string = "1.7.0"
5+
Version string = "1.7.1"
66
)

0 commit comments

Comments
 (0)