-
Notifications
You must be signed in to change notification settings - Fork 14
/
splitid.go
80 lines (65 loc) · 1.37 KB
/
splitid.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
package object
import (
"github.com/google/uuid"
)
// SplitID is a UUIDv4 used as attribute in split objects.
type SplitID struct {
uuid uuid.UUID
}
// NewSplitID returns UUID representation of splitID attribute.
//
// Defaults:
// - id: random UUID.
func NewSplitID() *SplitID {
return &SplitID{
uuid: uuid.New(),
}
}
// NewSplitIDFromV2 returns parsed UUID from bytes.
// If v is invalid UUIDv4 byte sequence, then function returns nil.
//
// Nil converts to nil.
func NewSplitIDFromV2(v []byte) *SplitID {
if v == nil {
return nil
}
id := uuid.New()
err := id.UnmarshalBinary(v)
if err != nil {
return nil
}
return &SplitID{
uuid: id,
}
}
// Parse converts UUIDv4 string representation into [SplitID].
func (id *SplitID) Parse(s string) (err error) {
id.uuid, err = uuid.Parse(s)
if err != nil {
return err
}
return nil
}
// String returns UUIDv4 string representation of [SplitID].
func (id *SplitID) String() string {
if id == nil {
return ""
}
return id.uuid.String()
}
// SetUUID sets pre created UUID structure as [SplitID].
func (id *SplitID) SetUUID(v uuid.UUID) {
if id != nil {
id.uuid = v
}
}
// ToV2 converts [SplitID] to a representation of SplitID in neofs-api v2.
//
// Nil SplitID converts to nil.
func (id *SplitID) ToV2() []byte {
if id == nil {
return nil
}
data, _ := id.uuid.MarshalBinary() // err is always nil
return data
}