forked from TrueCloudLab/frostfs-api-go
[#168] refs: Implement binary/JSON encoders/decoders on Address
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
9325e22871
commit
4ce751f13c
7 changed files with 114 additions and 33 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -60,3 +60,29 @@ func TestAddress_Parse(t *testing.T) {
|
|||
require.EqualError(t, NewAddress().Parse(s), ErrBadID.Error())
|
||||
})
|
||||
}
|
||||
|
||||
func TestAddressEncoding(t *testing.T) {
|
||||
a := NewAddress()
|
||||
a.SetObjectID(randID(t))
|
||||
a.SetContainerID(randCID(t))
|
||||
|
||||
t.Run("binary", func(t *testing.T) {
|
||||
data, err := a.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
a2 := NewAddress()
|
||||
require.NoError(t, a2.Unmarshal(data))
|
||||
|
||||
require.Equal(t, a, a2)
|
||||
})
|
||||
|
||||
t.Run("json", func(t *testing.T) {
|
||||
data, err := a.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
a2 := NewAddress()
|
||||
require.NoError(t, a2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, a, a2)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -20,6 +20,13 @@ func randID(t *testing.T) *ID {
|
|||
return id
|
||||
}
|
||||
|
||||
func randCID(t *testing.T) *container.ID {
|
||||
id := container.NewID()
|
||||
id.SetSHA256(randSHA256Checksum(t))
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
func randSHA256Checksum(t *testing.T) (cs [sha256.Size]byte) {
|
||||
_, err := rand.Read(cs[:])
|
||||
require.NoError(t, err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue