[#168] object: Implement binary/JSON encoders/decoders on SplitHeader
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
0caff85fb7
commit
78374caa41
4 changed files with 45 additions and 5 deletions
|
@ -44,3 +44,23 @@ func (a *Attribute) UnmarshalJSON(data []byte) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *SplitHeader) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
SplitHeaderToGRPCMessage(h),
|
||||
)
|
||||
}
|
||||
|
||||
func (h *SplitHeader) UnmarshalJSON(data []byte) error {
|
||||
msg := new(object.Header_Split)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*h = *SplitHeaderFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -30,3 +30,15 @@ func TestAttributeJSON(t *testing.T) {
|
|||
|
||||
require.Equal(t, a, a2)
|
||||
}
|
||||
|
||||
func TestSplitHeaderJSON(t *testing.T) {
|
||||
h := generateSplit("sig")
|
||||
|
||||
data, err := h.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
h2 := new(object.SplitHeader)
|
||||
require.NoError(t, h2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, h, h2)
|
||||
}
|
||||
|
|
|
@ -295,6 +295,17 @@ func (h *SplitHeader) StableSize() (size int) {
|
|||
return size
|
||||
}
|
||||
|
||||
func (h *SplitHeader) Unmarshal(data []byte) error {
|
||||
m := new(object.Header_Split)
|
||||
if err := goproto.Unmarshal(data, m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*h = *SplitHeaderFromGRPCMessage(m)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Header) StableMarshal(buf []byte) ([]byte, error) {
|
||||
if h == nil {
|
||||
return []byte{}, nil
|
||||
|
|
|
@ -46,16 +46,13 @@ func TestSplitHeader_StableMarshal(t *testing.T) {
|
|||
hdr := generateHeader(123)
|
||||
from.SetParentHeader(hdr)
|
||||
|
||||
transport := new(grpc.Header_Split)
|
||||
|
||||
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.SplitHeader)
|
||||
require.NoError(t, to.Unmarshal(wire))
|
||||
|
||||
to := object.SplitHeaderFromGRPCMessage(transport)
|
||||
require.Equal(t, from, to)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue