diff --git a/object/lock.go b/object/lock.go index 2ac632f6..952822bb 100644 --- a/object/lock.go +++ b/object/lock.go @@ -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() diff --git a/object/lock_test.go b/object/lock_test.go new file mode 100644 index 00000000..ee680fbf --- /dev/null +++ b/object/lock_test.go @@ -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)) +} diff --git a/object/test/generate.go b/object/test/generate.go index 323767c7..0c8e9c25 100644 --- a/object/test/generate.go +++ b/object/test/generate.go @@ -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 +}