2020-09-02 11:43:37 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2020-11-16 13:33:41 +00:00
|
|
|
"errors"
|
2020-10-14 19:08:27 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-09-02 11:43:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
)
|
|
|
|
|
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-11-16 13:33:41 +00:00
|
|
|
var errInvalidAddressString = errors.New("incorrect format of the string object address")
|
2020-10-14 19:08:27 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
addressParts = 2
|
|
|
|
addressSeparator = "/"
|
|
|
|
)
|
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
// NewAddressFromV2 converts v2 Address message to Address.
|
2021-06-08 15:44:47 +00:00
|
|
|
//
|
|
|
|
// Nil refs.Address converts to nil.
|
2020-09-10 09:57:29 +00:00
|
|
|
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.
|
2021-06-08 15:44:47 +00:00
|
|
|
//
|
|
|
|
// Nil Address converts to nil.
|
2020-09-10 09:57:29 +00:00
|
|
|
func (a *Address) ToV2() *refs.Address {
|
|
|
|
return (*refs.Address)(a)
|
2020-09-02 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 08:42:19 +00:00
|
|
|
// ContainerID returns container identifier.
|
2021-05-31 12:09:04 +00:00
|
|
|
func (a *Address) ContainerID() *cid.ID {
|
|
|
|
return cid.NewFromV2(
|
2020-09-17 10:16:43 +00:00
|
|
|
(*refs.Address)(a).GetContainerID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID sets container identifier.
|
2021-05-31 12:09:04 +00:00
|
|
|
func (a *Address) SetContainerID(id *cid.ID) {
|
2020-09-17 10:16:43 +00:00
|
|
|
(*refs.Address)(a).SetContainerID(id.ToV2())
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:42:19 +00:00
|
|
|
// ObjectID returns object identifier.
|
|
|
|
func (a *Address) ObjectID() *ID {
|
2020-09-17 10:16:43 +00:00
|
|
|
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()
|
2021-05-31 12:09:04 +00:00
|
|
|
id = cid.New()
|
2020-10-14 19:08:27 +00:00
|
|
|
parts = strings.Split(s, addressSeparator)
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(parts) != addressParts {
|
2020-11-16 13:33:41 +00:00
|
|
|
return errInvalidAddressString
|
2021-05-31 12:09:04 +00:00
|
|
|
} else if err = id.Parse(parts[0]); err != nil {
|
2020-10-14 19:08:27 +00:00
|
|
|
return err
|
|
|
|
} else if err = oid.Parse(parts[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.SetObjectID(oid)
|
2021-05-31 12:09:04 +00:00
|
|
|
a.SetContainerID(id)
|
2020-10-14 19:08:27 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of Object.Address.
|
|
|
|
func (a *Address) String() string {
|
|
|
|
return strings.Join([]string{
|
2020-11-16 08:42:19 +00:00
|
|
|
a.ContainerID().String(),
|
|
|
|
a.ObjectID().String(),
|
2020-10-14 19:08:27 +00:00
|
|
|
}, addressSeparator)
|
|
|
|
}
|
2020-11-13 12:58:53 +00:00
|
|
|
|
|
|
|
// Marshal marshals Address into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (a *Address) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*refs.Address)(a).
|
|
|
|
StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of Address.
|
|
|
|
func (a *Address) Unmarshal(data []byte) error {
|
|
|
|
return (*refs.Address)(a).
|
|
|
|
Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes Address to protobuf JSON format.
|
|
|
|
func (a *Address) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*refs.Address)(a).
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes Address from protobuf JSON format.
|
|
|
|
func (a *Address) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*refs.Address)(a).
|
|
|
|
UnmarshalJSON(data)
|
|
|
|
}
|