[#168] object: Implement binary/JSON encoders/decoders on Object

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 15:51:27 +03:00 committed by Alex Vanin
parent 9cdd14841a
commit 9325e22871
7 changed files with 102 additions and 15 deletions

View file

@ -37,14 +37,3 @@ func (o *Object) ToV2() *object.Object {
return nil
}
// FromBytes restores Object instance from a binary representation.
func FromBytes(data []byte) (*Object, error) {
oV2 := new(object.Object)
if err := oV2.StableUnmarshal(data); err != nil {
return nil, err
}
return NewFromV2(oV2), nil
}

View file

@ -287,3 +287,28 @@ func TestRwObject_HasParent(t *testing.T) {
require.False(t, obj.HasParent())
}
func TestRWObjectEncoding(t *testing.T) {
o := NewRaw()
o.SetID(randID(t))
t.Run("binary", func(t *testing.T) {
data, err := o.Marshal()
require.NoError(t, err)
o2 := NewRaw()
require.NoError(t, o2.Unmarshal(data))
require.Equal(t, o, o2)
})
t.Run("json", func(t *testing.T) {
data, err := o.MarshalJSON()
require.NoError(t, err)
o2 := NewRaw()
require.NoError(t, o2.UnmarshalJSON(data))
require.Equal(t, o, o2)
})
}

View file

@ -352,3 +352,35 @@ func (o *rwObject) HasParent() bool {
GetHeader().
GetSplit() != nil
}
// Marshal marshals object into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (o *rwObject) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return (*object.Object)(o).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of object.
func (o *rwObject) Unmarshal(data []byte) error {
return (*object.Object)(o).
Unmarshal(data)
}
// MarshalJSON encodes object to protobuf JSON format.
func (o *rwObject) MarshalJSON() ([]byte, error) {
return (*object.Object)(o).
MarshalJSON()
}
// UnmarshalJSON decodes object from protobuf JSON format.
func (o *rwObject) UnmarshalJSON(data []byte) error {
return (*object.Object)(o).
UnmarshalJSON(data)
}

View file

@ -104,3 +104,23 @@ func (h *HeaderWithSignature) UnmarshalJSON(data []byte) error {
return nil
}
func (o *Object) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
ObjectToGRPCMessage(o),
)
}
func (o *Object) UnmarshalJSON(data []byte) error {
msg := new(object.Object)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*o = *ObjectFromGRPCMessage(msg)
return nil
}

View file

@ -66,3 +66,15 @@ func TestHeaderWithSignatureJSON(t *testing.T) {
require.Equal(t, h, h2)
}
func TestObjectJSON(t *testing.T) {
o := generateObject("data")
data, err := o.MarshalJSON()
require.NoError(t, err)
o2 := new(object.Object)
require.NoError(t, o2.UnmarshalJSON(data))
require.Equal(t, o, o2)
}

View file

@ -557,6 +557,17 @@ func (o *Object) StableUnmarshal(data []byte) error {
return nil
}
func (o *Object) Unmarshal(data []byte) error {
m := new(object.Object)
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
*o = *ObjectFromGRPCMessage(m)
return nil
}
func (r *GetRequestBody) StableMarshal(buf []byte) ([]byte, error) {
if r == nil {
return []byte{}, nil

View file

@ -78,16 +78,14 @@ func TestHeader_StableMarshal(t *testing.T) {
func TestObject_StableMarshal(t *testing.T) {
from := generateObject("Payload")
transport := new(grpc.Object)
t.Run("non empty", func(t *testing.T) {
wire, err := from.StableMarshal(nil)
require.NoError(t, err)
err = goproto.Unmarshal(wire, transport)
require.NoError(t, err)
to := new(object.Object)
require.NoError(t, to.Unmarshal(wire))
to := object.ObjectFromGRPCMessage(transport)
require.Equal(t, from, to)
})
}