forked from TrueCloudLab/frostfs-api-go
[#168] object: Implement binary/JSON encoders/decoders on Header
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
78374caa41
commit
b8f86f1a60
4 changed files with 45 additions and 5 deletions
|
@ -64,3 +64,23 @@ func (h *SplitHeader) UnmarshalJSON(data []byte) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Header) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
HeaderToGRPCMessage(h),
|
||||
)
|
||||
}
|
||||
|
||||
func (h *Header) UnmarshalJSON(data []byte) error {
|
||||
msg := new(object.Header)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*h = *HeaderFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -42,3 +42,15 @@ func TestSplitHeaderJSON(t *testing.T) {
|
|||
|
||||
require.Equal(t, h, h2)
|
||||
}
|
||||
|
||||
func TestHeaderJSON(t *testing.T) {
|
||||
h := generateHeader(10)
|
||||
|
||||
data, err := h.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
h2 := new(object.Header)
|
||||
require.NoError(t, h2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, h, h2)
|
||||
}
|
||||
|
|
|
@ -422,6 +422,17 @@ func (h *Header) StableSize() (size int) {
|
|||
return size
|
||||
}
|
||||
|
||||
func (h *Header) Unmarshal(data []byte) error {
|
||||
m := new(object.Header)
|
||||
if err := goproto.Unmarshal(data, m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*h = *HeaderFromGRPCMessage(m)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HeaderWithSignature) StableMarshal(buf []byte) ([]byte, error) {
|
||||
if h == nil {
|
||||
return []byte{}, nil
|
||||
|
|
|
@ -65,16 +65,13 @@ func TestHeader_StableMarshal(t *testing.T) {
|
|||
from := generateHeader(500)
|
||||
from.SetSplit(split)
|
||||
|
||||
transport := new(grpc.Header)
|
||||
|
||||
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.Header)
|
||||
require.NoError(t, to.Unmarshal(wire))
|
||||
|
||||
to := object.HeaderFromGRPCMessage(transport)
|
||||
require.Equal(t, from, to)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue