2020-11-25 16:32:52 +00:00
|
|
|
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.
|
|
|
|
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.
|
2021-06-09 13:18:56 +00:00
|
|
|
//
|
|
|
|
// Nil converts to nil.
|
2020-11-25 16:32:52 +00:00
|
|
|
func NewSplitIDFromV2(v []byte) *SplitID {
|
2021-06-09 13:18:56 +00:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-25 16:32:52 +00:00
|
|
|
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.
|
2020-11-26 10:00:03 +00:00
|
|
|
func (id *SplitID) String() string {
|
|
|
|
if id == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-11-25 16:32:52 +00:00
|
|
|
return id.uuid.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetUUID sets pre created UUID structure as SplitID.
|
|
|
|
func (id *SplitID) SetUUID(v uuid.UUID) {
|
2020-11-26 10:00:03 +00:00
|
|
|
if id != nil {
|
|
|
|
id.uuid = v
|
|
|
|
}
|
2020-11-25 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts SplitID to a representation of SplitID in neofs-api v2.
|
2021-06-09 13:18:56 +00:00
|
|
|
//
|
|
|
|
// Nil SplitID converts to nil.
|
2020-11-26 10:00:03 +00:00
|
|
|
func (id *SplitID) ToV2() []byte {
|
|
|
|
if id == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-25 16:32:52 +00:00
|
|
|
data, _ := id.uuid.MarshalBinary() // err is always nil
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|