2021-11-08 10:04:45 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2022-04-12 16:23:16 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-11-08 10:04:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SplitInfo object.SplitInfo
|
|
|
|
|
|
|
|
// NewSplitInfoFromV2 wraps v2 SplitInfo message to SplitInfo.
|
|
|
|
//
|
|
|
|
// Nil object.SplitInfo converts to nil.
|
|
|
|
func NewSplitInfoFromV2(v2 *object.SplitInfo) *SplitInfo {
|
|
|
|
return (*SplitInfo)(v2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSplitInfo creates and initializes blank SplitInfo.
|
|
|
|
//
|
|
|
|
// Defaults:
|
2022-08-24 14:17:40 +00:00
|
|
|
// - splitID: nil;
|
|
|
|
// - lastPart nil;
|
|
|
|
// - link: nil.
|
2021-11-08 10:04:45 +00:00
|
|
|
func NewSplitInfo() *SplitInfo {
|
|
|
|
return NewSplitInfoFromV2(new(object.SplitInfo))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts SplitInfo to v2 SplitInfo message.
|
|
|
|
//
|
|
|
|
// Nil SplitInfo converts to nil.
|
|
|
|
func (s *SplitInfo) ToV2() *object.SplitInfo {
|
|
|
|
return (*object.SplitInfo)(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitInfo) SplitID() *SplitID {
|
|
|
|
return NewSplitIDFromV2(
|
|
|
|
(*object.SplitInfo)(s).GetSplitID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitInfo) SetSplitID(v *SplitID) {
|
|
|
|
(*object.SplitInfo)(s).SetSplitID(v.ToV2())
|
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
func (s SplitInfo) LastPart() (v oid.ID, isSet bool) {
|
2022-04-11 16:25:14 +00:00
|
|
|
v2 := (object.SplitInfo)(s)
|
|
|
|
|
|
|
|
lpV2 := v2.GetLastPart()
|
|
|
|
if lpV2 != nil {
|
2022-04-12 16:23:16 +00:00
|
|
|
_ = v.ReadFromV2(*lpV2)
|
|
|
|
isSet = true
|
2022-04-11 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
return
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
func (s *SplitInfo) SetLastPart(v oid.ID) {
|
|
|
|
var idV2 refs.ObjectID
|
|
|
|
v.WriteToV2(&idV2)
|
|
|
|
|
|
|
|
(*object.SplitInfo)(s).SetLastPart(&idV2)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
func (s SplitInfo) Link() (v oid.ID, isSet bool) {
|
2022-04-11 16:25:14 +00:00
|
|
|
v2 := (object.SplitInfo)(s)
|
|
|
|
|
|
|
|
linkV2 := v2.GetLink()
|
|
|
|
if linkV2 != nil {
|
2022-04-12 16:23:16 +00:00
|
|
|
_ = v.ReadFromV2(*linkV2)
|
|
|
|
isSet = true
|
2022-04-11 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
return
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
func (s *SplitInfo) SetLink(v oid.ID) {
|
|
|
|
var idV2 refs.ObjectID
|
|
|
|
v.WriteToV2(&idV2)
|
|
|
|
|
|
|
|
(*object.SplitInfo)(s).SetLink(&idV2)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitInfo) Marshal() ([]byte, error) {
|
2022-05-30 19:05:35 +00:00
|
|
|
return (*object.SplitInfo)(s).StableMarshal(nil), nil
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitInfo) Unmarshal(data []byte) error {
|
2022-04-12 16:23:16 +00:00
|
|
|
err := (*object.SplitInfo)(s).Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatCheckSI((*object.SplitInfo)(s))
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
2022-03-14 13:52:45 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (s *SplitInfo) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*object.SplitInfo)(s).MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
func (s *SplitInfo) UnmarshalJSON(data []byte) error {
|
2022-04-12 16:23:16 +00:00
|
|
|
err := (*object.SplitInfo)(s).UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatCheckSI((*object.SplitInfo)(s))
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:18:49 +00:00
|
|
|
var errSplitInfoMissingFields = errors.New("neither link object ID nor last part object ID is set")
|
2022-04-12 16:23:16 +00:00
|
|
|
|
|
|
|
func formatCheckSI(v2 *object.SplitInfo) error {
|
|
|
|
link := v2.GetLink()
|
2022-05-17 14:18:49 +00:00
|
|
|
lastPart := v2.GetLastPart()
|
|
|
|
if link == nil && lastPart == nil {
|
|
|
|
return errSplitInfoMissingFields
|
2022-04-12 16:23:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 14:18:49 +00:00
|
|
|
var oID oid.ID
|
2022-04-12 16:23:16 +00:00
|
|
|
|
2022-05-17 14:18:49 +00:00
|
|
|
if link != nil {
|
|
|
|
err := oID.ReadFromV2(*link)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert link object ID: %w", err)
|
|
|
|
}
|
2022-04-12 16:23:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 14:18:49 +00:00
|
|
|
if lastPart != nil {
|
|
|
|
err := oID.ReadFromV2(*lastPart)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert last part object ID: %w", err)
|
|
|
|
}
|
2022-04-12 16:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-03-14 13:52:45 +00:00
|
|
|
}
|