[#140] sdk: Refactor reference types

Refactor v2-compatible reference types to be wrappers over corresponding
types from v2.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-10 12:57:29 +03:00 committed by Stanislav Bogatyrev
parent 2026473733
commit 524280a5e8
10 changed files with 73 additions and 215 deletions

View file

@ -140,14 +140,7 @@ func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, op
return nil, errors.Wrap(err, "can't verify response message") return nil, errors.Wrap(err, "can't verify response message")
} }
cidV2 := resp.GetBody().GetContainerID() return container.NewIDFromV2(resp.GetBody().GetContainerID()), nil
cid, err := container.IDFromV2(cidV2)
if err != nil {
return nil, errors.Wrapf(err, "could not convert %T to %T", cidV2, cid)
}
return cid, nil
default: default:
return nil, unsupportedProtocolErr return nil, unsupportedProtocolErr
} }
@ -235,12 +228,7 @@ func (c Client) listContainerV2(ctx context.Context, owner *owner.ID, opts ...Ca
result := make([]*container.ID, 0, len(resp.GetBody().GetContainerIDs())) result := make([]*container.ID, 0, len(resp.GetBody().GetContainerIDs()))
for _, cidV2 := range resp.GetBody().GetContainerIDs() { for _, cidV2 := range resp.GetBody().GetContainerIDs() {
cid, err := container.IDFromV2(cidV2) result = append(result, container.NewIDFromV2(cidV2))
if err != nil {
return nil, errors.Wrapf(err, "could not convert %T to %T", cidV2, cid)
}
result = append(result, cid)
} }
return result, nil return result, nil

View file

@ -262,10 +262,7 @@ func (c *Client) putObjectV2(ctx context.Context, p *PutObjectParams, opts ...Ca
} }
// convert object identifier // convert object identifier
id, err := object.IDFromV2(resp.GetBody().GetObjectID()) id := object.NewIDFromV2(resp.GetBody().GetObjectID())
if err != nil {
return nil, errors.Wrap(err, "could not convert object identifier")
}
return id, nil return id, nil
} }

View file

@ -4,44 +4,29 @@ import (
"crypto/sha256" "crypto/sha256"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
) )
// ID represents container identifier // ID represents v2-compatible container identifier.
// that supports different type of values. type ID refs.ContainerID
type ID struct {
val []byte // NewIDFromV2 wraps v2 ContainerID message to ID.
func NewIDFromV2(idV2 *refs.ContainerID) *ID {
return (*ID)(idV2)
}
// NewID creates and initializes blank ID.
//
// Works similar to NewIDFromV2(new(ContainerID)).
func NewID() *ID {
return NewIDFromV2(new(refs.ContainerID))
} }
// SetSHA256 sets container identifier value to SHA256 checksum. // SetSHA256 sets container identifier value to SHA256 checksum.
func (id *ID) SetSHA256(v [sha256.Size]byte) { func (id *ID) SetSHA256(v [sha256.Size]byte) {
if id != nil { (*refs.ContainerID)(id).SetValue(v[:])
id.val = v[:]
}
} }
// ToV2 returns the v2 container ID message. // ToV2 returns the v2 container ID message.
func (id *ID) ToV2() *refs.ContainerID { func (id *ID) ToV2() *refs.ContainerID {
if id != nil { return (*refs.ContainerID)(id)
idV2 := new(refs.ContainerID)
idV2.SetValue(id.val)
return idV2
}
return nil
}
func IDFromV2(idV2 *refs.ContainerID) (*ID, error) {
val := idV2.GetValue()
if ln := len(val); ln != sha256.Size {
return nil, errors.Errorf(
"could not convert %T to %T: expected length %d, received %d",
idV2, (*ID)(nil), sha256.Size, ln,
)
}
return &ID{
val: val,
}, nil
} }

View file

@ -9,7 +9,7 @@ import (
) )
func TestIDV2_0(t *testing.T) { func TestIDV2_0(t *testing.T) {
cid := new(ID) cid := NewID()
checksum := [sha256.Size]byte{} checksum := [sha256.Size]byte{}
@ -20,8 +20,5 @@ func TestIDV2_0(t *testing.T) {
cidV2 := cid.ToV2() cidV2 := cid.ToV2()
cid2, err := IDFromV2(cidV2) require.Equal(t, checksum[:], cidV2.GetValue())
require.NoError(t, err)
require.Equal(t, cid, cid2)
} }

View file

@ -1,60 +1,28 @@
package object package object
import ( import (
"github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Address represents address of NeoFS object. // Address represents v2-compatible object address.
type Address struct { type Address refs.Address
cid *container.ID
oid *ID // NewAddressFromV2 converts v2 Address message to Address.
func NewAddressFromV2(aV2 *refs.Address) *Address {
return (*Address)(aV2)
}
// NewAddress creates and initializes blank Address.
//
// Works similar as NewAddressFromV2(new(Address)).
func NewAddress() *Address {
return NewAddressFromV2(new(refs.Address))
} }
// ToV2 converts Address to v2 Address message. // ToV2 converts Address to v2 Address message.
func (a *Address) ToV2() *refs.Address { func (a *Address) ToV2() *refs.Address {
if a != nil { return (*refs.Address)(a)
aV2 := new(refs.Address)
aV2.SetContainerID(a.cid.ToV2())
aV2.SetObjectID(a.oid.ToV2())
return aV2
}
return nil
}
// GetObjectIDV2 converts object identifier to v2 ObjectID message.
func (a *Address) GetObjectIDV2() *refs.ObjectID {
if a != nil {
return a.oid.ToV2()
}
return nil
}
// AddressFromV2 converts v2 Address message to Address.
func AddressFromV2(aV2 *refs.Address) (*Address, error) {
if aV2 == nil {
return nil, nil
}
oid, err := IDFromV2(aV2.GetObjectID())
if err != nil {
return nil, errors.Wrap(err, "could not convert object identifier")
}
cid, err := container.IDFromV2(aV2.GetContainerID())
if err != nil {
return nil, errors.Wrap(err, "could not convert container identifier")
}
return &Address{
cid: cid,
oid: oid,
}, nil
} }
// AddressFromBytes restores Address from a binary representation. // AddressFromBytes restores Address from a binary representation.
@ -64,5 +32,5 @@ func AddressFromBytes(data []byte) (*Address, error) {
return nil, errors.Wrap(err, "could not unmarshal object address") return nil, errors.Wrap(err, "could not unmarshal object address")
} }
return AddressFromV2(addrV2) return NewAddressFromV2(addrV2), nil
} }

View file

@ -4,47 +4,29 @@ import (
"crypto/sha256" "crypto/sha256"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
) )
// ID represents object identifier that // ID represents v2-compatible object identifier.
// supports different type of values. type ID refs.ObjectID
type ID struct {
val []byte // NewIDFromV2 wraps v2 ObjectID message to ID.
func NewIDFromV2(idV2 *refs.ObjectID) *ID {
return (*ID)(idV2)
}
// NewID creates and initializes blank ID.
//
// Works similar as NewIDFromV2(new(ObjectID)).
func NewID() *ID {
return NewIDFromV2(new(refs.ObjectID))
} }
// SetSHA256 sets object identifier value to SHA256 checksum. // SetSHA256 sets object identifier value to SHA256 checksum.
func (id *ID) SetSHA256(v [sha256.Size]byte) { func (id *ID) SetSHA256(v [sha256.Size]byte) {
if id != nil { (*refs.ObjectID)(id).SetValue(v[:])
id.val = v[:]
}
} }
// ToV2 converts ID to v2 ObjectID message. // ToV2 converts ID to v2 ObjectID message.
func (id *ID) ToV2() *refs.ObjectID { func (id *ID) ToV2() *refs.ObjectID {
if id != nil { return (*refs.ObjectID)(id)
idV2 := new(refs.ObjectID)
idV2.SetValue(id.val)
return idV2
}
return nil
}
// IDFromV2 converts v2 ObjectID message to ID.
//
// Returns an error if the format of the identifier
// in the message is broken.
func IDFromV2(idV2 *refs.ObjectID) (*ID, error) {
val := idV2.GetValue()
if ln := len(val); ln != sha256.Size {
return nil, errors.Errorf("could not convert %T to %T: invalid length %d",
idV2, (*ID)(nil), ln,
)
}
return &ID{
val: val,
}, nil
} }

View file

@ -9,7 +9,7 @@ import (
) )
func TestIDV2(t *testing.T) { func TestIDV2(t *testing.T) {
id := new(ID) id := NewID()
checksum := [sha256.Size]byte{} checksum := [sha256.Size]byte{}
@ -20,8 +20,5 @@ func TestIDV2(t *testing.T) {
idV2 := id.ToV2() idV2 := id.ToV2()
id2, err := IDFromV2(idV2) require.Equal(t, checksum[:], idV2.GetValue())
require.NoError(t, err)
require.Equal(t, id, id2)
} }

View file

@ -59,7 +59,7 @@ func (o *Object) Verify() error {
hdrChecksum := sha256.Sum256(data) hdrChecksum := sha256.Sum256(data)
if !bytes.Equal(hdrChecksum[:], o.id.val) { if !bytes.Equal(hdrChecksum[:], o.id.ToV2().GetValue()) {
return errors.New("invalid object identifier") return errors.New("invalid object identifier")
} }
@ -77,18 +77,6 @@ func (o *Object) Verify() error {
return nil return nil
} }
// Address returns address of the object.
func (o *Object) Address() *Address {
if o != nil {
return &Address{
cid: o.cid,
oid: o.id,
}
}
return nil
}
// GetPayload returns object payload bytes. // GetPayload returns object payload bytes.
func (o *Object) GetPayload() []byte { func (o *Object) GetPayload() []byte {
if o != nil { if o != nil {
@ -201,23 +189,8 @@ func FromV2(oV2 *object.Object) (*Object, error) {
return nil, nil return nil, nil
} }
id, err := IDFromV2(oV2.GetObjectID())
if err != nil {
return nil, errors.Wrap(err, "could not convert object ID")
}
hdr := oV2.GetHeader() hdr := oV2.GetHeader()
ownerID, err := owner.IDFromV2(hdr.GetOwnerID())
if err != nil {
return nil, errors.Wrap(err, "could not convert owner ID")
}
cid, err := container.IDFromV2(hdr.GetContainerID())
if err != nil {
return nil, errors.Wrap(err, "could not convert container ID")
}
// TODO: convert other fields // TODO: convert other fields
sig := oV2.GetSignature() sig := oV2.GetSignature()
@ -225,11 +198,11 @@ func FromV2(oV2 *object.Object) (*Object, error) {
return &Object{ return &Object{
rwObject: rwObject{ rwObject: rwObject{
fin: true, fin: true,
id: id, id: NewIDFromV2(oV2.GetObjectID()),
key: sig.GetKey(), key: sig.GetKey(),
sig: sig.GetSign(), sig: sig.GetSign(),
cid: cid, cid: container.NewIDFromV2(hdr.GetContainerID()),
ownerID: ownerID, ownerID: owner.NewIDFromV2(hdr.GetOwnerID()),
payloadChecksum: hdr.GetPayloadHash(), payloadChecksum: hdr.GetPayloadHash(),
payload: oV2.GetPayload(), payload: oV2.GetPayload(),
}, },

View file

@ -1,64 +1,38 @@
package owner package owner
import ( import (
"crypto/sha256"
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
) )
// ID represents owner identifier that // ID represents v2-compatible owner identifier.
// supports different type of values. type ID refs.OwnerID
type ID struct {
val []byte // NewIDFromV2 wraps v2 OwnerID message to ID.
func NewIDFromV2(idV2 *refs.OwnerID) *ID {
return (*ID)(idV2)
}
// NewID creates and initializes blank ID.
//
// Works similar as NewIDFromV2(new(OwnerID)).
func NewID() *ID {
return NewIDFromV2(new(refs.OwnerID))
} }
// SetNeo3Wallet sets owner identifier value to NEO3 wallet address. // SetNeo3Wallet sets owner identifier value to NEO3 wallet address.
func (id *ID) SetNeo3Wallet(v *NEO3Wallet) { func (id *ID) SetNeo3Wallet(v *NEO3Wallet) {
if id != nil { (*refs.OwnerID)(id).SetValue(v.Bytes())
id.val = v.Bytes()
}
} }
// ToV2 returns the v2 owner ID message. // ToV2 returns the v2 owner ID message.
func (id *ID) ToV2() *refs.OwnerID { func (id *ID) ToV2() *refs.OwnerID {
if id != nil { return (*refs.OwnerID)(id)
idV2 := new(refs.OwnerID)
idV2.SetValue(id.val)
return idV2
}
return nil
}
// IDFromV2 converts owner ID v2 structure to ID.
func IDFromV2(idV2 *refs.OwnerID) (*ID, error) {
if idV2 == nil {
return nil, nil
}
val := idV2.GetValue()
if ln := len(val); ln != 25 {
return nil, errors.Errorf(
"could not convert %T to %T: expected length %d, received %d",
idV2, (*ID)(nil), sha256.Size, ln,
)
}
return &ID{
val: val,
}, nil
} }
func (id *ID) String() string { func (id *ID) String() string {
if id != nil { return base58.Encode((*refs.OwnerID)(id).GetValue())
return base58.Encode(id.val)
}
return ""
} }
func ScriptHashBE(id *ID) ([]byte, error) { func ScriptHashBE(id *ID) ([]byte, error) {

View file

@ -8,7 +8,7 @@ import (
) )
func TestIDV2(t *testing.T) { func TestIDV2(t *testing.T) {
id := new(ID) id := NewID()
wallet := new(NEO3Wallet) wallet := new(NEO3Wallet)
@ -19,8 +19,5 @@ func TestIDV2(t *testing.T) {
idV2 := id.ToV2() idV2 := id.ToV2()
id2, err := IDFromV2(idV2) require.Equal(t, wallet.Bytes(), idV2.GetValue())
require.NoError(t, err)
require.Equal(t, id, id2)
} }