-
Notifications
You must be signed in to change notification settings - Fork 53
/
generic.go
127 lines (108 loc) · 2.56 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
package util
import (
"context"
"encoding/json"
"reflect"
"strings"
"sync"
"github.com/iancoleman/strcase"
"github.com/mitchellh/mapstructure"
"github.com/rancher/opni/pkg/logger"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
var (
stackLg = logger.New(logger.WithZapOptions(zap.AddStacktrace(zap.InfoLevel)))
errType = reflect.TypeOf((*error)(nil)).Elem()
)
func Must[T any](t T, err ...error) T {
if len(err) > 0 {
if err[0] != nil {
stackLg.Panic(err)
}
}
if typ := reflect.TypeOf(t); typ != nil && typ.Implements(errType) {
stackLg.Panic(err)
}
return 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 Pointer[T any](t T) *T {
return &t
}
func ProtoClone[T proto.Message](msg T) T {
return proto.Clone(msg).(T)
}
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](f func(context.Context) A, ctx context.Context) func() A {
return func() A {
return f(ctx)
}
}
func BindContext2[A, B any](f func(context.Context) (A, B), ctx context.Context) func() (A, B) {
return func() (A, B) {
return f(ctx)
}
}