[#163] object: Implement functions linking Object and Lock instances

Add `WriteLock`/`ReadLock` function which writes/read `Lock` instace
to/from the `Object` instance.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
remotes/fyrchik/split-info-json
Leonard Lyubich 2022-03-05 16:54:28 +03:00 committed by LeL
parent fb8daf80fc
commit 1c161956c8
3 changed files with 67 additions and 0 deletions

View File

@ -8,8 +8,28 @@ import (
// Lock represents record with locked objects. It is compatible with
// NeoFS API V2 protocol.
//
// Lock instance can be written to the Object, see WriteLock/ReadLock.
type Lock v2object.Lock
// WriteLock writes Lock to the Object, and sets its type to TypeLock.
// The object must not be nil.
//
// See also ReadLock.
func WriteLock(obj *Object, l Lock) {
obj.SetType(TypeLock)
obj.SetPayload(l.Marshal())
}
// ReadLock reads Lock from the Object. The lock must not be nil.
// Returns an error describing incorrect format. Makes sense only
// if object has TypeLock type.
//
// See also WriteLock.
func ReadLock(l *Lock, obj Object) error {
return l.Unmarshal(obj.Payload())
}
// NumberOfMembers returns number of members in lock list.
func (x Lock) NumberOfMembers() int {
return (*v2object.Lock)(&x).NumberOfMembers()

View File

@ -0,0 +1,39 @@
package object_test
import (
"testing"
"github.com/nspcc-dev/neofs-sdk-go/object"
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/test"
"github.com/stretchr/testify/require"
)
func TestLockEncoding(t *testing.T) {
l := *objecttest.Lock()
t.Run("binary", func(t *testing.T) {
data := l.Marshal()
var l2 object.Lock
require.NoError(t, l2.Unmarshal(data))
require.Equal(t, l, l2)
})
}
func TestWriteLock(t *testing.T) {
l := *objecttest.Lock()
var o object.Object
object.WriteLock(&o, l)
var l2 object.Lock
require.NoError(t, object.ReadLock(&l2, o))
require.Equal(t, l, l2)
// corrupt payload
o.Payload()[0]++
require.Error(t, object.ReadLock(&l2, o))
}

View File

@ -112,3 +112,11 @@ func SearchFilters() object.SearchFilters {
return x
}
// Lock returns random object.Lock.
func Lock() *object.Lock {
var l object.Lock
l.WriteMembers([]oid.ID{*oidtest.ID(), *oidtest.ID()})
return &l
}