-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
118 lines (100 loc) · 2.76 KB
/
map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package wrap
import (
"encoding/json"
)
// Map is a generic wrapper for a map with keys of type K and values of type V.
type Map[K comparable, V any] struct {
X map[K]V
}
// Object is a shortcut for Map[string, any]
type Object Map[string, any]
// MapEntry represents a key-value pair in the map.
type MapEntry[K comparable, V any] struct {
Key K
Value V
}
// NewMap creates a new Map instance with the provided initial map.
func NewMap[K comparable, V any](m map[K]V) Map[K, V] {
return Map[K, V]{
X: m,
}
}
// Unwrap returns the underlying map of type map[K]V.
func (m *Map[K, V]) Unwrap() map[K]V {
return m.X
}
// Get retrieves the value associated with the specified key and a boolean indicating if the key exists.
func (m *Map[K, V]) Get(key K) (V, bool) {
var zero V
value, exists := m.X[key]
if !exists {
return zero, false
}
return value, true
}
// Set adds or updates the value for the specified key and returns the Map instance.
func (m Map[K, V]) Set(key K, value V) Map[K, V] {
m.X[key] = value
return m
}
// Delete removes the key-value pair associated with the specified key and returns the Map instance.
func (m Map[K, V]) Delete(key K) Map[K, V] {
delete(m.X, key)
return m
}
// Contains checks if the specified key exists in the map.
func (m Map[K, V]) Contains(key K) bool {
_, exists := m.X[key]
return exists
}
// Keys returns a slice of all keys in the map.
func (m Map[K, V]) Keys() []K {
keys := make([]K, 0, len(m.X))
for key := range m.X {
keys = append(keys, key)
}
return keys
}
// Values returns a slice of all values in the map.
func (m *Map[K, V]) Values() Slice[V] {
values := make([]V, 0, len(m.X))
for _, value := range m.X {
values = append(values, value)
}
return NewSlice(values)
}
// Find returns the key of the first value that satisfies the provided comparison function, or zero value and false if not found.
func (m Map[K, V]) Find(compare func(V) bool) (K, bool) {
var zero K
for key, value := range m.X {
if compare(value) {
return key, true
}
}
return zero, false
}
// Len returns the number of key-value pairs in the map.
func (m Map[K, V]) Len() int {
return len(m.X)
}
// IsEmpty returns true if the map is empty, otherwise false.
func (m Map[K, V]) IsEmpty() bool {
return len(m.X) == 0
}
// Clear removes all key-value pairs from the map.
func (m Map[K, V]) Clear() {
for key := range m.X {
delete(m.X, key)
}
}
// UnmarshalJSON unmarshals JSON data into the Map. It expects a JSON object representation.
func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &m.X); err != nil {
return err
}
return nil
}
// MarshalJSON marshals the Map into JSON. It produces a JSON object representation.
func (m Map[K, V]) MarshalJSON() ([]byte, error) {
return json.Marshal(m.X)
}