forked from jiyeyuran/mediasoup-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
68 lines (57 loc) · 1.1 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
package mediasoup
import (
"encoding/json"
"math/rand"
"reflect"
"sync"
"time"
"github.com/imdario/mergo"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type H map[string]interface{}
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
}
func Bool(b bool) *bool {
return &b
}
func generateRandomNumber() uint32 {
return uint32(rand.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 int) {
m.Range(func(key, val interface{}) bool {
len++
return true
})
return
}