2020-09-10 13:11:12 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-09-22 10:53:10 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-09-10 13:11:12 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// wrapper over v2 Object that provides
|
|
|
|
// public getter and private setters.
|
|
|
|
type rwObject object.Object
|
|
|
|
|
2020-09-16 11:16:58 +00:00
|
|
|
// ToV2 converts Object to v2 Object message.
|
|
|
|
func (o *rwObject) ToV2() *object.Object {
|
|
|
|
return (*object.Object)(o)
|
|
|
|
}
|
2020-09-10 13:11:12 +00:00
|
|
|
|
2020-09-16 11:16:58 +00:00
|
|
|
func (o *rwObject) setHeaderField(setter func(*object.Header)) {
|
|
|
|
obj := (*object.Object)(o)
|
|
|
|
h := obj.GetHeader()
|
2020-09-10 13:11:12 +00:00
|
|
|
|
2020-09-16 11:16:58 +00:00
|
|
|
if h == nil {
|
|
|
|
h = new(object.Header)
|
|
|
|
obj.SetHeader(h)
|
|
|
|
}
|
2020-09-10 13:11:12 +00:00
|
|
|
|
2020-09-16 11:16:58 +00:00
|
|
|
setter(h)
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 11:16:58 +00:00
|
|
|
func (o *rwObject) setSplitFields(setter func(*object.SplitHeader)) {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
split := h.GetSplit()
|
|
|
|
if split == nil {
|
|
|
|
split = new(object.SplitHeader)
|
|
|
|
h.SetSplit(split)
|
|
|
|
}
|
2020-09-10 13:11:12 +00:00
|
|
|
|
2020-09-16 11:16:58 +00:00
|
|
|
setter(split)
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// ID returns object identifier.
|
|
|
|
func (o *rwObject) ID() *ID {
|
2020-09-10 13:11:12 +00:00
|
|
|
return NewIDFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetObjectID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setID(v *ID) {
|
|
|
|
(*object.Object)(o).
|
|
|
|
SetObjectID(v.ToV2())
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// Signature returns signature of the object identifier.
|
|
|
|
func (o *rwObject) Signature() *pkg.Signature {
|
2020-09-10 13:11:12 +00:00
|
|
|
return pkg.NewSignatureFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetSignature(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setSignature(v *pkg.Signature) {
|
|
|
|
(*object.Object)(o).
|
|
|
|
SetSignature(v.ToV2())
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// Payload returns payload bytes.
|
|
|
|
func (o *rwObject) Payload() []byte {
|
2020-09-10 13:11:12 +00:00
|
|
|
return (*object.Object)(o).
|
|
|
|
GetPayload()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setPayload(v []byte) {
|
|
|
|
(*object.Object)(o).
|
|
|
|
SetPayload(v)
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// Version returns version of the object.
|
|
|
|
func (o *rwObject) Version() *pkg.Version {
|
2020-09-10 13:11:12 +00:00
|
|
|
return pkg.NewVersionFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetVersion(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setVersion(v *pkg.Version) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetVersion(v.ToV2())
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// PayloadSize returns payload length of the object.
|
|
|
|
func (o *rwObject) PayloadSize() uint64 {
|
2020-09-10 13:11:12 +00:00
|
|
|
return (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetPayloadLength()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setPayloadSize(v uint64) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetPayloadLength(v)
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// ContainerID returns identifier of the related container.
|
|
|
|
func (o *rwObject) ContainerID() *container.ID {
|
2020-09-10 13:11:12 +00:00
|
|
|
return container.NewIDFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetContainerID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setContainerID(v *container.ID) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetContainerID(v.ToV2())
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// OwnerID returns identifier of the object owner.
|
|
|
|
func (o *rwObject) OwnerID() *owner.ID {
|
2020-09-10 13:11:12 +00:00
|
|
|
return owner.NewIDFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetOwnerID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setOwnerID(v *owner.ID) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetOwnerID(v.ToV2())
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// CreationEpoch returns epoch number in which object was created.
|
|
|
|
func (o *rwObject) CreationEpoch() uint64 {
|
2020-09-10 13:11:12 +00:00
|
|
|
return (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetCreationEpoch()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setCreationEpoch(v uint64) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetCreationEpoch(v)
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// PayloadChecksum returns checksum of the object payload.
|
|
|
|
func (o *rwObject) PayloadChecksum() *pkg.Checksum {
|
2020-09-10 13:11:12 +00:00
|
|
|
return pkg.NewChecksumFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetPayloadHash(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setPayloadChecksum(v *pkg.Checksum) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetPayloadHash(v.ToV2())
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// PayloadHomomorphicHash returns homomorphic hash of the object payload.
|
|
|
|
func (o *rwObject) PayloadHomomorphicHash() *pkg.Checksum {
|
2020-09-10 13:11:12 +00:00
|
|
|
return pkg.NewChecksumFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetHomomorphicHash(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setPayloadHomomorphicHash(v *pkg.Checksum) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetHomomorphicHash(v.ToV2())
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// Attributes returns object attributes.
|
|
|
|
func (o *rwObject) Attributes() []*Attribute {
|
2020-09-10 13:11:12 +00:00
|
|
|
attrs := (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetAttributes()
|
|
|
|
|
|
|
|
res := make([]*Attribute, 0, len(attrs))
|
|
|
|
|
|
|
|
for i := range attrs {
|
|
|
|
res = append(res, NewAttributeFromV2(attrs[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setAttributes(v ...*Attribute) {
|
|
|
|
attrs := make([]*object.Attribute, 0, len(v))
|
|
|
|
|
|
|
|
for i := range v {
|
|
|
|
attrs = append(attrs, v[i].ToV2())
|
|
|
|
}
|
|
|
|
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetAttributes(attrs)
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// PreviousID returns identifier of the previous sibling object.
|
|
|
|
func (o *rwObject) PreviousID() *ID {
|
2020-09-10 13:11:12 +00:00
|
|
|
return NewIDFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSplit().
|
|
|
|
GetPrevious(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setPreviousID(v *ID) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
|
|
|
split.SetPrevious(v.ToV2())
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// Children return list of the identifiers of the child objects.
|
|
|
|
func (o *rwObject) Children() []*ID {
|
2020-09-10 13:11:12 +00:00
|
|
|
ids := (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSplit().
|
|
|
|
GetChildren()
|
|
|
|
|
|
|
|
res := make([]*ID, 0, len(ids))
|
|
|
|
|
|
|
|
for i := range ids {
|
|
|
|
res = append(res, NewIDFromV2(ids[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setChildren(v ...*ID) {
|
|
|
|
ids := make([]*refs.ObjectID, 0, len(v))
|
|
|
|
|
|
|
|
for i := range v {
|
|
|
|
ids = append(ids, v[i].ToV2())
|
|
|
|
}
|
|
|
|
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
|
|
|
split.SetChildren(ids)
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// ParentID returns identifier of the parent object.
|
|
|
|
func (o *rwObject) ParentID() *ID {
|
2020-09-30 15:39:27 +00:00
|
|
|
return NewIDFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSplit().
|
|
|
|
GetParent(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setParentID(v *ID) {
|
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
|
|
|
split.SetParent(v.ToV2())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// Parent returns parent object w/o payload.
|
|
|
|
func (o *rwObject) Parent() *Object {
|
2020-09-10 13:11:12 +00:00
|
|
|
h := (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSplit()
|
|
|
|
|
2020-09-30 15:40:57 +00:00
|
|
|
parSig := h.GetParentSignature()
|
|
|
|
parHdr := h.GetParentHeader()
|
|
|
|
|
|
|
|
if parSig == nil && parHdr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 13:11:12 +00:00
|
|
|
oV2 := new(object.Object)
|
|
|
|
oV2.SetObjectID(h.GetParent())
|
2020-09-30 15:40:57 +00:00
|
|
|
oV2.SetSignature(parSig)
|
|
|
|
oV2.SetHeader(parHdr)
|
2020-09-10 13:11:12 +00:00
|
|
|
|
|
|
|
return NewFromV2(oV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setParent(v *Object) {
|
2020-09-16 11:16:58 +00:00
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
|
|
|
split.SetParent((*object.Object)(v.rwObject).GetObjectID())
|
|
|
|
split.SetParentSignature((*object.Object)(v.rwObject).GetSignature())
|
|
|
|
split.SetParentHeader((*object.Object)(v.rwObject).GetHeader())
|
|
|
|
})
|
2020-09-10 13:11:12 +00:00
|
|
|
}
|
2020-09-22 10:53:10 +00:00
|
|
|
|
2020-09-30 17:21:32 +00:00
|
|
|
func (o *rwObject) initRelations() {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetSplit(new(object.SplitHeader))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-30 15:59:31 +00:00
|
|
|
func (o *rwObject) resetRelations() {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetSplit(nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// SessionToken returns token of the session
|
2020-09-22 10:53:10 +00:00
|
|
|
// within which object was created.
|
2020-11-16 08:48:38 +00:00
|
|
|
func (o *rwObject) SessionToken() *token.SessionToken {
|
2020-09-22 10:53:10 +00:00
|
|
|
return token.NewSessionTokenFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSessionToken(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setSessionToken(v *token.SessionToken) {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetSessionToken(v.ToV2())
|
|
|
|
})
|
|
|
|
}
|
2020-09-22 11:02:36 +00:00
|
|
|
|
2020-11-16 08:48:38 +00:00
|
|
|
// Type returns type of the object.
|
|
|
|
func (o *rwObject) Type() Type {
|
2020-09-22 11:02:36 +00:00
|
|
|
return TypeFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetObjectType(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *rwObject) setType(t Type) {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetObjectType(t.ToV2())
|
|
|
|
})
|
|
|
|
}
|
2020-09-22 11:42:17 +00:00
|
|
|
|
|
|
|
func (o *rwObject) cutPayload() *rwObject {
|
|
|
|
ov2 := new(object.Object)
|
|
|
|
*ov2 = *(*object.Object)(o)
|
|
|
|
ov2.SetPayload(nil)
|
|
|
|
|
|
|
|
return (*rwObject)(ov2)
|
|
|
|
}
|
2020-09-30 17:21:32 +00:00
|
|
|
|
|
|
|
func (o *rwObject) HasParent() bool {
|
|
|
|
return (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSplit() != nil
|
|
|
|
}
|
2020-11-13 12:51:27 +00:00
|
|
|
|
|
|
|
// Marshal marshals object into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (o *rwObject) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*object.Object)(o).
|
|
|
|
StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of object.
|
|
|
|
func (o *rwObject) Unmarshal(data []byte) error {
|
|
|
|
return (*object.Object)(o).
|
|
|
|
Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes object to protobuf JSON format.
|
|
|
|
func (o *rwObject) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*object.Object)(o).
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes object from protobuf JSON format.
|
|
|
|
func (o *rwObject) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*object.Object)(o).
|
|
|
|
UnmarshalJSON(data)
|
|
|
|
}
|