forked from TrueCloudLab/frostfs-node
[#1214] *: Use single Object
type in whole project
Remove `Object` and `RawObject` types from `pkg/core/object` package. Use `Object` type from NeoFS SDK Go library everywhere. Avoid using the deprecated elements. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
19ad349b27
commit
7ccd1625af
100 changed files with 847 additions and 965 deletions
|
@ -68,7 +68,7 @@ func NewFormatValidator(opts ...FormatValidatorOption) *FormatValidator {
|
|||
// Does not validate payload checksum and content.
|
||||
//
|
||||
// Returns nil error if object has valid structure.
|
||||
func (v *FormatValidator) Validate(obj *Object) error {
|
||||
func (v *FormatValidator) Validate(obj *object.Object) error {
|
||||
if obj == nil {
|
||||
return errNilObject
|
||||
} else if obj.ID() == nil {
|
||||
|
@ -93,18 +93,18 @@ func (v *FormatValidator) Validate(obj *Object) error {
|
|||
return fmt.Errorf("object did not pass expiration check: %w", err)
|
||||
}
|
||||
|
||||
if err := object.CheckHeaderVerificationFields(obj.SDK()); err != nil {
|
||||
if err := object.CheckHeaderVerificationFields(obj); err != nil {
|
||||
return fmt.Errorf("(%T) could not validate header fields: %w", v, err)
|
||||
}
|
||||
|
||||
if obj = obj.GetParent(); obj != nil {
|
||||
if obj = obj.Parent(); obj != nil {
|
||||
return v.Validate(obj)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *FormatValidator) validateSignatureKey(obj *Object) error {
|
||||
func (v *FormatValidator) validateSignatureKey(obj *object.Object) error {
|
||||
token := obj.SessionToken()
|
||||
key := obj.Signature().Key()
|
||||
|
||||
|
@ -133,7 +133,7 @@ func (v *FormatValidator) checkOwnerKey(id *owner.ID, key []byte) error {
|
|||
}
|
||||
|
||||
// ValidateContent validates payload content according to object type.
|
||||
func (v *FormatValidator) ValidateContent(o *Object) error {
|
||||
func (v *FormatValidator) ValidateContent(o *object.Object) error {
|
||||
switch o.Type() {
|
||||
case object.TypeTombstone:
|
||||
if len(o.Payload()) == 0 {
|
||||
|
@ -174,7 +174,7 @@ func (v *FormatValidator) ValidateContent(o *Object) error {
|
|||
}
|
||||
|
||||
if v.deleteHandler != nil {
|
||||
v.deleteHandler.DeleteObjects(o.Address(), addrList...)
|
||||
v.deleteHandler.DeleteObjects(AddressOf(o), addrList...)
|
||||
}
|
||||
case object.TypeStorageGroup:
|
||||
if len(o.Payload()) == 0 {
|
||||
|
@ -201,7 +201,7 @@ func (v *FormatValidator) ValidateContent(o *Object) error {
|
|||
|
||||
var errExpired = errors.New("object has expired")
|
||||
|
||||
func (v *FormatValidator) checkExpiration(obj *Object) error {
|
||||
func (v *FormatValidator) checkExpiration(obj *object.Object) error {
|
||||
exp, err := expirationEpochAttribute(obj)
|
||||
if err != nil {
|
||||
if errors.Is(err, errNoExpirationEpoch) {
|
||||
|
@ -218,7 +218,7 @@ func (v *FormatValidator) checkExpiration(obj *Object) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func expirationEpochAttribute(obj *Object) (uint64, error) {
|
||||
func expirationEpochAttribute(obj *object.Object) (uint64, error) {
|
||||
for _, a := range obj.Attributes() {
|
||||
if a.Key() != objectV2.SysAttributeExpEpoch {
|
||||
continue
|
||||
|
@ -235,7 +235,7 @@ var (
|
|||
errEmptyAttrVal = errors.New("empty attribute value")
|
||||
)
|
||||
|
||||
func (v *FormatValidator) checkAttributes(obj *Object) error {
|
||||
func (v *FormatValidator) checkAttributes(obj *object.Object) error {
|
||||
as := obj.Attributes()
|
||||
|
||||
mUnique := make(map[string]struct{}, len(as))
|
||||
|
@ -259,7 +259,7 @@ func (v *FormatValidator) checkAttributes(obj *Object) error {
|
|||
|
||||
var errIncorrectOwner = errors.New("incorrect object owner")
|
||||
|
||||
func (v *FormatValidator) checkOwner(obj *Object) error {
|
||||
func (v *FormatValidator) checkOwner(obj *object.Object) error {
|
||||
// TODO: use appropriate functionality after neofs-api-go#352
|
||||
if len(obj.OwnerID().ToV2().GetValue()) != owner.NEO3WalletSize {
|
||||
return errIncorrectOwner
|
||||
|
|
|
@ -35,8 +35,8 @@ func testObjectID(t *testing.T) *oidSDK.ID {
|
|||
return id
|
||||
}
|
||||
|
||||
func blankValidObject(key *ecdsa.PrivateKey) *RawObject {
|
||||
obj := NewRaw()
|
||||
func blankValidObject(key *ecdsa.PrivateKey) *object.Object {
|
||||
obj := object.New()
|
||||
obj.SetContainerID(cidtest.ID())
|
||||
obj.SetOwnerID(owner.NewIDFromPublicKey(&key.PublicKey))
|
||||
|
||||
|
@ -68,24 +68,24 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("nil identifier", func(t *testing.T) {
|
||||
obj := NewRaw()
|
||||
obj := object.New()
|
||||
|
||||
require.True(t, errors.Is(v.Validate(obj.Object()), errNilID))
|
||||
require.True(t, errors.Is(v.Validate(obj), errNilID))
|
||||
})
|
||||
|
||||
t.Run("nil container identifier", func(t *testing.T) {
|
||||
obj := NewRaw()
|
||||
obj := object.New()
|
||||
obj.SetID(testObjectID(t))
|
||||
|
||||
require.True(t, errors.Is(v.Validate(obj.Object()), errNilCID))
|
||||
require.True(t, errors.Is(v.Validate(obj), errNilCID))
|
||||
})
|
||||
|
||||
t.Run("unsigned object", func(t *testing.T) {
|
||||
obj := NewRaw()
|
||||
obj := object.New()
|
||||
obj.SetContainerID(cidtest.ID())
|
||||
obj.SetID(testObjectID(t))
|
||||
|
||||
require.Error(t, v.Validate(obj.Object()))
|
||||
require.Error(t, v.Validate(obj))
|
||||
})
|
||||
|
||||
t.Run("correct w/ session token", func(t *testing.T) {
|
||||
|
@ -94,29 +94,29 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
tok := sessiontest.Token()
|
||||
tok.SetOwnerID(oid)
|
||||
|
||||
obj := NewRaw()
|
||||
obj := object.New()
|
||||
obj.SetContainerID(cidtest.ID())
|
||||
obj.SetSessionToken(sessiontest.Token())
|
||||
obj.SetOwnerID(tok.OwnerID())
|
||||
|
||||
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj.SDK()))
|
||||
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj))
|
||||
|
||||
require.NoError(t, v.Validate(obj.Object()))
|
||||
require.NoError(t, v.Validate(obj))
|
||||
})
|
||||
|
||||
t.Run("correct w/o session token", func(t *testing.T) {
|
||||
obj := blankValidObject(&ownerKey.PrivateKey)
|
||||
|
||||
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj.SDK()))
|
||||
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj))
|
||||
|
||||
require.NoError(t, v.Validate(obj.Object()))
|
||||
require.NoError(t, v.Validate(obj))
|
||||
})
|
||||
|
||||
t.Run("tombstone content", func(t *testing.T) {
|
||||
obj := NewRaw()
|
||||
obj := object.New()
|
||||
obj.SetType(object.TypeTombstone)
|
||||
|
||||
require.Error(t, v.ValidateContent(obj.Object())) // no tombstone content
|
||||
require.Error(t, v.ValidateContent(obj)) // no tombstone content
|
||||
|
||||
content := object.NewTombstone()
|
||||
content.SetMembers([]*oidSDK.ID{nil})
|
||||
|
@ -126,7 +126,7 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetPayload(data)
|
||||
|
||||
require.Error(t, v.ValidateContent(obj.Object())) // no members in tombstone
|
||||
require.Error(t, v.ValidateContent(obj)) // no members in tombstone
|
||||
|
||||
content.SetMembers([]*oidSDK.ID{testObjectID(t)})
|
||||
|
||||
|
@ -135,7 +135,7 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetPayload(data)
|
||||
|
||||
require.Error(t, v.ValidateContent(obj.Object())) // no expiration epoch in tombstone
|
||||
require.Error(t, v.ValidateContent(obj)) // no expiration epoch in tombstone
|
||||
|
||||
expirationAttribute := object.NewAttribute()
|
||||
expirationAttribute.SetKey(objectV2.SysAttributeExpEpoch)
|
||||
|
@ -143,7 +143,7 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetAttributes(expirationAttribute)
|
||||
|
||||
require.Error(t, v.ValidateContent(obj.Object())) // different expiration values
|
||||
require.Error(t, v.ValidateContent(obj)) // different expiration values
|
||||
|
||||
content.SetExpirationEpoch(10)
|
||||
data, err = content.Marshal()
|
||||
|
@ -151,14 +151,14 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetPayload(data)
|
||||
|
||||
require.NoError(t, v.ValidateContent(obj.Object())) // all good
|
||||
require.NoError(t, v.ValidateContent(obj)) // all good
|
||||
})
|
||||
|
||||
t.Run("storage group content", func(t *testing.T) {
|
||||
obj := NewRaw()
|
||||
obj := object.New()
|
||||
obj.SetType(object.TypeStorageGroup)
|
||||
|
||||
require.Error(t, v.ValidateContent(obj.Object()))
|
||||
require.Error(t, v.ValidateContent(obj))
|
||||
|
||||
content := storagegroup.New()
|
||||
content.SetMembers([]*oidSDK.ID{nil})
|
||||
|
@ -168,7 +168,7 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetPayload(data)
|
||||
|
||||
require.Error(t, v.ValidateContent(obj.Object()))
|
||||
require.Error(t, v.ValidateContent(obj))
|
||||
|
||||
content.SetMembers([]*oidSDK.ID{testObjectID(t)})
|
||||
|
||||
|
@ -177,11 +177,11 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetPayload(data)
|
||||
|
||||
require.NoError(t, v.ValidateContent(obj.Object()))
|
||||
require.NoError(t, v.ValidateContent(obj))
|
||||
})
|
||||
|
||||
t.Run("expiration", func(t *testing.T) {
|
||||
fn := func(val string) *Object {
|
||||
fn := func(val string) *object.Object {
|
||||
obj := blankValidObject(&ownerKey.PrivateKey)
|
||||
|
||||
a := object.NewAttribute()
|
||||
|
@ -190,9 +190,9 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetAttributes(a)
|
||||
|
||||
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj.SDK()))
|
||||
require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj))
|
||||
|
||||
return obj.Object()
|
||||
return obj
|
||||
}
|
||||
|
||||
t.Run("invalid attribute value", func(t *testing.T) {
|
||||
|
@ -228,12 +228,12 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetAttributes(a1, a2)
|
||||
|
||||
err := v.checkAttributes(obj.Object())
|
||||
err := v.checkAttributes(obj)
|
||||
require.NoError(t, err)
|
||||
|
||||
a2.SetKey(a1.Key())
|
||||
|
||||
err = v.checkAttributes(obj.Object())
|
||||
err = v.checkAttributes(obj)
|
||||
require.Equal(t, errDuplAttr, err)
|
||||
})
|
||||
|
||||
|
@ -245,7 +245,7 @@ func TestFormatValidator_Validate(t *testing.T) {
|
|||
|
||||
obj.SetAttributes(a)
|
||||
|
||||
err := v.checkAttributes(obj.Object())
|
||||
err := v.checkAttributes(obj)
|
||||
require.Equal(t, errEmptyAttrVal, err)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,70 +1,18 @@
|
|||
package object
|
||||
|
||||
import (
|
||||
objectV2 "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/object"
|
||||
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
||||
)
|
||||
|
||||
// Object represents the NeoFS object.
|
||||
//
|
||||
// Object inherits object type from NeoFS SDK.
|
||||
// It is used to implement some useful methods and functions
|
||||
// for convenient processing of an object by a node.
|
||||
type Object struct {
|
||||
*object.Object
|
||||
}
|
||||
// AddressOf returns address of the object.
|
||||
func AddressOf(obj *object.Object) *addressSDK.Address {
|
||||
if obj != nil {
|
||||
addr := addressSDK.NewAddress()
|
||||
addr.SetObjectID(obj.ID())
|
||||
addr.SetContainerID(obj.ContainerID())
|
||||
|
||||
// Address returns address of the object.
|
||||
func (o *Object) Address() *addressSDK.Address {
|
||||
if o != nil {
|
||||
aV2 := new(refs.Address)
|
||||
aV2.SetObjectID(o.ID().ToV2())
|
||||
aV2.SetContainerID(o.ContainerID().ToV2())
|
||||
|
||||
return addressSDK.NewAddressFromV2(aV2)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SDK returns NeoFS SDK object instance.
|
||||
func (o *Object) SDK() *object.Object {
|
||||
if o != nil {
|
||||
return o.Object
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewFromV2 constructs Object instance from v2 Object message.
|
||||
func NewFromV2(obj *objectV2.Object) *Object {
|
||||
return &Object{
|
||||
Object: object.NewFromV2(obj),
|
||||
}
|
||||
}
|
||||
|
||||
// NewFromSDK constructs Object instance from NeoFS SDK Object.
|
||||
func NewFromSDK(obj *object.Object) *Object {
|
||||
return &Object{
|
||||
Object: obj,
|
||||
}
|
||||
}
|
||||
|
||||
// New constructs blank Object instance.
|
||||
func New() *Object {
|
||||
return NewFromSDK(object.New())
|
||||
}
|
||||
|
||||
// GetParent returns parent object.
|
||||
func (o *Object) GetParent() *Object {
|
||||
if o != nil {
|
||||
if par := o.Object.Parent(); par != nil {
|
||||
return &Object{
|
||||
Object: par,
|
||||
}
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
package object
|
||||
|
||||
import (
|
||||
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
||||
"github.com/nspcc-dev/neofs-sdk-go/object"
|
||||
)
|
||||
|
||||
// RawObject represents the raw NeoFS object.
|
||||
//
|
||||
// RawObject inherits RawObject type from NeoFS SDK.
|
||||
// It is used to implement some useful methods and functions
|
||||
// for convenient processing of a raw object by a node.
|
||||
type RawObject struct {
|
||||
*object.RawObject
|
||||
}
|
||||
|
||||
// NewRawFromV2 constructs RawObject instance from v2 Object message.
|
||||
func NewRawFromV2(obj *objectV2.Object) *RawObject {
|
||||
return &RawObject{
|
||||
RawObject: object.NewRawFromV2(obj),
|
||||
}
|
||||
}
|
||||
|
||||
// NewRawFrom constructs RawObject instance from NeoFS SDK RawObject.
|
||||
func NewRawFrom(obj *object.RawObject) *RawObject {
|
||||
return &RawObject{
|
||||
RawObject: obj,
|
||||
}
|
||||
}
|
||||
|
||||
// NewRawFromObject wraps Object instance to RawObject.
|
||||
func NewRawFromObject(obj *Object) *RawObject {
|
||||
return NewRawFrom(object.NewRawFrom(obj.SDK()))
|
||||
}
|
||||
|
||||
// NewRaw constructs blank RawObject instance.
|
||||
func NewRaw() *RawObject {
|
||||
return NewRawFrom(object.NewRaw())
|
||||
}
|
||||
|
||||
// SDK converts RawObject to NeoFS SDK RawObject instance.
|
||||
func (o *RawObject) SDK() *object.RawObject {
|
||||
if o != nil {
|
||||
return o.RawObject
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Object converts RawObject to read-only Object instance.
|
||||
func (o *RawObject) Object() *Object {
|
||||
if o != nil {
|
||||
return &Object{
|
||||
Object: o.RawObject.Object(),
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CutPayload returns RawObject w/ empty payload.
|
||||
//
|
||||
// Changes of non-payload fields affect source object.
|
||||
func (o *RawObject) CutPayload() *RawObject {
|
||||
if o != nil {
|
||||
return &RawObject{
|
||||
RawObject: o.RawObject.CutPayload(),
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue