-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
64 lines (55 loc) · 1.41 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
package storers
import (
"time"
"github.com/lusis/statusthing/internal/validation"
"google.golang.org/protobuf/types/known/timestamppb"
)
// TsToInt64 - Consistent timestamp -> datastore mapping
func TsToInt64(ts *timestamppb.Timestamp) int64 {
if ts == nil {
return 0
}
return ts.AsTime().UnixNano()
}
// Int64ToTs - Consistent datastore -> timestamp mapping
func Int64ToTs(i int64) *timestamppb.Timestamp {
// zero int val is a nil for us
if i == 0 {
return nil
}
return timestamppb.New(time.Unix(0, i).UTC())
}
// TimeToUint64 converts a time.Time to uint64
func TimeToUint64(t *time.Time) uint64 {
def := uint64(0)
if t != nil {
def = uint64(t.UnixNano())
}
return def
}
// TsToUInt64 returns a timestamppb.Timestamp as a uint64
func TsToUInt64(ts *timestamppb.Timestamp) uint64 {
def := uint64(0)
if ts != nil {
def = uint64(ts.AsTime().UnixNano())
}
return def
}
// TsToUInt64Ptr returns a timestamppb.Timestamp as a pointer uint64
// this is used in place of sql.Null* types
func TsToUInt64Ptr(ts *timestamppb.Timestamp) *uint64 {
def := uint64(0)
if ts != nil {
def = uint64(ts.AsTime().UnixNano())
}
return &def
}
// StringPtr returns a string ptr as pointers must be to addressables
// and we don't want to have to create variables for this kind of stuff
// these are used in place of sql.Null* types
func StringPtr(s string) *string {
if validation.ValidString(s) {
return &s
}
return nil
}