[#168] refs: Implement binary/JSON encoders/decoders on Address

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 15:58:53 +03:00 committed by Alex Vanin
parent 9325e22871
commit 4ce751f13c
7 changed files with 114 additions and 33 deletions

View file

@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neofs-api-go/internal"
"github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
)
// Address represents v2-compatible object address.
@ -38,16 +37,6 @@ func (a *Address) ToV2() *refs.Address {
return (*refs.Address)(a)
}
// 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")
}
return NewAddressFromV2(addrV2), nil
}
// GetContainerID returns container identifier.
func (a *Address) GetContainerID() *container.ID {
return container.NewIDFromV2(
@ -102,3 +91,35 @@ func (a *Address) String() string {
a.GetObjectID().String(),
}, addressSeparator)
}
// 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)
}