2020-09-02 11:43:37 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2020-10-14 19:08:27 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/internal"
|
2020-09-17 10:16:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2020-09-02 11:43:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
// Address represents v2-compatible object address.
|
|
|
|
type Address refs.Address
|
2020-09-02 11:43:37 +00:00
|
|
|
|
2020-10-14 19:08:27 +00:00
|
|
|
// ErrBadAddress returns when string representation doesn't
|
|
|
|
// contains Container.ID and Object.ID separated by `/`.
|
|
|
|
const ErrBadAddress = internal.Error("address should contains container.ID and object.ID separated with `/`")
|
|
|
|
|
|
|
|
const (
|
|
|
|
addressParts = 2
|
|
|
|
addressSeparator = "/"
|
|
|
|
)
|
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
// NewAddressFromV2 converts v2 Address message to Address.
|
|
|
|
func NewAddressFromV2(aV2 *refs.Address) *Address {
|
|
|
|
return (*Address)(aV2)
|
2020-09-02 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
// NewAddress creates and initializes blank Address.
|
|
|
|
//
|
|
|
|
// Works similar as NewAddressFromV2(new(Address)).
|
|
|
|
func NewAddress() *Address {
|
|
|
|
return NewAddressFromV2(new(refs.Address))
|
2020-09-02 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
// ToV2 converts Address to v2 Address message.
|
|
|
|
func (a *Address) ToV2() *refs.Address {
|
|
|
|
return (*refs.Address)(a)
|
2020-09-02 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddressFromBytes restores Address from a binary representation.
|
|
|
|
func AddressFromBytes(data []byte) (*Address, error) {
|
|
|
|
addrV2 := new(refs.Address)
|
|
|
|
if err := addrV2.StableUnmarshal(data); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not unmarshal object address")
|
|
|
|
}
|
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
return NewAddressFromV2(addrV2), nil
|
2020-09-02 11:43:37 +00:00
|
|
|
}
|
2020-09-17 10:16:43 +00:00
|
|
|
|
|
|
|
// GetContainerID returns container identifier.
|
|
|
|
func (a *Address) GetContainerID() *container.ID {
|
|
|
|
return container.NewIDFromV2(
|
|
|
|
(*refs.Address)(a).GetContainerID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID sets container identifier.
|
|
|
|
func (a *Address) SetContainerID(id *container.ID) {
|
|
|
|
(*refs.Address)(a).SetContainerID(id.ToV2())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectID returns object identifier.
|
|
|
|
func (a *Address) GetObjectID() *ID {
|
|
|
|
return NewIDFromV2(
|
|
|
|
(*refs.Address)(a).GetObjectID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetObjectID sets object identifier.
|
|
|
|
func (a *Address) SetObjectID(id *ID) {
|
|
|
|
(*refs.Address)(a).SetObjectID(id.ToV2())
|
|
|
|
}
|
2020-10-14 19:08:27 +00:00
|
|
|
|
|
|
|
// Parse converts base58 string representation into Address.
|
|
|
|
func (a *Address) Parse(s string) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
oid = NewID()
|
|
|
|
cid = container.NewID()
|
|
|
|
parts = strings.Split(s, addressSeparator)
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(parts) != addressParts {
|
|
|
|
return ErrBadAddress
|
|
|
|
} else if err = cid.Parse(parts[0]); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err = oid.Parse(parts[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.SetObjectID(oid)
|
|
|
|
a.SetContainerID(cid)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of Object.Address.
|
|
|
|
func (a *Address) String() string {
|
|
|
|
return strings.Join([]string{
|
|
|
|
a.GetContainerID().String(),
|
|
|
|
a.GetObjectID().String(),
|
|
|
|
}, addressSeparator)
|
|
|
|
}
|