[#289] oid: Add JSON encoding of Address type

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-07-04 12:24:38 +03:00 committed by LeL
parent 2ad89085a3
commit 30d27c3050
2 changed files with 40 additions and 0 deletions

View file

@ -65,6 +65,32 @@ func (x Address) WriteToV2(m *refs.Address) {
m.SetContainerID(&cnr)
}
// MarshalJSON encodes Address into a JSON format of the NeoFS API protocol
// (Protocol Buffers JSON).
//
// See also UnmarshalJSON.
func (x Address) MarshalJSON() ([]byte, error) {
var m refs.Address
x.WriteToV2(&m)
return m.MarshalJSON()
}
// UnmarshalJSON decodes NeoFS API protocol JSON format into the Address
// (Protocol Buffers JSON). Returns an error describing a format violation.
//
// See also MarshalJSON.
func (x *Address) UnmarshalJSON(data []byte) error {
var m refs.Address
err := m.UnmarshalJSON(data)
if err != nil {
return err
}
return x.ReadFromV2(m)
}
// Container returns unique identifier of the NeoFS object container.
//
// Zero Address has zero container ID, which is incorrect according to NeoFS

View file

@ -93,3 +93,17 @@ func TestAddress_DecodeString(t *testing.T) {
require.Error(t, x2.DecodeString(strCnr+"\\"+strObj))
require.NoError(t, x2.DecodeString(strCnr+"/"+strObj))
}
func TestAddressEncoding(t *testing.T) {
v := oidtest.Address()
t.Run("json", func(t *testing.T) {
data, err := v.MarshalJSON()
require.NoError(t, err)
var v2 oid.Address
require.NoError(t, v2.UnmarshalJSON(data))
require.Equal(t, v, v2)
})
}