From f9939e8c902f872884fc10d93973710cf5d6349a Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 10 Feb 2021 01:31:50 +0300 Subject: [PATCH] [#255] v2/object: Implement json.Marshaler/Unmarshaler on SearchFilter Signed-off-by: Leonard Lyubich --- v2/object/json.go | 20 ++++++++++++++++++++ v2/object/json_test.go | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/v2/object/json.go b/v2/object/json.go index 940aa7c3..e2caede9 100644 --- a/v2/object/json.go +++ b/v2/object/json.go @@ -124,3 +124,23 @@ func (o *Object) UnmarshalJSON(data []byte) error { 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 +} diff --git a/v2/object/json_test.go b/v2/object/json_test.go index ceefd96f..e8ad5d25 100644 --- a/v2/object/json_test.go +++ b/v2/object/json_test.go @@ -78,3 +78,15 @@ func TestObjectJSON(t *testing.T) { 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) +}