diff --git a/v2/object/json.go b/v2/object/json.go index 19c0018b..3cf35848 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 917dc7a0..87d05c98 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 9a5eec29..0728cb6f 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 62f7d09b..dbaf2f35 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)