-
Notifications
You must be signed in to change notification settings - Fork 134
/
uuid.go
107 lines (94 loc) · 3.18 KB
/
uuid.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
package armadaevents
import (
"encoding/binary"
"strings"
"github.com/google/uuid"
"github.com/oklog/ulid"
"github.com/pkg/errors"
"github.com/armadaproject/armada/internal/common/armadaerrors"
)
// UuidFromProtoUuid creates and returns a uuid.UUID from an armadaevents.Uuid
// (i.e., the efficient representation used in proto messages).
func UuidFromProtoUuid(id *Uuid) uuid.UUID {
var rv uuid.UUID
binary.BigEndian.PutUint64(rv[:8], id.High64)
binary.BigEndian.PutUint64(rv[8:], id.Low64)
return rv
}
// ProtoUuidFromUuid returns an efficient representation of a UUID meant for embedding in proto messages
// from a uuid.UUID.
func ProtoUuidFromUuid(id uuid.UUID) *Uuid {
return &Uuid{
High64: binary.BigEndian.Uint64(id[:8]),
Low64: binary.BigEndian.Uint64(id[8:]),
}
}
// UlidFromProtoUuid creates and returns a ulid.ULID from an armadaevents.Uuid
// (i.e., the efficient representation used in proto messages).
func UlidFromProtoUuid(protoUuid *Uuid) ulid.ULID {
var rv ulid.ULID
binary.BigEndian.PutUint64(rv[:8], protoUuid.High64)
binary.BigEndian.PutUint64(rv[8:], protoUuid.Low64)
return rv
}
// ProtoUuidFromUlid returns an efficient representation of a UUID meant for embedding in proto messages
// from a uuid.UUID.
func ProtoUuidFromUlid(id ulid.ULID) *Uuid {
return &Uuid{
High64: binary.BigEndian.Uint64(id[:8]),
Low64: binary.BigEndian.Uint64(id[8:]),
}
}
// UlidStringFromProtoUuid returns a string representation of a proto UUID.
// Because Kubernetes requires ids to be valid DNS subdomain names, the string is returned in lower-case; see
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
func UlidStringFromProtoUuid(id *Uuid) (string, error) {
if id == nil {
return "", errors.WithStack(&armadaerrors.ErrInvalidArgument{
Name: "Objects",
Value: nil,
Message: "cannot create string from nil uuid",
})
}
return strings.ToLower(UlidFromProtoUuid(id).String()), nil
}
func UuidStringFromProtoUuid(id *Uuid) (string, error) {
if id == nil {
return "", errors.WithStack(&armadaerrors.ErrInvalidArgument{
Name: "Objects",
Value: nil,
Message: "cannot create string from nil uuid",
})
}
return strings.ToLower(UuidFromProtoUuid(id).String()), nil
}
// MustProtoUuidFromUlidString parses a ULID string into a proto UUID and returns it.
func MustProtoUuidFromUlidString(ulidString string) *Uuid {
id, err := ulid.Parse(ulidString)
if err != nil {
err = errors.WithStack(err)
panic(err)
}
return ProtoUuidFromUlid(id)
}
// ProtoUuidFromUlidString parses a ULID string into a proto UUID and returns it.
func ProtoUuidFromUlidString(ulidString string) (*Uuid, error) {
id, err := ulid.Parse(ulidString)
if err != nil {
err = errors.WithStack(err)
return nil, err
}
return ProtoUuidFromUlid(id), nil
}
// ProtoUuidFromUuidString parses a UUID string into a proto UUID and returns it.
func ProtoUuidFromUuidString(uuidString string) (*Uuid, error) {
id, err := uuid.Parse(uuidString)
if err != nil {
err = errors.WithStack(err)
return nil, err
}
return ProtoUuidFromUuid(id), nil
}
func MustUlidStringFromProtoUuid(id *Uuid) string {
return strings.ToLower(UlidFromProtoUuid(id).String())
}