[#168] object: Implement binary/JSON encoders/decoders on Attribute

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 15:31:15 +03:00 committed by Alex Vanin
parent ce0d70fa02
commit 0caff85fb7
6 changed files with 103 additions and 4 deletions

View file

@ -43,3 +43,35 @@ func (a *Attribute) SetValue(v string) {
func (a *Attribute) ToV2() *object.Attribute {
return (*object.Attribute)(a)
}
// Marshal marshals Attribute into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (d *Attribute) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return (*object.Attribute)(d).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of Attribute.
func (d *Attribute) Unmarshal(data []byte) error {
return (*object.Attribute)(d).
Unmarshal(data)
}
// MarshalJSON encodes Attribute to protobuf JSON format.
func (d *Attribute) MarshalJSON() ([]byte, error) {
return (*object.Attribute)(d).
MarshalJSON()
}
// UnmarshalJSON decodes Attribute from protobuf JSON format.
func (d *Attribute) UnmarshalJSON(data []byte) error {
return (*object.Attribute)(d).
UnmarshalJSON(data)
}

View file

@ -21,3 +21,29 @@ func TestAttribute(t *testing.T) {
require.Equal(t, key, aV2.GetKey())
require.Equal(t, val, aV2.GetValue())
}
func TestAttributeEncoding(t *testing.T) {
a := NewAttribute()
a.SetKey("key")
a.SetValue("value")
t.Run("binary", func(t *testing.T) {
data, err := a.Marshal()
require.NoError(t, err)
a2 := NewAttribute()
require.NoError(t, a2.Unmarshal(data))
require.Equal(t, a, a2)
})
t.Run("json", func(t *testing.T) {
data, err := a.MarshalJSON()
require.NoError(t, err)
a2 := NewAttribute()
require.NoError(t, a2.UnmarshalJSON(data))
require.Equal(t, a, a2)
})
}

View file

@ -24,3 +24,23 @@ func (h *ShortHeader) UnmarshalJSON(data []byte) error {
return nil
}
func (a *Attribute) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
AttributeToGRPCMessage(a),
)
}
func (a *Attribute) UnmarshalJSON(data []byte) error {
msg := new(object.Header_Attribute)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*a = *AttributeFromGRPCMessage(msg)
return nil
}

View file

@ -18,3 +18,15 @@ func TestShortHeaderJSON(t *testing.T) {
require.Equal(t, h, h2)
}
func TestAttributeJSON(t *testing.T) {
a := generateAttribute("key", "value")
data, err := a.MarshalJSON()
require.NoError(t, err)
a2 := new(object.Attribute)
require.NoError(t, a2.UnmarshalJSON(data))
require.Equal(t, a, a2)
}

View file

@ -213,6 +213,17 @@ func (a *Attribute) StableSize() (size int) {
return size
}
func (a *Attribute) Unmarshal(data []byte) error {
m := new(object.Header_Attribute)
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
*a = *AttributeFromGRPCMessage(m)
return nil
}
func (h *SplitHeader) StableMarshal(buf []byte) ([]byte, error) {
if h == nil {
return []byte{}, nil

View file

@ -29,16 +29,14 @@ func TestShortHeader_StableMarshal(t *testing.T) {
func TestAttribute_StableMarshal(t *testing.T) {
from := generateAttribute("Key", "Value")
transport := new(grpc.Header_Attribute)
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.Attribute)
require.NoError(t, to.Unmarshal(wire))
to := object.AttributeFromGRPCMessage(transport)
require.Equal(t, from, to)
})
}