-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps_transform.go
76 lines (66 loc) · 1.89 KB
/
maps_transform.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
package generics
import (
"context"
"fmt"
)
// KeyValuePair is a pairing of key/values, for when we have to represent map
// sets as a list/slice.
type KeyValuePair[K comparable, V any] struct {
Key K
Value V
}
// KeyValuesToMap converts a slice of key-value pairs to a map
func KeyValuesToMap[K comparable, V any](input []KeyValuePair[K, V]) map[K]V {
result := make(map[K]V, len(input))
for _, item := range input {
result[item.Key] = item.Value
}
return result
}
// Keys gets the set of keys in a map
func Keys[K comparable, V any](input map[K]V) []K {
result := make([]K, 0, len(input))
for k := range input {
result = append(result, k)
}
return result
}
// MapValues translates all values in a map to new values
func MapValues[K comparable, V any, NV any](input map[K]V, mapper func(k K, v V) NV) map[K]NV {
result := make(map[K]NV, len(input))
for k, v := range input {
result[k] = mapper(k, v)
}
return result
}
// MapValuesWithContext translates all values in a map to new values
func MapValuesWithContext[K comparable, V any, NV any](ctx context.Context, input map[K]V, mapper func(ctx context.Context, k K, v V) (NV, error)) (map[K]NV, error) {
result := make(map[K]NV, len(input))
for k, v := range input {
mapped, err := mapper(ctx, k, v)
if err != nil {
return nil, fmt.Errorf("failed to map key %v: %w", k, err)
}
result[k] = mapped
}
return result, nil
}
// ToKeyValues converts a map into a set of key/value pairs
func ToKeyValues[K comparable, V any](input map[K]V) []KeyValuePair[K, V] {
result := make([]KeyValuePair[K, V], 0, len(input))
for k, v := range input {
result = append(result, KeyValuePair[K, V]{
Key: k,
Value: v,
})
}
return result
}
// Values gets the set of keys in a map
func Values[K comparable, V any](input map[K]V) []V {
result := make([]V, 0, len(input))
for _, v := range input {
result = append(result, v)
}
return result
}