[#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

@ -1,60 +1,28 @@
package object
import (
"github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
)
// Address represents address of NeoFS object.
type Address struct {
cid *container.ID
// Address represents v2-compatible object address.
type Address refs.Address
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.
func (a *Address) ToV2() *refs.Address {
if a != nil {
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
return (*refs.Address)(a)
}
// 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 AddressFromV2(addrV2)
return NewAddressFromV2(addrV2), nil
}