-
Notifications
You must be signed in to change notification settings - Fork 42
/
util.go
182 lines (159 loc) · 3.91 KB
/
util.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
173
174
175
176
177
178
179
180
181
182
package util
import (
"encoding/json"
"errors"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
// UID generates a unique id
func UID() string {
return uuid.NewV4().String()
}
// Wait is a sync.WaitGroup.Wait() implementation that supports timeouts
func Wait(wg *sync.WaitGroup, timeout time.Duration) bool {
wgDone := make(chan bool)
defer close(wgDone)
go func() {
wg.Wait()
wgDone <- true
}()
select {
case <-wgDone:
return true
case <-time.After(timeout):
return false
}
}
func ConvertStructsToMap(i interface{}) (map[string]interface{}, error) {
ds, err := json.Marshal(i)
if err != nil {
return nil, err
}
var mp map[string]interface{}
err = json.Unmarshal(ds, &mp)
if err != nil {
return nil, err
}
return mp, nil
}
func MustConvertStructsToMap(i interface{}) map[string]interface{} {
if result, err := ConvertStructsToMap(i); err != nil {
panic(err)
} else {
return result
}
}
func Truncate(val interface{}, maxLen int) string {
s := fmt.Sprintf("%v", val)
if len(s) <= maxLen {
return s
}
affix := fmt.Sprintf("<truncated orig_len: %d>", len(s))
return s[:len(s)-len(affix)] + affix
}
// SyncMapLen is simply a sync.Map with options to retrieve the length of it.
type SyncMapLen struct {
mp sync.Map
cachedLen *int32
}
func (e *SyncMapLen) Len() int {
if e.cachedLen == nil {
var count int32
e.mp.Range(func(k, v interface{}) bool {
count++
return true
})
atomic.StoreInt32(e.cachedLen, count)
return int(count)
}
return int(atomic.LoadInt32(e.cachedLen))
}
func (e *SyncMapLen) LoadOrStore(key interface{}, value interface{}) (actual interface{}, loaded bool) {
actual, loaded = e.mp.LoadOrStore(key, value)
if !loaded && e.cachedLen != nil {
atomic.AddInt32(e.cachedLen, 1)
}
return actual, loaded
}
func (e *SyncMapLen) Load(key interface{}) (value interface{}, ok bool) {
return e.mp.Load(key)
}
func (e *SyncMapLen) Store(key, value interface{}) {
e.mp.Store(key, value)
e.cachedLen = nil // We are not sure if an entry was replaced or added
}
func (e *SyncMapLen) Delete(id string) {
e.mp.Delete(id)
e.cachedLen = nil // We are not sure if an entry was removed
}
func (e *SyncMapLen) Range(f func(key interface{}, value interface{}) bool) {
e.mp.Range(f)
}
func LogIfError(err error) {
if err != nil {
logrus.Error(err)
}
}
func AssertProtoEqual(t *testing.T, expected, actual proto.Message) {
assert.True(t, proto.Equal(expected, actual), "expected: %v, actual: %v", expected, actual)
}
// Numeric is a representation
type Number struct {
val float64 // Fix loss of precision in uint64 and int64
}
func (n Number) Value() interface{} {
// TODO return original type
return n.val
}
func ToNumber(val interface{}) (Number, error) {
switch t := val.(type) {
case float64:
return Number{val: t}, nil
case float32:
return Number{val: float64(t)}, nil
case int:
return Number{val: float64(t)}, nil
case int32:
return Number{val: float64(t)}, nil
case int16:
return Number{val: float64(t)}, nil
case int64:
return Number{val: float64(t)}, nil
case int8:
return Number{val: float64(t)}, nil
default:
return Number{}, errors.New("not a supported number (int, int8, int16, int32, int64, float32, " +
"and float64)")
}
}
func CmpProtoTimestamps(l, r *timestamp.Timestamp) bool {
if l.GetSeconds() < r.GetSeconds() {
return true
} else if l.GetSeconds() > r.GetSeconds() {
return false
}
return l.GetNanos() <= r.GetNanos()
}
func MustTimestampProto(ts time.Time) *timestamp.Timestamp {
protoTs, err := ptypes.TimestampProto(ts)
if err != nil {
panic(err)
}
return protoTs
}
func MustTimestamp(protoTs *timestamp.Timestamp) time.Time {
ts, err := ptypes.Timestamp(protoTs)
if err != nil {
panic(err)
}
return ts
}