-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
utils.go
48 lines (38 loc) · 914 Bytes
/
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
package common
import (
"encoding/binary"
"time"
"golang.org/x/exp/slices"
)
// An easily-mockable clock interface.
//
//go:generate mockery --quiet --name Clock --output ./mocks/ --case=underscore
type Clock interface {
Now() time.Time
}
type realClock struct {
}
func NewRealClock() Clock {
return &realClock{}
}
func (*realClock) Now() time.Time {
return time.Now()
}
func Uint32ToBytes(val uint32) []byte {
result := make([]byte, 4)
binary.BigEndian.PutUint32(result, val)
return result
}
func BytesToUint32(data []byte) uint32 {
return binary.BigEndian.Uint32(data)
}
// input string can't have any 0x0 characters
func StringToAlignedBytes(input string, size int) []byte {
aligned := make([]byte, size)
copy(aligned, input)
return aligned
}
func AlignedBytesToString(data []byte) string {
idx := slices.IndexFunc(data, func(b byte) bool { return b == 0 })
return string(data[:idx])
}