[#302] pkg/netmap: Document default values set in NewFilter

Make `filtersFromV2` return `nil` if passed
value is `nil`. Document field values of
instance constructed via `NewFilter`. Assert
the values in corresponding unit test.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 16:53:21 +03:00 committed by Alex Vanin
parent 9a7e120485
commit 44437bf81a
2 changed files with 33 additions and 0 deletions

View file

@ -148,6 +148,13 @@ func (c *Context) matchKeyValue(f *Filter, b *Node) bool {
} }
// NewFilter creates and returns new Filter instance. // NewFilter creates and returns new Filter instance.
//
// Defaults:
// - name: "";
// - key: "";
// - value: "";
// - operation: 0;
// - filters: nil.
func NewFilter() *Filter { func NewFilter() *Filter {
return NewFilterFromV2(new(netmap.Filter)) return NewFilterFromV2(new(netmap.Filter))
} }
@ -217,6 +224,10 @@ func (f *Filter) SetOperation(op Operation) {
} }
func filtersFromV2(fs []*netmap.Filter) []*Filter { func filtersFromV2(fs []*netmap.Filter) []*Filter {
if fs == nil {
return nil
}
res := make([]*Filter, 0, len(fs)) res := make([]*Filter, 0, len(fs))
for i := range fs { for i := range fs {

View file

@ -307,3 +307,25 @@ func TestFilterEncoding(t *testing.T) {
require.Equal(t, f, f2) require.Equal(t, f, f2)
}) })
} }
func TestNewFilter(t *testing.T) {
t.Run("default values", func(t *testing.T) {
filter := NewFilter()
// check initial values
require.Empty(t, filter.Name())
require.Empty(t, filter.Key())
require.Empty(t, filter.Value())
require.Zero(t, filter.Operation())
require.Nil(t, filter.InnerFilters())
// convert to v2 message
filterV2 := filter.ToV2()
require.Empty(t, filterV2.GetName())
require.Empty(t, filterV2.GetKey())
require.Empty(t, filterV2.GetValue())
require.Equal(t, netmap.UnspecifiedOperation, filterV2.GetOp())
require.Nil(t, filterV2.GetFilters())
})
}