forked from TrueCloudLab/frostfs-api-go
[#255] pkg/object: Implement json.Marshaler/Unmarshaler on SearchFilters
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
f9939e8c90
commit
e782531f25
2 changed files with 35 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
package object
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg"
|
||||
|
@ -255,3 +256,21 @@ func (f *SearchFilters) AddSplitIDFilter(m SearchMatchType, id *SplitID) {
|
|||
func (f *SearchFilters) AddTypeFilter(m SearchMatchType, typ Type) {
|
||||
f.addReservedFilter(m, fKeyType, typ)
|
||||
}
|
||||
|
||||
// MarshalJSON encodes SearchFilters to protobuf JSON format.
|
||||
func (f *SearchFilters) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(f.ToV2())
|
||||
}
|
||||
|
||||
// UnmarshalJSON decodes SearchFilters from protobuf JSON format.
|
||||
func (f *SearchFilters) UnmarshalJSON(data []byte) error {
|
||||
var fsV2 []*v2object.SearchFilter
|
||||
|
||||
if err := json.Unmarshal(data, &fsV2); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*f = NewSearchFiltersFromV2(fsV2)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -176,3 +176,19 @@ func TestSearchFilters_AddTypeFilter(t *testing.T) {
|
|||
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
|
||||
})
|
||||
}
|
||||
|
||||
func TestSearchFiltersEncoding(t *testing.T) {
|
||||
fs := object.NewSearchFilters()
|
||||
fs.AddFilter("key 1", "value 2", object.MatchStringEqual)
|
||||
fs.AddFilter("key 2", "value 2", object.MatchStringNotEqual)
|
||||
|
||||
t.Run("json", func(t *testing.T) {
|
||||
data, err := fs.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
fs2 := object.NewSearchFilters()
|
||||
require.NoError(t, fs2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, fs, fs2)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue