Skip to content

Commit 611eb7a

Browse files
committed
feat: add map helper functions.
1 parent 2443cb5 commit 611eb7a

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

util.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,52 @@ func isEqualForMixSignInt(v1, v2 reflect.Value) bool {
155155
return intVal.Int() == int64(uintVal.Uint())
156156
}
157157

158+
// isMapHasValue checks whether the map contains the specified key or not.
159+
func isMapHasKey(m, k any) bool {
160+
if m == nil || reflect.TypeOf(m).Kind() != reflect.Map {
161+
return false
162+
}
163+
164+
mv := reflect.ValueOf(m)
165+
if mv.Len() == 0 {
166+
return false
167+
}
168+
169+
if !reflect.TypeOf(k).AssignableTo(mv.Type().Key()) {
170+
return false
171+
}
172+
173+
return mv.MapIndex(reflect.ValueOf(k)).Kind() != reflect.Invalid
174+
}
175+
176+
// isMapHasValue checks whether the map contains the specified value or not.
177+
func isMapHasValue(m, v any) bool {
178+
if m == nil || reflect.TypeOf(m).Kind() != reflect.Map {
179+
return false
180+
}
181+
182+
mv := reflect.ValueOf(m)
183+
if mv.Len() == 0 {
184+
return false
185+
}
186+
187+
if !reflect.TypeOf(v).AssignableTo(mv.Type().Elem()) {
188+
return false
189+
}
190+
191+
vv := reflect.ValueOf(v)
192+
iter := mv.MapRange()
193+
194+
for iter.Next() {
195+
mvv := iter.Value()
196+
if isEqual(mvv, vv) {
197+
return true
198+
}
199+
}
200+
201+
return false
202+
}
203+
158204
// isNil checks whether a value is nil or not. It'll always return false if the value is not a
159205
// channel, a function, a map, a point, an unsafe point, an interface, or a slice.
160206
func isNil(val any) bool {

util_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,56 @@ func TestIsComparable(t *testing.T) {
129129
assert.Equal(isComparable([]int{1, 2, 3}), false)
130130
}
131131

132+
func TestIsMapHasKKey(t *testing.T) {
133+
assert := New(t)
134+
135+
assert.NotTrue(isMapHasKey(nil, nil))
136+
assert.NotTrue(isMapHasKey(map[string]int{}, "a"))
137+
assert.True(isMapHasKey(map[string]int{
138+
"a": 1,
139+
"b": 2,
140+
}, "a"))
141+
assert.NotTrue(isMapHasKey(map[string]int{
142+
"a": 1,
143+
"b": 2,
144+
}, "c"))
145+
assert.NotTrue(isMapHasKey(map[string]int{
146+
"a": 1,
147+
"b": 2,
148+
}, 1))
149+
assert.True(isMapHasKey(map[any]int{
150+
"a": 1,
151+
1: 2,
152+
}, 1))
153+
assert.True(isMapHasKey(map[any]int{
154+
"a": 1,
155+
1: 2,
156+
}, "a"))
157+
assert.NotTrue(isMapHasKey(map[any]int{
158+
"a": 1,
159+
1: 2,
160+
}, 1.1))
161+
}
162+
163+
func TestIsMapHasValue(t *testing.T) {
164+
assert := New(t)
165+
166+
assert.NotTrue(isMapHasValue(nil, nil))
167+
assert.NotTrue(isMapHasValue(map[string]int{}, 3))
168+
assert.True(isMapHasValue(map[string]int{
169+
"a": 1,
170+
"b": 2,
171+
}, 1))
172+
assert.NotTrue(isMapHasValue(map[string]int{
173+
"a": 1,
174+
"b": 2,
175+
}, 3))
176+
assert.NotTrue(isMapHasValue(map[string]int{
177+
"a": 1,
178+
"b": 2,
179+
}, true))
180+
}
181+
132182
func TestIsNil(t *testing.T) {
133183
assert := New(t)
134184

0 commit comments

Comments
 (0)