-
Notifications
You must be signed in to change notification settings - Fork 53
/
generic.go
172 lines (148 loc) · 3.63 KB
/
generic.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package util
import (
"context"
"encoding/json"
"reflect"
"strings"
"sync"
"slices"
"github.com/iancoleman/strcase"
"github.com/mitchellh/mapstructure"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
func Must[T any](t T, err ...error) T {
if len(err) > 0 {
if err[0] != nil {
panic(err)
}
} else if tv := reflect.ValueOf(t); (tv != reflect.Value{}) {
if verr := tv.Interface().(error); verr != nil {
panic(verr)
}
}
return t
}
// Used with lo.Map to wrap functions that do not take an index argument
func Indexed[T any, U any](f func(T) U) func(T, int) U {
return func(t T, _ int) U {
return f(t)
}
}
func DecodeStruct[T any](input interface{}) (*T, error) {
output := new(T)
config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
TagName: "json",
Squash: true,
MatchName: func(mapKey, fieldName string) bool {
return strings.EqualFold(mapKey, fieldName) ||
strings.EqualFold(strcase.ToSnake(mapKey), fieldName) ||
strings.EqualFold(strcase.ToLowerCamel(mapKey), fieldName)
},
}
// NewDecoder cannot fail - the only error condition is if
// config.Result is not a pointer
decoder := Must(mapstructure.NewDecoder(config))
if err := decoder.Decode(input); err != nil {
return nil, err
}
return output, nil
}
func DeepCopyInto[T any](out, in *T) {
Must(json.Unmarshal(Must(json.Marshal(in)), out))
}
func DeepCopy[T any](in *T) *T {
out := new(T)
DeepCopyInto(out, in)
return out
}
func ProtoClone[T proto.Message](msg T) T {
return proto.Clone(msg).(T)
}
func NewMessage[T proto.Message]() T {
var t T
return t.ProtoReflect().New().Interface().(T)
}
func FieldByName[T proto.Message](name string) protoreflect.FieldDescriptor {
var t T
fields := t.ProtoReflect().Descriptor().Fields()
for i, l := 0, fields.Len(); i < l; i++ {
field := fields.Get(i)
if strings.EqualFold(string(field.Name()), name) {
return field
}
}
return nil
}
func FieldIndexByName[T proto.Message](name string) int {
var t T
fields := t.ProtoReflect().Descriptor().Fields()
for i, l := 0, fields.Len(); i < l; i++ {
field := fields.Get(i)
if strings.EqualFold(string(field.Name()), name) {
return i
}
}
return -1
}
func ReplaceFirstOccurrence[S ~[]T, T comparable](items S, old T, new T) S {
index := slices.Index(items, old)
if index < 0 {
return items
}
return slices.Replace(items, index, index+1, new)
}
func RemoveFirstOccurence[S ~[]T, T comparable](items S, remove T) S {
index := slices.Index(items, remove)
if index < 0 {
return items
}
return slices.Delete(items, index, index+1)
}
func IsInterfaceNil(i interface{}) bool {
return reflect.ValueOf(i).Kind() == reflect.Ptr && reflect.ValueOf(i).IsNil()
}
type LockMap[K comparable, L sync.Locker] interface {
Get(key K) L
Delete(key K)
}
type locker[T any] interface {
*T
sync.Locker
}
type lockMapImpl[K comparable, L locker[T], T any] struct {
locks map[K]L
locksMu sync.Mutex
}
func (lm *lockMapImpl[K, L, T]) Get(key K) L {
lm.locksMu.Lock()
defer lm.locksMu.Unlock()
if l, ok := lm.locks[key]; ok {
return l
}
l := new(T)
lm.locks[key] = l
return l
}
func (lm *lockMapImpl[K, L, T]) Delete(key K) {
lm.locksMu.Lock()
defer lm.locksMu.Unlock()
delete(lm.locks, key)
}
func NewLockMap[K comparable, L locker[T], T any]() LockMap[K, L] {
return &lockMapImpl[K, L, T]{
locks: make(map[K]L),
}
}
func BindContext[A any](ctx context.Context, f func(context.Context) A) func() A {
return func() A {
return f(ctx)
}
}
func BindContext2[A, B any](ctx context.Context, f func(context.Context) (A, B)) func() (A, B) {
return func() (A, B) {
return f(ctx)
}
}