From 9cdd14841a59f05c02eb373224f563a2dde7e3bf Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 15:43:02 +0300 Subject: [PATCH] [#168] object: Implement binary/JSON encoders/decoders on HeaderWithSig Signed-off-by: Leonard Lyubich --- v2/object/json.go | 20 ++++++++++++++++++++ v2/object/json_test.go | 12 ++++++++++++ v2/object/marshal.go | 11 +++++++++++ v2/object/marshal_test.go | 28 +++++++++++++++++++++++----- 4 files changed, 66 insertions(+), 5 deletions(-) diff --git a/v2/object/json.go b/v2/object/json.go index 19c0018..3cf3584 100644 --- a/v2/object/json.go +++ b/v2/object/json.go @@ -84,3 +84,23 @@ func (h *Header) UnmarshalJSON(data []byte) error { return nil } + +func (h *HeaderWithSignature) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + HeaderWithSignatureToGRPCMessage(h), + ) +} + +func (h *HeaderWithSignature) UnmarshalJSON(data []byte) error { + msg := new(object.HeaderWithSignature) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *h = *HeaderWithSignatureFromGRPCMessage(msg) + + return nil +} diff --git a/v2/object/json_test.go b/v2/object/json_test.go index 917dc7a..87d05c9 100644 --- a/v2/object/json_test.go +++ b/v2/object/json_test.go @@ -54,3 +54,15 @@ func TestHeaderJSON(t *testing.T) { require.Equal(t, h, h2) } + +func TestHeaderWithSignatureJSON(t *testing.T) { + h := generateHeaderWithSignature() + + data, err := h.MarshalJSON() + require.NoError(t, err) + + h2 := new(object.HeaderWithSignature) + require.NoError(t, h2.UnmarshalJSON(data)) + + require.Equal(t, h, h2) +} diff --git a/v2/object/marshal.go b/v2/object/marshal.go index 9a5eec2..0728cb6 100644 --- a/v2/object/marshal.go +++ b/v2/object/marshal.go @@ -475,6 +475,17 @@ func (h *HeaderWithSignature) StableSize() (size int) { return size } +func (h *HeaderWithSignature) Unmarshal(data []byte) error { + m := new(object.HeaderWithSignature) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *h = *HeaderWithSignatureFromGRPCMessage(m) + + return nil +} + func (o *Object) StableMarshal(buf []byte) ([]byte, error) { if o == nil { return []byte{}, nil diff --git a/v2/object/marshal_test.go b/v2/object/marshal_test.go index 62f7d09..dbaf2f3 100644 --- a/v2/object/marshal_test.go +++ b/v2/object/marshal_test.go @@ -352,6 +352,20 @@ func TestGetRangeHashResponseBody_StableMarshal(t *testing.T) { }) } +func TestHeaderWithSignature_StableMarshal(t *testing.T) { + from := generateHeaderWithSignature() + + t.Run("non empty", func(t *testing.T) { + wire, err := from.StableMarshal(nil) + require.NoError(t, err) + + to := new(object.HeaderWithSignature) + require.NoError(t, to.Unmarshal(wire)) + + require.Equal(t, from, to) + }) +} + func generateOwner(id string) *refs.OwnerID { owner := new(refs.OwnerID) owner.SetValue([]byte(id)) @@ -582,12 +596,8 @@ func generateHeadResponseBody(flag bool) *object.HeadResponseBody { short.SetShortHeader(generateShortHeader("short id")) part = short } else { - hdrWithSig := new(object.HeaderWithSignature) - hdrWithSig.SetHeader(generateHeader(30)) - hdrWithSig.SetSignature(generateSignature("sig", "key")) - full := new(object.GetHeaderPartFull) - full.SetHeaderWithSignature(hdrWithSig) + full.SetHeaderWithSignature(generateHeaderWithSignature()) part = full } @@ -596,6 +606,14 @@ func generateHeadResponseBody(flag bool) *object.HeadResponseBody { return req } +func generateHeaderWithSignature() *object.HeaderWithSignature { + hdrWithSig := new(object.HeaderWithSignature) + hdrWithSig.SetHeader(generateHeader(30)) + hdrWithSig.SetSignature(generateSignature("sig", "key")) + + return hdrWithSig +} + func generateFilter(k, v string) *object.SearchFilter { f := new(object.SearchFilter) f.SetKey(k)