Debian Codegraph @

tw/mapper.go

raw
1 package tw
2
3 import (
4 "fmt"
5 "sort"
6 )
7
8 // KeyValuePair represents a single key-value pair from a Mapper.
9 type KeyValuePair[K comparable, V any] struct {
10 Key K
11 Value V
12 }
13
14 // Mapper is a generic map type with comparable keys and any value type.
15 // It provides type-safe operations on maps with additional convenience methods.
16 type Mapper[K comparable, V any] map[K]V
17
18 // NewMapper creates and returns a new initialized Mapper.
19 func NewMapper[K comparable, V any]() Mapper[K, V] {
20 return make(Mapper[K, V])
21 }
22
23 // NewMapperWithKeys creates a Mapper with the given keys and zero values
24 func NewMapperWithKeys[K comparable, V any](keys ...K) Mapper[K, V] {
25 m := make(Mapper[K, V], len(keys))
26 for _, k := range keys {
27 m[k] = *new(V)
28 }
29 return m
30 }
31
32 // NewMapperFromPairs creates a Mapper from alternating key-value pairs
33 func NewMapperFromPairs[K comparable, V any](pairs ...any) Mapper[K, V] {
34 m := make(Mapper[K, V])
35 for i := 0; i < len(pairs); i += 2 {
36 if i+1 >= len(pairs) {
37 break
38 }
39 k, ok := pairs[i].(K)
40 if !ok {
41 continue
42 }
43 v, ok := pairs[i+1].(V)
44 if !ok {
45 continue
46 }
47 m[k] = v
48 }
49 return m
50 }
51
52 // Get returns the value associated with the key.
53 // If the key doesn't exist or the map is nil, it returns the zero value for the value type.
54 func (m Mapper[K, V]) Get(key K) V {
55 if m == nil {
56 var zero V
57 return zero
58 }
59 return m[key]
60 }
61
62 // OK returns the value associated with the key and a boolean indicating whether the key exists.
63 func (m Mapper[K, V]) OK(key K) (V, bool) {
64 if m == nil {
65 var zero V
66 return zero, false
67 }
68 val, ok := m[key]
69 return val, ok
70 }
71
72 // Set sets the value for the specified key.
73 // Does nothing if the map is nil.
74 func (m Mapper[K, V]) Set(key K, value V) Mapper[K, V] {
75 if m != nil {
76 m[key] = value
77 }
78 return m
79 }
80
81 // Delete removes the specified key from the map.
82 // Does nothing if the key doesn't exist or the map is nil.
83 func (m Mapper[K, V]) Delete(key K) Mapper[K, V] {
84 if m != nil {
85 delete(m, key)
86 }
87 return m
88 }
89
90 // Has returns true if the key exists in the map, false otherwise.
91 func (m Mapper[K, V]) Has(key K) bool {
92 if m == nil {
93 return false
94 }
95 _, exists := m[key]
96 return exists
97 }
98
99 // Len returns the number of elements in the map.
100 // Returns 0 if the map is nil.
101 func (m Mapper[K, V]) Len() int {
102 if m == nil {
103 return 0
104 }
105 return len(m)
106 }
107
108 // Keys returns a slice containing all keys in the map.
109 // Returns nil if the map is nil or empty.
110 func (m Mapper[K, V]) Keys() []K {
111 if m == nil {
112 return nil
113 }
114 keys := make([]K, 0, len(m))
115 for k := range m {
116 keys = append(keys, k)
117 }
118 return keys
119 }
120
121 func (m Mapper[K, V]) Clear() {
122 if m == nil {
123 return
124 }
125 for k := range m {
126 delete(m, k)
127 }
128 }
129
130 // Values returns a slice containing all values in the map.
131 // Returns nil if the map is nil or empty.
132 func (m Mapper[K, V]) Values() []V {
133 if m == nil {
134 return nil
135 }
136 values := make([]V, 0, len(m))
137 for _, v := range m {
138 values = append(values, v)
139 }
140 return values
141 }
142
143 // Each iterates over each key-value pair in the map and calls the provided function.
144 // Does nothing if the map is nil.
145 func (m Mapper[K, V]) Each(fn func(K, V)) {
146 for k, v := range m {
147 fn(k, v)
148 }
149 }
150
151 // Filter returns a new Mapper containing only the key-value pairs that satisfy the predicate.
152 func (m Mapper[K, V]) Filter(fn func(K, V) bool) Mapper[K, V] {
153 result := NewMapper[K, V]()
154 for k, v := range m {
155 if fn(k, v) {
156 result[k] = v
157 }
158 }
159 return result
160 }
161
162 // MapValues returns a new Mapper with the same keys but values transformed by the provided function.
163 func (m Mapper[K, V]) MapValues(fn func(V) V) Mapper[K, V] {
164 result := NewMapper[K, V]()
165 for k, v := range m {
166 result[k] = fn(v)
167 }
168 return result
169 }
170
171 // Clone returns a shallow copy of the Mapper.
172 func (m Mapper[K, V]) Clone() Mapper[K, V] {
173 result := NewMapper[K, V]()
174 for k, v := range m {
175 result[k] = v
176 }
177 return result
178 }
179
180 // Slicer converts the Mapper to a Slicer of key-value pairs.
181 func (m Mapper[K, V]) Slicer() Slicer[KeyValuePair[K, V]] {
182 if m == nil {
183 return nil
184 }
185 result := make(Slicer[KeyValuePair[K, V]], 0, len(m))
186 for k, v := range m {
187 result = append(result, KeyValuePair[K, V]{Key: k, Value: v})
188 }
189 return result
190 }
191
192 func (m Mapper[K, V]) SortedKeys() []K {
193 keys := make([]K, 0, len(m))
194 for k := range m {
195 keys = append(keys, k)
196 }
197
198 sort.Slice(keys, func(i, j int) bool {
199 a, b := any(keys[i]), any(keys[j])
200
201 switch va := a.(type) {
202 case int:
203 if vb, ok := b.(int); ok {
204 return va < vb
205 }
206 case int32:
207 if vb, ok := b.(int32); ok {
208 return va < vb
209 }
210 case int64:
211 if vb, ok := b.(int64); ok {
212 return va < vb
213 }
214 case uint:
215 if vb, ok := b.(uint); ok {
216 return va < vb
217 }
218 case uint64:
219 if vb, ok := b.(uint64); ok {
220 return va < vb
221 }
222 case float32:
223 if vb, ok := b.(float32); ok {
224 return va < vb
225 }
226 case float64:
227 if vb, ok := b.(float64); ok {
228 return va < vb
229 }
230 case string:
231 if vb, ok := b.(string); ok {
232 return va < vb
233 }
234 }
235
236 // fallback to string comparison
237 return fmt.Sprintf("%v", a) < fmt.Sprintf("%v", b)
238 })
239
240 return keys
241 }
242
243 func NewBoolMapper[K comparable](keys ...K) Mapper[K, bool] {
244 if len(keys) == 0 {
245 return nil
246 }
247 mapper := NewMapper[K, bool]()
248 for _, key := range keys {
249 mapper.Set(key, true)
250 }
251 return mapper
252 }
253
254 func NewIntMapper[K comparable](keys ...K) Mapper[K, int] {
255 if len(keys) == 0 {
256 return nil
257 }
258 mapper := NewMapper[K, int]()
259 for _, key := range keys {
260 mapper.Set(key, 0)
261 }
262 return mapper
263 }
264
265 func NewIdentityMapper[K comparable](keys ...K) Mapper[K, K] {
266 if len(keys) == 0 {
267 return nil
268 }
269 mapper := NewMapper[K, K]()
270 for _, key := range keys {
271 mapper.Set(key, key)
272 }
273 return mapper
274 }