forked from TrueCloudLab/frostfs-api-go
[#168] object: Implement binary/JSON encoders/decoders on Object
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
9cdd14841a
commit
9325e22871
7 changed files with 102 additions and 15 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue