-
Notifications
You must be signed in to change notification settings - Fork 43
/
utils.go
76 lines (64 loc) · 1.26 KB
/
utils.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 mediasoup
import (
"encoding/json"
"math/rand"
"reflect"
"sync"
"time"
"github.com/imdario/mergo"
)
var randPool = sync.Pool{
New: func() any {
return rand.New(rand.NewSource(time.Now().UnixNano()))
},
}
type H map[string]interface{}
func Bool(b bool) *bool {
return &b
}
func Uint8(v uint8) *uint8 {
return &v
}
func generateRandomNumber() uint32 {
r := randPool.Get().(*rand.Rand)
defer randPool.Put(r)
return uint32(r.Int63n(900000000)) + 100000000
}
func clone(from, to interface{}) (err error) {
data, err := json.Marshal(from)
if err != nil {
return err
}
return json.Unmarshal(data, to)
}
func override(dst, src interface{}) error {
return mergo.Merge(dst, src,
mergo.WithOverride,
mergo.WithTypeCheck,
mergo.WithTransformers(ptrTransformers{}),
)
}
func syncMapLen(m *sync.Map) (len uint32) {
m.Range(func(key, val interface{}) bool {
len++
return true
})
return
}
type ptrTransformers struct{}
// overwrites pointer type
func (ptrTransformers) Transformer(tp reflect.Type) func(dst, src reflect.Value) error {
if tp.Kind() == reflect.Ptr {
return func(dst, src reflect.Value) error {
if !src.IsNil() {
if dst.CanSet() {
dst.Set(src)
} else {
dst = src
}
}
return nil
}
}
return nil
}