From 70c3644e2b80c977a24f7162b05abcd6e0bceae5 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 3 Mar 2022 14:48:55 +0300 Subject: [PATCH] [#139] object: Deprecate `RawObject` type and everything related From now `Object` type should be used directly. Signed-off-by: Leonard Lyubich --- client/object_get.go | 8 +- object/fmt.go | 12 +- object/fmt_test.go | 8 +- object/object.go | 479 ++++++++++++++++++++++++++++++++++++++-- object/raw.go | 170 ++------------ object/raw_test.go | 108 +++++---- object/rw.go | 440 ------------------------------------ object/test/generate.go | 15 +- 8 files changed, 559 insertions(+), 681 deletions(-) delete mode 100644 object/rw.go diff --git a/client/object_get.go b/client/object_get.go index fbd8c996..e16f3843 100644 --- a/client/object_get.go +++ b/client/object_get.go @@ -393,7 +393,7 @@ func GetFullObject(ctx context.Context, c *Client, idCnr cid.ID, idObj oid.ID) ( return nil, fmt.Errorf("read payload: %w", err) } - object.NewRawFrom(&obj).SetPayload(payload) + obj.SetPayload(payload) } res, err := rdr.Close() @@ -445,10 +445,10 @@ func (x *ResObjectHead) ReadHeader(dst *object.Object) bool { objv2.SetHeader(x.hdr.GetHeader()) objv2.SetSignature(x.hdr.GetSignature()) - raw := object.NewRawFromV2(&objv2) - raw.SetID(&x.idObj) + obj := object.NewFromV2(&objv2) + obj.SetID(&x.idObj) - *dst = *raw.Object() + *dst = *obj return true } diff --git a/object/fmt.go b/object/fmt.go index 38646ad9..1ae7b4a6 100644 --- a/object/fmt.go +++ b/object/fmt.go @@ -28,7 +28,7 @@ func CalculatePayloadChecksum(payload []byte) *checksum.Checksum { // CalculateAndSetPayloadChecksum calculates checksum of current // object payload and writes it to the object. -func CalculateAndSetPayloadChecksum(obj *RawObject) { +func CalculateAndSetPayloadChecksum(obj *Object) { obj.SetPayloadChecksum( CalculatePayloadChecksum(obj.Payload()), ) @@ -60,8 +60,8 @@ func CalculateID(obj *Object) (*oid.ID, error) { // CalculateAndSetID calculates identifier for the object // and writes the result to it. -func CalculateAndSetID(obj *RawObject) error { - id, err := CalculateID(obj.Object()) +func CalculateAndSetID(obj *Object) error { + id, err := CalculateID(obj) if err != nil { return err } @@ -94,7 +94,7 @@ func CalculateIDSignature(key *ecdsa.PrivateKey, id *oid.ID) (*signature.Signatu }) } -func CalculateAndSetSignature(key *ecdsa.PrivateKey, obj *RawObject) error { +func CalculateAndSetSignature(key *ecdsa.PrivateKey, obj *Object) error { sig, err := CalculateIDSignature(key, obj.ID()) if err != nil { return err @@ -115,7 +115,7 @@ func VerifyIDSignature(obj *Object) error { } // SetIDWithSignature sets object identifier and signature. -func SetIDWithSignature(key *ecdsa.PrivateKey, obj *RawObject) error { +func SetIDWithSignature(key *ecdsa.PrivateKey, obj *Object) error { if err := CalculateAndSetID(obj); err != nil { return fmt.Errorf("could not set identifier: %w", err) } @@ -128,7 +128,7 @@ func SetIDWithSignature(key *ecdsa.PrivateKey, obj *RawObject) error { } // SetVerificationFields calculates and sets all verification fields of the object. -func SetVerificationFields(key *ecdsa.PrivateKey, obj *RawObject) error { +func SetVerificationFields(key *ecdsa.PrivateKey, obj *Object) error { CalculateAndSetPayloadChecksum(obj) return SetIDWithSignature(key, obj) diff --git a/object/fmt_test.go b/object/fmt_test.go index 6e4bab7a..500d74aa 100644 --- a/object/fmt_test.go +++ b/object/fmt_test.go @@ -9,7 +9,7 @@ import ( ) func TestVerificationFields(t *testing.T) { - obj := NewRaw() + obj := New() payload := make([]byte, 10) _, _ = rand.Read(payload) @@ -21,7 +21,7 @@ func TestVerificationFields(t *testing.T) { require.NoError(t, err) require.NoError(t, SetVerificationFields(&p.PrivateKey, obj)) - require.NoError(t, CheckVerificationFields(obj.Object())) + require.NoError(t, CheckVerificationFields(obj)) items := []struct { corrupt func() @@ -72,10 +72,10 @@ func TestVerificationFields(t *testing.T) { for _, item := range items { item.corrupt() - require.Error(t, CheckVerificationFields(obj.Object())) + require.Error(t, CheckVerificationFields(obj)) item.restore() - require.NoError(t, CheckVerificationFields(obj.Object())) + require.NoError(t, CheckVerificationFields(obj)) } } diff --git a/object/object.go b/object/object.go index 55000dcc..26ff38e3 100644 --- a/object/object.go +++ b/object/object.go @@ -2,24 +2,24 @@ package object import ( "github.com/nspcc-dev/neofs-api-go/v2/object" + "github.com/nspcc-dev/neofs-api-go/v2/refs" + "github.com/nspcc-dev/neofs-sdk-go/checksum" + cid "github.com/nspcc-dev/neofs-sdk-go/container/id" + oid "github.com/nspcc-dev/neofs-sdk-go/object/id" + "github.com/nspcc-dev/neofs-sdk-go/owner" + "github.com/nspcc-dev/neofs-sdk-go/session" + "github.com/nspcc-dev/neofs-sdk-go/signature" + "github.com/nspcc-dev/neofs-sdk-go/version" ) // Object represents v2-compatible NeoFS object that provides // a convenient interface for working in isolation // from the internal structure of an object. -// -// Object allows to work with the object in read-only -// mode as a reflection of the immutability of objects -// in the system. -type Object struct { - *rwObject -} +type Object object.Object // NewFromV2 wraps v2 Object message to Object. func NewFromV2(oV2 *object.Object) *Object { - return &Object{ - rwObject: (*rwObject)(oV2), - } + return (*Object)(oV2) } // New creates and initializes blank Object. @@ -31,15 +31,462 @@ func New() *Object { // ToV2 converts Object to v2 Object message. func (o *Object) ToV2() *object.Object { - if o != nil { - return (*object.Object)(o.rwObject) - } - - return nil + return (*object.Object)(o) } // MarshalHeaderJSON marshals object's header // into JSON format. func (o *Object) MarshalHeaderJSON() ([]byte, error) { - return (*object.Object)(o.rwObject).GetHeader().MarshalJSON() + return (*object.Object)(o).GetHeader().MarshalJSON() +} + +func (o *Object) setHeaderField(setter func(*object.Header)) { + obj := (*object.Object)(o) + h := obj.GetHeader() + + if h == nil { + h = new(object.Header) + obj.SetHeader(h) + } + + setter(h) +} + +func (o *Object) setSplitFields(setter func(*object.SplitHeader)) { + o.setHeaderField(func(h *object.Header) { + split := h.GetSplit() + if split == nil { + split = new(object.SplitHeader) + h.SetSplit(split) + } + + setter(split) + }) +} + +// ID returns object identifier. +func (o *Object) ID() *oid.ID { + return oid.NewIDFromV2( + (*object.Object)(o). + GetObjectID(), + ) +} + +// SetID sets object identifier. +func (o *Object) SetID(v *oid.ID) { + (*object.Object)(o). + SetObjectID(v.ToV2()) +} + +// Signature returns signature of the object identifier. +func (o *Object) Signature() *signature.Signature { + return signature.NewFromV2( + (*object.Object)(o).GetSignature()) +} + +// SetSignature sets signature of the object identifier. +func (o *Object) SetSignature(v *signature.Signature) { + (*object.Object)(o).SetSignature(v.ToV2()) +} + +// Payload returns payload bytes. +func (o *Object) Payload() []byte { + return (*object.Object)(o).GetPayload() +} + +// SetPayload sets payload bytes. +func (o *Object) SetPayload(v []byte) { + (*object.Object)(o).SetPayload(v) +} + +// Version returns version of the object. +func (o *Object) Version() *version.Version { + return version.NewFromV2( + (*object.Object)(o). + GetHeader(). + GetVersion(), + ) +} + +// SetVersion sets version of the object. +func (o *Object) SetVersion(v *version.Version) { + o.setHeaderField(func(h *object.Header) { + h.SetVersion(v.ToV2()) + }) +} + +// PayloadSize returns payload length of the object. +func (o *Object) PayloadSize() uint64 { + return (*object.Object)(o). + GetHeader(). + GetPayloadLength() +} + +// SetPayloadSize sets payload length of the object. +func (o *Object) SetPayloadSize(v uint64) { + o.setHeaderField(func(h *object.Header) { + h.SetPayloadLength(v) + }) +} + +// ContainerID returns identifier of the related container. +func (o *Object) ContainerID() *cid.ID { + return cid.NewFromV2( + (*object.Object)(o). + GetHeader(). + GetContainerID(), + ) +} + +// SetContainerID sets identifier of the related container. +func (o *Object) SetContainerID(v *cid.ID) { + o.setHeaderField(func(h *object.Header) { + h.SetContainerID(v.ToV2()) + }) +} + +// OwnerID returns identifier of the object owner. +func (o *Object) OwnerID() *owner.ID { + return owner.NewIDFromV2( + (*object.Object)(o). + GetHeader(). + GetOwnerID(), + ) +} + +// SetOwnerID sets identifier of the object owner. +func (o *Object) SetOwnerID(v *owner.ID) { + o.setHeaderField(func(h *object.Header) { + h.SetOwnerID(v.ToV2()) + }) +} + +// CreationEpoch returns epoch number in which object was created. +func (o *Object) CreationEpoch() uint64 { + return (*object.Object)(o). + GetHeader(). + GetCreationEpoch() +} + +// SetCreationEpoch sets epoch number in which object was created. +func (o *Object) SetCreationEpoch(v uint64) { + o.setHeaderField(func(h *object.Header) { + h.SetCreationEpoch(v) + }) +} + +// PayloadChecksum returns checksum of the object payload. +func (o *Object) PayloadChecksum() *checksum.Checksum { + return checksum.NewFromV2( + (*object.Object)(o). + GetHeader(). + GetPayloadHash(), + ) +} + +// SetPayloadChecksum sets checksum of the object payload. +func (o *Object) SetPayloadChecksum(v *checksum.Checksum) { + o.setHeaderField(func(h *object.Header) { + h.SetPayloadHash(v.ToV2()) + }) +} + +// PayloadHomomorphicHash returns homomorphic hash of the object payload. +func (o *Object) PayloadHomomorphicHash() *checksum.Checksum { + return checksum.NewFromV2( + (*object.Object)(o). + GetHeader(). + GetHomomorphicHash(), + ) +} + +// SetPayloadHomomorphicHash sets homomorphic hash of the object payload. +func (o *Object) SetPayloadHomomorphicHash(v *checksum.Checksum) { + o.setHeaderField(func(h *object.Header) { + h.SetHomomorphicHash(v.ToV2()) + }) +} + +// Attributes returns object attributes. +func (o *Object) Attributes() []*Attribute { + attrs := (*object.Object)(o). + GetHeader(). + GetAttributes() + + res := make([]*Attribute, 0, len(attrs)) + + for i := range attrs { + res = append(res, NewAttributeFromV2(attrs[i])) + } + + return res +} + +// SetAttributes sets object attributes. +func (o *Object) SetAttributes(v ...*Attribute) { + attrs := make([]*object.Attribute, 0, len(v)) + + for i := range v { + attrs = append(attrs, v[i].ToV2()) + } + + o.setHeaderField(func(h *object.Header) { + h.SetAttributes(attrs) + }) +} + +// PreviousID returns identifier of the previous sibling object. +func (o *Object) PreviousID() *oid.ID { + return oid.NewIDFromV2( + (*object.Object)(o). + GetHeader(). + GetSplit(). + GetPrevious(), + ) +} + +// SetPreviousID sets identifier of the previous sibling object. +func (o *Object) SetPreviousID(v *oid.ID) { + o.setSplitFields(func(split *object.SplitHeader) { + split.SetPrevious(v.ToV2()) + }) +} + +// Children return list of the identifiers of the child objects. +func (o *Object) Children() []*oid.ID { + ids := (*object.Object)(o). + GetHeader(). + GetSplit(). + GetChildren() + + res := make([]*oid.ID, 0, len(ids)) + + for i := range ids { + res = append(res, oid.NewIDFromV2(ids[i])) + } + + return res +} + +// SetChildren sets list of the identifiers of the child objects. +func (o *Object) SetChildren(v ...*oid.ID) { + ids := make([]*refs.ObjectID, 0, len(v)) + + for i := range v { + ids = append(ids, v[i].ToV2()) + } + + o.setSplitFields(func(split *object.SplitHeader) { + split.SetChildren(ids) + }) +} + +// NotificationInfo groups information about object notification +// that can be written to object. +// +// Topic is an optional field. +type NotificationInfo struct { + ni object.NotificationInfo +} + +// Epoch returns object notification tick +// epoch. +func (n NotificationInfo) Epoch() uint64 { + return n.ni.Epoch() +} + +// SetEpoch sets object notification tick +// epoch. +func (n *NotificationInfo) SetEpoch(epoch uint64) { + n.ni.SetEpoch(epoch) +} + +// Topic return optional object notification +// topic. +func (n NotificationInfo) Topic() string { + return n.ni.Topic() +} + +// SetTopic sets optional object notification +// topic. +func (n *NotificationInfo) SetTopic(topic string) { + n.ni.SetTopic(topic) +} + +// NotificationInfo returns notification info +// read from the object structure. +// Returns any error that appeared during notification +// information parsing. +func (o *Object) NotificationInfo() (*NotificationInfo, error) { + ni, err := object.GetNotificationInfo((*object.Object)(o)) + if err != nil { + return nil, err + } + + return &NotificationInfo{ + ni: *ni, + }, nil +} + +// SetNotification writes NotificationInfo to the object structure. +func (o *Object) SetNotification(ni NotificationInfo) { + object.WriteNotificationInfo((*object.Object)(o), ni.ni) +} + +// SplitID return split identity of split object. If object is not split +// returns nil. +func (o *Object) SplitID() *SplitID { + return NewSplitIDFromV2( + (*object.Object)(o). + GetHeader(). + GetSplit(). + GetSplitID(), + ) +} + +// SetSplitID sets split identifier for the split object. +func (o *Object) SetSplitID(id *SplitID) { + o.setSplitFields(func(split *object.SplitHeader) { + split.SetSplitID(id.ToV2()) + }) +} + +// ParentID returns identifier of the parent object. +func (o *Object) ParentID() *oid.ID { + return oid.NewIDFromV2( + (*object.Object)(o). + GetHeader(). + GetSplit(). + GetParent(), + ) +} + +// SetParentID sets identifier of the parent object. +func (o *Object) SetParentID(v *oid.ID) { + o.setSplitFields(func(split *object.SplitHeader) { + split.SetParent(v.ToV2()) + }) +} + +// Parent returns parent object w/o payload. +func (o *Object) Parent() *Object { + h := (*object.Object)(o). + GetHeader(). + GetSplit() + + parSig := h.GetParentSignature() + parHdr := h.GetParentHeader() + + if parSig == nil && parHdr == nil { + return nil + } + + oV2 := new(object.Object) + oV2.SetObjectID(h.GetParent()) + oV2.SetSignature(parSig) + oV2.SetHeader(parHdr) + + return NewFromV2(oV2) +} + +// SetParent sets parent object w/o payload. +func (o *Object) SetParent(v *Object) { + o.setSplitFields(func(split *object.SplitHeader) { + split.SetParent((*object.Object)(v).GetObjectID()) + split.SetParentSignature((*object.Object)(v).GetSignature()) + split.SetParentHeader((*object.Object)(v).GetHeader()) + }) +} + +func (o *Object) initRelations() { + o.setHeaderField(func(h *object.Header) { + h.SetSplit(new(object.SplitHeader)) + }) +} + +func (o *Object) resetRelations() { + o.setHeaderField(func(h *object.Header) { + h.SetSplit(nil) + }) +} + +// SessionToken returns token of the session +// within which object was created. +func (o *Object) SessionToken() *session.Token { + return session.NewTokenFromV2( + (*object.Object)(o). + GetHeader(). + GetSessionToken(), + ) +} + +// SetSessionToken sets token of the session +// within which object was created. +func (o *Object) SetSessionToken(v *session.Token) { + o.setHeaderField(func(h *object.Header) { + h.SetSessionToken(v.ToV2()) + }) +} + +// Type returns type of the object. +func (o *Object) Type() Type { + return TypeFromV2( + (*object.Object)(o). + GetHeader(). + GetObjectType(), + ) +} + +// SetType sets type of the object. +func (o *Object) SetType(v Type) { + o.setHeaderField(func(h *object.Header) { + h.SetObjectType(v.ToV2()) + }) +} + +// CutPayload returns Object w/ empty payload. +// +// Changes of non-payload fields affect source object. +func (o *Object) CutPayload() *Object { + ov2 := new(object.Object) + *ov2 = *(*object.Object)(o) + ov2.SetPayload(nil) + + return (*Object)(ov2) +} + +func (o *Object) HasParent() bool { + return (*object.Object)(o). + GetHeader(). + GetSplit() != nil +} + +// ResetRelations removes all fields of links with other objects. +func (o *Object) ResetRelations() { + o.resetRelations() +} + +// InitRelations initializes relation field. +func (o *Object) InitRelations() { + o.initRelations() +} + +// Marshal marshals object into a protobuf binary form. +func (o *Object) Marshal() ([]byte, error) { + return (*object.Object)(o).StableMarshal(nil) +} + +// Unmarshal unmarshals protobuf binary representation of object. +func (o *Object) Unmarshal(data []byte) error { + return (*object.Object)(o).Unmarshal(data) +} + +// MarshalJSON encodes object to protobuf JSON format. +func (o *Object) MarshalJSON() ([]byte, error) { + return (*object.Object)(o).MarshalJSON() +} + +// UnmarshalJSON decodes object from protobuf JSON format. +func (o *Object) UnmarshalJSON(data []byte) error { + return (*object.Object)(o).UnmarshalJSON(data) } diff --git a/object/raw.go b/object/raw.go index 409176c4..fd776bd7 100644 --- a/object/raw.go +++ b/object/raw.go @@ -2,167 +2,39 @@ package object import ( "github.com/nspcc-dev/neofs-api-go/v2/object" - "github.com/nspcc-dev/neofs-sdk-go/checksum" - cid "github.com/nspcc-dev/neofs-sdk-go/container/id" - oid "github.com/nspcc-dev/neofs-sdk-go/object/id" - "github.com/nspcc-dev/neofs-sdk-go/owner" - "github.com/nspcc-dev/neofs-sdk-go/session" - "github.com/nspcc-dev/neofs-sdk-go/signature" - "github.com/nspcc-dev/neofs-sdk-go/version" ) // RawObject represents v2-compatible NeoFS object that provides // a convenient interface to fill in the fields of // an object in isolation from its internal structure. -type RawObject struct { - *rwObject -} - -// NewRawFromV2 wraps v2 Object message to RawObject. -func NewRawFromV2(oV2 *object.Object) *RawObject { - return &RawObject{ - rwObject: (*rwObject)(oV2), - } -} - -// NewRawFrom wraps Object instance to RawObject. -func NewRawFrom(obj *Object) *RawObject { - return NewRawFromV2(obj.ToV2()) -} - -// NewRaw creates and initializes blank RawObject. // -// Works similar as NewRawFromV2(new(Object)). -func NewRaw() *RawObject { - return NewRawFromV2(new(object.Object)) -} +// Deprecated: use Object type instead. +type RawObject = Object -// Object returns read-only object instance. -func (o *RawObject) Object() *Object { - if o != nil { - return &Object{ - rwObject: o.rwObject, - } - } - - return nil -} - -// SetID sets object identifier. -func (o *RawObject) SetID(v *oid.ID) { - o.setID(v) -} - -// SetSignature sets signature of the object identifier. -func (o *RawObject) SetSignature(v *signature.Signature) { - o.setSignature(v) -} - -// SetPayload sets payload bytes. -func (o *RawObject) SetPayload(v []byte) { - o.setPayload(v) -} - -// SetVersion sets version of the object. -func (o *RawObject) SetVersion(v *version.Version) { - o.setVersion(v) -} - -// SetPayloadSize sets payload length of the object. -func (o *RawObject) SetPayloadSize(v uint64) { - o.setPayloadSize(v) -} - -// SetContainerID sets identifier of the related container. -func (o *RawObject) SetContainerID(v *cid.ID) { - o.setContainerID(v) -} - -// SetOwnerID sets identifier of the object owner. -func (o *RawObject) SetOwnerID(v *owner.ID) { - o.setOwnerID(v) -} - -// SetCreationEpoch sets epoch number in which object was created. -func (o *RawObject) SetCreationEpoch(v uint64) { - o.setCreationEpoch(v) -} - -// SetPayloadChecksum sets checksum of the object payload. -func (o *RawObject) SetPayloadChecksum(v *checksum.Checksum) { - o.setPayloadChecksum(v) -} - -// SetPayloadHomomorphicHash sets homomorphic hash of the object payload. -func (o *RawObject) SetPayloadHomomorphicHash(v *checksum.Checksum) { - o.setPayloadHomomorphicHash(v) -} - -// SetAttributes sets object attributes. -func (o *RawObject) SetAttributes(v ...*Attribute) { - o.setAttributes(v...) -} - -// SetPreviousID sets identifier of the previous sibling object. -func (o *RawObject) SetPreviousID(v *oid.ID) { - o.setPreviousID(v) -} - -// SetChildren sets list of the identifiers of the child objects. -func (o *RawObject) SetChildren(v ...*oid.ID) { - o.setChildren(v...) -} - -// SetSplitID sets split identifier for the split object. -func (o *RawObject) SetSplitID(id *SplitID) { - o.setSplitID(id) -} - -// SetParentID sets identifier of the parent object. -func (o *RawObject) SetParentID(v *oid.ID) { - o.setParentID(v) -} - -// SetParent sets parent object w/o payload. -func (o *RawObject) SetParent(v *Object) { - o.setParent(v) -} - -// SetSessionToken sets token of the session -// within which object was created. -func (o *RawObject) SetSessionToken(v *session.Token) { - o.setSessionToken(v) -} - -// SetType sets type of the object. -func (o *RawObject) SetType(v Type) { - o.setType(v) -} - -// CutPayload returns RawObject w/ empty payload. +// NewRawFromV2 wraps v2 Object message to Object. // -// Changes of non-payload fields affect source object. -func (o *RawObject) CutPayload() *RawObject { - if o != nil { - return &RawObject{ - rwObject: o.rwObject.cutPayload(), - } - } - - return nil +// Deprecated: (v1.0.0) use NewFromV2 function instead. +func NewRawFromV2(oV2 *object.Object) *Object { + return NewFromV2(oV2) } -// ResetRelations removes all fields of links with other objects. -func (o *RawObject) ResetRelations() { - o.resetRelations() +// NewRawFrom wraps Object instance to Object. +// +// Deprecated: (v1.0.0) function is no-op. +func NewRawFrom(obj *Object) *Object { + return obj } -// InitRelations initializes relation field. -func (o *RawObject) InitRelations() { - o.initRelations() +// NewRaw creates and initializes blank Object. +// +// Deprecated: (v1.0.0) use New instead. +func NewRaw() *Object { + return New() } -// SetNotification writes NotificationInfo to the object structure. -func (o *RawObject) SetNotification(ni NotificationInfo) { - o.setNotification(ni) +// Object returns object instance. +// +// Deprecated: (v1.0.0) method is no-op, use arg directly. +func (o *Object) Object() *Object { + return o } diff --git a/object/raw_test.go b/object/raw_test.go index be254718..a05e4ad5 100644 --- a/object/raw_test.go +++ b/object/raw_test.go @@ -37,8 +37,8 @@ func randTZChecksum(t *testing.T) (cs [64]byte) { return } -func TestRawObject_SetID(t *testing.T) { - obj := NewRaw() +func TestObject_SetID(t *testing.T) { + obj := New() id := randID(t) @@ -47,8 +47,8 @@ func TestRawObject_SetID(t *testing.T) { require.Equal(t, id, obj.ID()) } -func TestRawObject_SetSignature(t *testing.T) { - obj := NewRaw() +func TestObject_SetSignature(t *testing.T) { + obj := New() sig := signature.New() sig.SetKey([]byte{1, 2, 3}) @@ -59,8 +59,8 @@ func TestRawObject_SetSignature(t *testing.T) { require.Equal(t, sig, obj.Signature()) } -func TestRawObject_SetPayload(t *testing.T) { - obj := NewRaw() +func TestObject_SetPayload(t *testing.T) { + obj := New() payload := make([]byte, 10) _, _ = rand.Read(payload) @@ -70,8 +70,8 @@ func TestRawObject_SetPayload(t *testing.T) { require.Equal(t, payload, obj.Payload()) } -func TestRawObject_SetVersion(t *testing.T) { - obj := NewRaw() +func TestObject_SetVersion(t *testing.T) { + obj := New() ver := version.New() ver.SetMajor(1) @@ -82,8 +82,8 @@ func TestRawObject_SetVersion(t *testing.T) { require.Equal(t, ver, obj.Version()) } -func TestRawObject_SetPayloadSize(t *testing.T) { - obj := NewRaw() +func TestObject_SetPayloadSize(t *testing.T) { + obj := New() sz := uint64(133) obj.SetPayloadSize(sz) @@ -91,8 +91,8 @@ func TestRawObject_SetPayloadSize(t *testing.T) { require.Equal(t, sz, obj.PayloadSize()) } -func TestRawObject_SetContainerID(t *testing.T) { - obj := NewRaw() +func TestObject_SetContainerID(t *testing.T) { + obj := New() cid := cidtest.ID() @@ -101,8 +101,8 @@ func TestRawObject_SetContainerID(t *testing.T) { require.Equal(t, cid, obj.ContainerID()) } -func TestRawObject_SetOwnerID(t *testing.T) { - obj := NewRaw() +func TestObject_SetOwnerID(t *testing.T) { + obj := New() ownerID := ownertest.ID() @@ -111,17 +111,17 @@ func TestRawObject_SetOwnerID(t *testing.T) { require.Equal(t, ownerID, obj.OwnerID()) } -func TestRawObject_SetCreationEpoch(t *testing.T) { - obj := NewRaw() +func TestObject_SetCreationEpoch(t *testing.T) { + obj := New() creat := uint64(228) - obj.setCreationEpoch(creat) + obj.SetCreationEpoch(creat) require.Equal(t, creat, obj.CreationEpoch()) } -func TestRawObject_SetPayloadChecksum(t *testing.T) { - obj := NewRaw() +func TestObject_SetPayloadChecksum(t *testing.T) { + obj := New() cs := checksum.New() cs.SetSHA256(randSHA256Checksum(t)) @@ -130,8 +130,8 @@ func TestRawObject_SetPayloadChecksum(t *testing.T) { require.Equal(t, cs, obj.PayloadChecksum()) } -func TestRawObject_SetPayloadHomomorphicHash(t *testing.T) { - obj := NewRaw() +func TestObject_SetPayloadHomomorphicHash(t *testing.T) { + obj := New() cs := checksum.New() cs.SetTillichZemor(randTZChecksum(t)) @@ -141,8 +141,8 @@ func TestRawObject_SetPayloadHomomorphicHash(t *testing.T) { require.Equal(t, cs, obj.PayloadHomomorphicHash()) } -func TestRawObject_SetAttributes(t *testing.T) { - obj := NewRaw() +func TestObject_SetAttributes(t *testing.T) { + obj := New() a1 := NewAttribute() a1.SetKey("key1") @@ -157,8 +157,8 @@ func TestRawObject_SetAttributes(t *testing.T) { require.Equal(t, []*Attribute{a1, a2}, obj.Attributes()) } -func TestRawObject_SetPreviousID(t *testing.T) { - obj := NewRaw() +func TestObject_SetPreviousID(t *testing.T) { + obj := New() prev := randID(t) @@ -167,8 +167,8 @@ func TestRawObject_SetPreviousID(t *testing.T) { require.Equal(t, prev, obj.PreviousID()) } -func TestRawObject_SetChildren(t *testing.T) { - obj := NewRaw() +func TestObject_SetChildren(t *testing.T) { + obj := New() id1 := randID(t) id2 := randID(t) @@ -178,8 +178,8 @@ func TestRawObject_SetChildren(t *testing.T) { require.Equal(t, []*oid.ID{id1, id2}, obj.Children()) } -func TestRawObject_SetSplitID(t *testing.T) { - obj := NewRaw() +func TestObject_SetSplitID(t *testing.T) { + obj := New() require.Nil(t, obj.SplitID()) @@ -189,34 +189,32 @@ func TestRawObject_SetSplitID(t *testing.T) { require.Equal(t, obj.SplitID(), splitID) } -func TestRawObject_SetParent(t *testing.T) { - obj := NewRaw() +func TestObject_SetParent(t *testing.T) { + obj := New() require.Nil(t, obj.Parent()) - par := NewRaw() + par := New() par.SetID(randID(t)) par.SetContainerID(cidtest.ID()) par.SetSignature(signature.New()) - parObj := par.Object() + obj.SetParent(par) - obj.SetParent(parObj) - - require.Equal(t, parObj, obj.Parent()) + require.Equal(t, par, obj.Parent()) } -func TestRawObject_ToV2(t *testing.T) { +func TestObject_ToV2(t *testing.T) { objV2 := new(object.Object) objV2.SetPayload([]byte{1, 2, 3}) - obj := NewRawFromV2(objV2) + obj := NewFromV2(objV2) require.Equal(t, objV2, obj.ToV2()) } -func TestRawObject_SetSessionToken(t *testing.T) { - obj := NewRaw() +func TestObject_SetSessionToken(t *testing.T) { + obj := New() tok := sessiontest.Token() @@ -225,8 +223,8 @@ func TestRawObject_SetSessionToken(t *testing.T) { require.Equal(t, tok, obj.SessionToken()) } -func TestRawObject_SetType(t *testing.T) { - obj := NewRaw() +func TestObject_SetType(t *testing.T) { + obj := New() typ := TypeStorageGroup @@ -235,8 +233,8 @@ func TestRawObject_SetType(t *testing.T) { require.Equal(t, typ, obj.Type()) } -func TestRawObject_CutPayload(t *testing.T) { - o1 := NewRaw() +func TestObject_CutPayload(t *testing.T) { + o1 := New() p1 := []byte{12, 3} o1.SetPayload(p1) @@ -262,17 +260,17 @@ func TestRawObject_CutPayload(t *testing.T) { require.Equal(t, p1, o1.Payload()) } -func TestRawObject_SetParentID(t *testing.T) { - obj := NewRaw() +func TestObject_SetParentID(t *testing.T) { + obj := New() id := randID(t) - obj.setParentID(id) + obj.SetParentID(id) require.Equal(t, id, obj.ParentID()) } -func TestRawObject_ResetRelations(t *testing.T) { - obj := NewRaw() +func TestObject_ResetRelations(t *testing.T) { + obj := New() obj.SetPreviousID(randID(t)) @@ -281,8 +279,8 @@ func TestRawObject_ResetRelations(t *testing.T) { require.Nil(t, obj.PreviousID()) } -func TestRwObject_HasParent(t *testing.T) { - obj := NewRaw() +func TestObject_HasParent(t *testing.T) { + obj := New() obj.InitRelations() @@ -293,15 +291,15 @@ func TestRwObject_HasParent(t *testing.T) { require.False(t, obj.HasParent()) } -func TestRWObjectEncoding(t *testing.T) { - o := NewRaw() +func TestObjectEncoding(t *testing.T) { + o := New() o.SetID(randID(t)) t.Run("binary", func(t *testing.T) { data, err := o.Marshal() require.NoError(t, err) - o2 := NewRaw() + o2 := New() require.NoError(t, o2.Unmarshal(data)) require.Equal(t, o, o2) @@ -311,7 +309,7 @@ func TestRWObjectEncoding(t *testing.T) { data, err := o.MarshalJSON() require.NoError(t, err) - o2 := NewRaw() + o2 := New() require.NoError(t, o2.UnmarshalJSON(data)) require.Equal(t, o, o2) diff --git a/object/rw.go b/object/rw.go deleted file mode 100644 index 1c24fe01..00000000 --- a/object/rw.go +++ /dev/null @@ -1,440 +0,0 @@ -package object - -import ( - "github.com/nspcc-dev/neofs-api-go/v2/object" - "github.com/nspcc-dev/neofs-api-go/v2/refs" - "github.com/nspcc-dev/neofs-sdk-go/checksum" - cid "github.com/nspcc-dev/neofs-sdk-go/container/id" - oid "github.com/nspcc-dev/neofs-sdk-go/object/id" - "github.com/nspcc-dev/neofs-sdk-go/owner" - "github.com/nspcc-dev/neofs-sdk-go/session" - "github.com/nspcc-dev/neofs-sdk-go/signature" - "github.com/nspcc-dev/neofs-sdk-go/version" -) - -// wrapper over v2 Object that provides -// public getter and private setters. -type rwObject object.Object - -// ToV2 converts Object to v2 Object message. -func (o *rwObject) ToV2() *object.Object { - return (*object.Object)(o) -} - -func (o *rwObject) setHeaderField(setter func(*object.Header)) { - obj := (*object.Object)(o) - h := obj.GetHeader() - - if h == nil { - h = new(object.Header) - obj.SetHeader(h) - } - - setter(h) -} - -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) - } - - setter(split) - }) -} - -// ID returns object identifier. -func (o *rwObject) ID() *oid.ID { - return oid.NewIDFromV2( - (*object.Object)(o). - GetObjectID(), - ) -} - -func (o *rwObject) setID(v *oid.ID) { - (*object.Object)(o). - SetObjectID(v.ToV2()) -} - -// Signature returns signature of the object identifier. -func (o *rwObject) Signature() *signature.Signature { - return signature.NewFromV2( - (*object.Object)(o).GetSignature()) -} - -func (o *rwObject) setSignature(v *signature.Signature) { - (*object.Object)(o).SetSignature(v.ToV2()) -} - -// Payload returns payload bytes. -func (o *rwObject) Payload() []byte { - return (*object.Object)(o).GetPayload() -} - -func (o *rwObject) setPayload(v []byte) { - (*object.Object)(o).SetPayload(v) -} - -// Version returns version of the object. -func (o *rwObject) Version() *version.Version { - return version.NewFromV2( - (*object.Object)(o). - GetHeader(). - GetVersion(), - ) -} - -func (o *rwObject) setVersion(v *version.Version) { - o.setHeaderField(func(h *object.Header) { - h.SetVersion(v.ToV2()) - }) -} - -// PayloadSize returns payload length of the object. -func (o *rwObject) PayloadSize() uint64 { - return (*object.Object)(o). - GetHeader(). - GetPayloadLength() -} - -func (o *rwObject) setPayloadSize(v uint64) { - o.setHeaderField(func(h *object.Header) { - h.SetPayloadLength(v) - }) -} - -// ContainerID returns identifier of the related container. -func (o *rwObject) ContainerID() *cid.ID { - return cid.NewFromV2( - (*object.Object)(o). - GetHeader(). - GetContainerID(), - ) -} - -func (o *rwObject) setContainerID(v *cid.ID) { - o.setHeaderField(func(h *object.Header) { - h.SetContainerID(v.ToV2()) - }) -} - -// OwnerID returns identifier of the object owner. -func (o *rwObject) OwnerID() *owner.ID { - return owner.NewIDFromV2( - (*object.Object)(o). - GetHeader(). - GetOwnerID(), - ) -} - -func (o *rwObject) setOwnerID(v *owner.ID) { - o.setHeaderField(func(h *object.Header) { - h.SetOwnerID(v.ToV2()) - }) -} - -// CreationEpoch returns epoch number in which object was created. -func (o *rwObject) CreationEpoch() uint64 { - return (*object.Object)(o). - GetHeader(). - GetCreationEpoch() -} - -func (o *rwObject) setCreationEpoch(v uint64) { - o.setHeaderField(func(h *object.Header) { - h.SetCreationEpoch(v) - }) -} - -// PayloadChecksum returns checksum of the object payload. -func (o *rwObject) PayloadChecksum() *checksum.Checksum { - return checksum.NewFromV2( - (*object.Object)(o). - GetHeader(). - GetPayloadHash(), - ) -} - -func (o *rwObject) setPayloadChecksum(v *checksum.Checksum) { - o.setHeaderField(func(h *object.Header) { - h.SetPayloadHash(v.ToV2()) - }) -} - -// PayloadHomomorphicHash returns homomorphic hash of the object payload. -func (o *rwObject) PayloadHomomorphicHash() *checksum.Checksum { - return checksum.NewFromV2( - (*object.Object)(o). - GetHeader(). - GetHomomorphicHash(), - ) -} - -func (o *rwObject) setPayloadHomomorphicHash(v *checksum.Checksum) { - o.setHeaderField(func(h *object.Header) { - h.SetHomomorphicHash(v.ToV2()) - }) -} - -// Attributes returns object attributes. -func (o *rwObject) Attributes() []*Attribute { - 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()) - } - - o.setHeaderField(func(h *object.Header) { - h.SetAttributes(attrs) - }) -} - -// PreviousID returns identifier of the previous sibling object. -func (o *rwObject) PreviousID() *oid.ID { - return oid.NewIDFromV2( - (*object.Object)(o). - GetHeader(). - GetSplit(). - GetPrevious(), - ) -} - -func (o *rwObject) setPreviousID(v *oid.ID) { - o.setSplitFields(func(split *object.SplitHeader) { - split.SetPrevious(v.ToV2()) - }) -} - -// Children return list of the identifiers of the child objects. -func (o *rwObject) Children() []*oid.ID { - ids := (*object.Object)(o). - GetHeader(). - GetSplit(). - GetChildren() - - res := make([]*oid.ID, 0, len(ids)) - - for i := range ids { - res = append(res, oid.NewIDFromV2(ids[i])) - } - - return res -} - -func (o *rwObject) setChildren(v ...*oid.ID) { - ids := make([]*refs.ObjectID, 0, len(v)) - - for i := range v { - ids = append(ids, v[i].ToV2()) - } - - o.setSplitFields(func(split *object.SplitHeader) { - split.SetChildren(ids) - }) -} - -// NotificationInfo groups information about object notification -// that can be written to object. -// -// Topic is an optional field. -type NotificationInfo struct { - ni object.NotificationInfo -} - -// Epoch returns object notification tick -// epoch. -func (n NotificationInfo) Epoch() uint64 { - return n.ni.Epoch() -} - -// SetEpoch sets object notification tick -// epoch. -func (n *NotificationInfo) SetEpoch(epoch uint64) { - n.ni.SetEpoch(epoch) -} - -// Topic return optional object notification -// topic. -func (n NotificationInfo) Topic() string { - return n.ni.Topic() -} - -// SetTopic sets optional object notification -// topic. -func (n *NotificationInfo) SetTopic(topic string) { - n.ni.SetTopic(topic) -} - -// NotificationInfo returns notification info -// read from the object structure. -// Returns any error that appeared during notification -// information parsing. -func (o *rwObject) NotificationInfo() (*NotificationInfo, error) { - ni, err := object.GetNotificationInfo((*object.Object)(o)) - if err != nil { - return nil, err - } - - return &NotificationInfo{ - ni: *ni, - }, nil -} - -func (o *rwObject) setNotification(ni NotificationInfo) { - object.WriteNotificationInfo((*object.Object)(o), ni.ni) -} - -// SplitID return split identity of split object. If object is not split -// returns nil. -func (o *rwObject) SplitID() *SplitID { - return NewSplitIDFromV2( - (*object.Object)(o). - GetHeader(). - GetSplit(). - GetSplitID(), - ) -} - -func (o *rwObject) setSplitID(id *SplitID) { - o.setSplitFields(func(split *object.SplitHeader) { - split.SetSplitID(id.ToV2()) - }) -} - -// ParentID returns identifier of the parent object. -func (o *rwObject) ParentID() *oid.ID { - return oid.NewIDFromV2( - (*object.Object)(o). - GetHeader(). - GetSplit(). - GetParent(), - ) -} - -func (o *rwObject) setParentID(v *oid.ID) { - o.setSplitFields(func(split *object.SplitHeader) { - split.SetParent(v.ToV2()) - }) -} - -// Parent returns parent object w/o payload. -func (o *rwObject) Parent() *Object { - h := (*object.Object)(o). - GetHeader(). - GetSplit() - - parSig := h.GetParentSignature() - parHdr := h.GetParentHeader() - - if parSig == nil && parHdr == nil { - return nil - } - - oV2 := new(object.Object) - oV2.SetObjectID(h.GetParent()) - oV2.SetSignature(parSig) - oV2.SetHeader(parHdr) - - return NewFromV2(oV2) -} - -func (o *rwObject) setParent(v *Object) { - 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()) - }) -} - -func (o *rwObject) initRelations() { - o.setHeaderField(func(h *object.Header) { - h.SetSplit(new(object.SplitHeader)) - }) -} - -func (o *rwObject) resetRelations() { - o.setHeaderField(func(h *object.Header) { - h.SetSplit(nil) - }) -} - -// SessionToken returns token of the session -// within which object was created. -func (o *rwObject) SessionToken() *session.Token { - return session.NewTokenFromV2( - (*object.Object)(o). - GetHeader(). - GetSessionToken(), - ) -} - -func (o *rwObject) setSessionToken(v *session.Token) { - o.setHeaderField(func(h *object.Header) { - h.SetSessionToken(v.ToV2()) - }) -} - -// Type returns type of the object. -func (o *rwObject) Type() Type { - return TypeFromV2( - (*object.Object)(o). - GetHeader(). - GetObjectType(), - ) -} - -func (o *rwObject) setType(t Type) { - o.setHeaderField(func(h *object.Header) { - h.SetObjectType(t.ToV2()) - }) -} - -func (o *rwObject) cutPayload() *rwObject { - ov2 := new(object.Object) - *ov2 = *(*object.Object)(o) - ov2.SetPayload(nil) - - return (*rwObject)(ov2) -} - -func (o *rwObject) HasParent() bool { - return (*object.Object)(o). - GetHeader(). - GetSplit() != nil -} - -// Marshal marshals object into a protobuf binary form. -func (o *rwObject) Marshal() ([]byte, error) { - return (*object.Object)(o).StableMarshal(nil) -} - -// 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) -} diff --git a/object/test/generate.go b/object/test/generate.go index 8d340103..323767c7 100644 --- a/object/test/generate.go +++ b/object/test/generate.go @@ -42,8 +42,8 @@ func SplitID() *object.SplitID { return x } -func generateRaw(withParent bool) *object.RawObject { - x := object.NewRaw() +func generate(withParent bool) *object.Object { + x := object.New() x.SetID(oidtest.ID()) x.SetSessionToken(sessiontest.Token()) @@ -64,20 +64,21 @@ func generateRaw(withParent bool) *object.RawObject { x.SetSignature(sigtest.Signature()) if withParent { - x.SetParent(generateRaw(false).Object()) + x.SetParent(generate(false)) } return x } -// Raw returns random object.RawObject. -func Raw() *object.RawObject { - return generateRaw(true) +// Raw returns random object.Object. +// Deprecated: (v1.0.0) use Object instead. +func Raw() *object.Object { + return Object() } // Object returns random object.Object. func Object() *object.Object { - return Raw().Object() + return generate(true) } // Tombstone returns random object.Tombstone.