[#255] v2/object: Implement json.Marshaler/Unmarshaler on SearchFilter

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-10 01:31:50 +03:00 committed by Alex Vanin
parent ebff07aaf2
commit f9939e8c90
2 changed files with 32 additions and 0 deletions

View file

@ -124,3 +124,23 @@ func (o *Object) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func (f *SearchFilter) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
SearchFilterToGRPCMessage(f),
)
}
func (f *SearchFilter) UnmarshalJSON(data []byte) error {
msg := new(object.SearchRequest_Body_Filter)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*f = *SearchFilterFromGRPCMessage(msg)
return nil
}

View file

@ -78,3 +78,15 @@ func TestObjectJSON(t *testing.T) {
require.Equal(t, o, o2) require.Equal(t, o, o2)
} }
func TestSearchFilterJSON(t *testing.T) {
f := generateFilter("key", "value")
data, err := f.MarshalJSON()
require.NoError(t, err)
f2 := new(object.SearchFilter)
require.NoError(t, f2.UnmarshalJSON(data))
require.Equal(t, f, f2)
}