From 3f4db5e82cff4a62fc278ea16398cae289bc8bd1 Mon Sep 17 00:00:00 2001 From: brent s Date: Sat, 18 Dec 2021 00:57:41 -0500 Subject: [PATCH] fix - suppress nil/empty items from being added to slices --- collection_funcs.go | 16 ++++++++++------ collection_funcs_test.go | 1 + service_funcs.go | 24 +++++++++++++++--------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/collection_funcs.go b/collection_funcs.go index b9a740d..6dc854b 100644 --- a/collection_funcs.go +++ b/collection_funcs.go @@ -146,15 +146,16 @@ func (c *Collection) Items() (items []*Item, err error) { paths = variant.Value().([]dbus.ObjectPath) - items = make([]*Item, len(paths)) + items = make([]*Item, 0) - for idx, path := range paths { + for _, path := range paths { + item = nil if item, err = NewItem(c, path); err != nil { errs = append(errs, err) err = nil continue } - items[idx] = item + items = append(items, item) } err = NewErrors(err) @@ -245,6 +246,7 @@ func (c *Collection) SearchItems(profile string) (items []*Item, err error) { var paths []dbus.ObjectPath var errs []error = make([]error, 0) var attrs map[string]string = make(map[string]string, 0) + var item *Item attrs["profile"] = profile @@ -254,14 +256,16 @@ func (c *Collection) SearchItems(profile string) (items []*Item, err error) { return } - items = make([]*Item, len(paths)) + items = make([]*Item, 0) - for idx, path := range paths { - if items[idx], err = NewItem(c, path); err != nil { + for _, path := range paths { + item = nil + if item, err = NewItem(c, path); err != nil { errs = append(errs, err) err = nil continue } + items = append(items, item) } err = NewErrors(err) diff --git a/collection_funcs_test.go b/collection_funcs_test.go index 710122e..7014b4b 100644 --- a/collection_funcs_test.go +++ b/collection_funcs_test.go @@ -148,6 +148,7 @@ func TestCollection_Label(t *testing.T) { t.Fatalf("NewService failed: %v", err.Error()) } + t.Logf("Attempting to get label of collection: %v", defaultCollectionLabel) if collection, err = svc.GetCollection(defaultCollectionLabel); err != nil { t.Errorf( "failed when fetching collection '%v': %v", diff --git a/service_funcs.go b/service_funcs.go index 85c986c..c633f18 100644 --- a/service_funcs.go +++ b/service_funcs.go @@ -57,16 +57,17 @@ func (s *Service) Collections() (collections []*Collection, err error) { paths = variant.Value().([]dbus.ObjectPath) - collections = make([]*Collection, len(paths)) + collections = make([]*Collection, 0) - for idx, path := range paths { + for _, path := range paths { + coll = nil if coll, err = NewCollection(s, path); err != nil { // return errs = append(errs, err) err = nil continue } - collections[idx] = coll + collections = append(collections, coll) } err = NewErrors(err) @@ -364,6 +365,7 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It var ok bool var c *Collection var cPath dbus.ObjectPath + var item *Item var errs []error = make([]error, 0) if attributes == nil || len(attributes) == 0 { @@ -375,8 +377,8 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It DbusServiceSearchItems, 0, attributes, ).Store(&unlocked, &locked) - lockedItems = make([]*Item, len(locked)) - unlockedItems = make([]*Item, len(unlocked)) + lockedItems = make([]*Item, 0) + unlockedItems = make([]*Item, 0) if collectionObjs, err = s.Collections(); err != nil { return @@ -389,8 +391,9 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It } // Locked items - for idx, i := range locked { + for _, i := range locked { + item = nil cPath = dbus.ObjectPath(filepath.Dir(string(i))) if c, ok = collections[cPath]; !ok { @@ -400,18 +403,20 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It continue } - if lockedItems[idx], err = NewItem(c, i); err != nil { + if item, err = NewItem(c, i); err != nil { errs = append(errs, errors.New(fmt.Sprintf( "could not create Item for locked item %v", string(i), ))) err = nil continue } + lockedItems = append(lockedItems, item) } // Unlocked items - for idx, i := range unlocked { + for _, i := range unlocked { + item = nil cPath = dbus.ObjectPath(filepath.Dir(string(i))) if c, ok = collections[cPath]; !ok { @@ -421,13 +426,14 @@ func (s *Service) SearchItems(attributes map[string]string) (unlockedItems []*It continue } - if unlockedItems[idx], err = NewItem(c, i); err != nil { + if item, err = NewItem(c, i); err != nil { errs = append(errs, errors.New(fmt.Sprintf( "could not create Item for unlocked item %v", string(i), ))) err = nil continue } + unlockedItems = append(unlockedItems, item) } if errs != nil && len(errs) > 0 {