[#194] pkg/container: Implement container nonce getter/setter as UUID

Implement NonceUUID/SetNonceUUID methods on container structure. Change
implementation of Nonce/SetNonce methods and mark them deprecated.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-12-24 12:26:41 +03:00 committed by Leonard Lyubich
parent 3550e128bb
commit 70c29ca3e5
2 changed files with 45 additions and 7 deletions

View file

@ -3,6 +3,7 @@ package container
import ( import (
"crypto/sha256" "crypto/sha256"
"github.com/google/uuid"
"github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap" "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
"github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/nspcc-dev/neofs-api-go/pkg/owner"
@ -21,7 +22,7 @@ func New(opts ...NewOption) *Container {
} }
cnr := new(Container) cnr := new(Container)
cnr.SetNonce(cnrOptions.nonce[:]) cnr.SetNonceUUID(cnrOptions.nonce)
cnr.SetBasicACL(cnrOptions.acl) cnr.SetBasicACL(cnrOptions.acl)
if cnrOptions.owner != nil { if cnrOptions.owner != nil {
@ -81,12 +82,46 @@ func (c *Container) SetOwnerID(v *owner.ID) {
c.v2.SetOwnerID(v.ToV2()) c.v2.SetOwnerID(v.ToV2())
} }
// Nonce returns container nonce in a binary format.
//
// Returns nil if container nonce is not a valid UUID.
//
// Deprecated: use NonceUUID instead.
func (c *Container) Nonce() []byte { func (c *Container) Nonce() []byte {
return c.v2.GetNonce() // return uuid? uid, err := c.NonceUUID()
if err == nil {
data, _ := uid.MarshalBinary()
return data
}
return nil
} }
// SetNonce sets container nonce in a binary format.
//
// If slice length is less than UUID size, than
// value is padded with a sequence of zeros.
// If slice length is more than UUID size, than
// value is cut.
//
// Deprecated: use SetNonceUUID instead.
func (c *Container) SetNonce(v []byte) { func (c *Container) SetNonce(v []byte) {
c.v2.SetNonce(v) // set uuid? u := uuid.UUID{}
copy(u[:], v)
c.v2.SetNonce(u[:])
}
// Returns container nonce in UUID format.
//
// Returns error if container nonce is not a valid UUID.
func (c *Container) NonceUUID() (uuid.UUID, error) {
return uuid.FromBytes(c.v2.GetNonce())
}
// SetNonceUUID sets container nonce as UUID.
func (c *Container) SetNonceUUID(v uuid.UUID) {
data, _ := v.MarshalBinary()
c.v2.SetNonce(data)
} }
func (c *Container) BasicACL() uint32 { func (c *Container) BasicACL() uint32 {

View file

@ -17,8 +17,7 @@ import (
func TestNewContainer(t *testing.T) { func TestNewContainer(t *testing.T) {
c := container.New() c := container.New()
nonce, err := uuid.New().MarshalBinary() nonce := uuid.New()
require.NoError(t, err)
wallet, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(1).PublicKey) wallet, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(1).PublicKey)
require.NoError(t, err) require.NoError(t, err)
@ -29,7 +28,7 @@ func TestNewContainer(t *testing.T) {
c.SetBasicACL(acl.PublicBasicRule) c.SetBasicACL(acl.PublicBasicRule)
c.SetAttributes(generateAttributes(5)) c.SetAttributes(generateAttributes(5))
c.SetPlacementPolicy(policy) c.SetPlacementPolicy(policy)
c.SetNonce(nonce) c.SetNonceUUID(nonce)
c.SetOwnerID(ownerID) c.SetOwnerID(ownerID)
c.SetVersion(pkg.SDKVersion()) c.SetVersion(pkg.SDKVersion())
@ -39,7 +38,11 @@ func TestNewContainer(t *testing.T) {
require.EqualValues(t, newContainer.PlacementPolicy(), policy) require.EqualValues(t, newContainer.PlacementPolicy(), policy)
require.EqualValues(t, newContainer.Attributes(), generateAttributes(5)) require.EqualValues(t, newContainer.Attributes(), generateAttributes(5))
require.EqualValues(t, newContainer.BasicACL(), acl.PublicBasicRule) require.EqualValues(t, newContainer.BasicACL(), acl.PublicBasicRule)
require.EqualValues(t, newContainer.Nonce(), nonce)
newNonce, err := newContainer.NonceUUID()
require.NoError(t, err)
require.EqualValues(t, newNonce, nonce)
require.EqualValues(t, newContainer.OwnerID(), ownerID) require.EqualValues(t, newContainer.OwnerID(), ownerID)
require.EqualValues(t, newContainer.Version(), pkg.SDKVersion()) require.EqualValues(t, newContainer.Version(), pkg.SDKVersion())
} }