forked from TrueCloudLab/frostfs-api-go
[#168] refs: Implement binary/JSON encoders/decoders on ContainerID
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
7289ff6c64
commit
c0f3ac51d4
6 changed files with 103 additions and 4 deletions
|
@ -44,3 +44,23 @@ func (o *ObjectID) UnmarshalJSON(data []byte) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ContainerID) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
ContainerIDToGRPCMessage(c),
|
||||
)
|
||||
}
|
||||
|
||||
func (c *ContainerID) UnmarshalJSON(data []byte) error {
|
||||
msg := new(refs.ContainerID)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*c = *ContainerIDFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -31,3 +31,16 @@ func TestObjectIDJSON(t *testing.T) {
|
|||
|
||||
require.Equal(t, o, o2)
|
||||
}
|
||||
|
||||
func TestContainerIDJSON(t *testing.T) {
|
||||
cid := new(refs.ContainerID)
|
||||
cid.SetValue([]byte{1})
|
||||
|
||||
data, err := cid.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
cid2 := new(refs.ContainerID)
|
||||
require.NoError(t, cid2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, cid, cid2)
|
||||
}
|
||||
|
|
|
@ -76,6 +76,17 @@ func (c *ContainerID) StableSize() int {
|
|||
return proto.BytesSize(containerIDValField, c.val)
|
||||
}
|
||||
|
||||
func (c *ContainerID) Unmarshal(data []byte) error {
|
||||
m := new(refs.ContainerID)
|
||||
if err := goproto.Unmarshal(data, m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*c = *ContainerIDFromGRPCMessage(m)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *ObjectID) StableMarshal(buf []byte) ([]byte, error) {
|
||||
if o == nil {
|
||||
return []byte{}, nil
|
||||
|
|
|
@ -29,7 +29,6 @@ func TestOwnerID_StableMarshal(t *testing.T) {
|
|||
|
||||
func TestContainerID_StableMarshal(t *testing.T) {
|
||||
cnrFrom := new(refs.ContainerID)
|
||||
cnrTransport := new(grpc.ContainerID)
|
||||
|
||||
t.Run("non empty", func(t *testing.T) {
|
||||
cnrFrom.SetValue([]byte("Container ID"))
|
||||
|
@ -37,10 +36,9 @@ func TestContainerID_StableMarshal(t *testing.T) {
|
|||
wire, err := cnrFrom.StableMarshal(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = goproto.Unmarshal(wire, cnrTransport)
|
||||
require.NoError(t, err)
|
||||
cnrTo := new(refs.ContainerID)
|
||||
require.NoError(t, cnrTo.Unmarshal(wire))
|
||||
|
||||
cnrTo := refs.ContainerIDFromGRPCMessage(cnrTransport)
|
||||
require.Equal(t, cnrFrom, cnrTo)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue