From 44437bf81af3b2675ec1be6f4df6f068fa854e79 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 16:53:21 +0300 Subject: [PATCH] [#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 --- pkg/netmap/filter.go | 11 +++++++++++ pkg/netmap/filter_test.go | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/pkg/netmap/filter.go b/pkg/netmap/filter.go index de6c9399..4beb1f55 100644 --- a/pkg/netmap/filter.go +++ b/pkg/netmap/filter.go @@ -148,6 +148,13 @@ func (c *Context) matchKeyValue(f *Filter, b *Node) bool { } // NewFilter creates and returns new Filter instance. +// +// Defaults: +// - name: ""; +// - key: ""; +// - value: ""; +// - operation: 0; +// - filters: nil. func NewFilter() *Filter { return NewFilterFromV2(new(netmap.Filter)) } @@ -217,6 +224,10 @@ func (f *Filter) SetOperation(op Operation) { } func filtersFromV2(fs []*netmap.Filter) []*Filter { + if fs == nil { + return nil + } + res := make([]*Filter, 0, len(fs)) for i := range fs { diff --git a/pkg/netmap/filter_test.go b/pkg/netmap/filter_test.go index a9eadaa8..c9103e12 100644 --- a/pkg/netmap/filter_test.go +++ b/pkg/netmap/filter_test.go @@ -307,3 +307,25 @@ func TestFilterEncoding(t *testing.T) { 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()) + }) +}