From 6bd1316eabe922cddbc1022249eccf8b7bee2b8a Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Sat, 6 Jul 2024 04:42:16 -0700 Subject: [PATCH] Fix (pogreb): 'With' method returns --- go.mod | 7 ++++--- pogreb/pogreb.go | 17 ++++++++++++++++- pogreb/pogreb_test.go | 6 +++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 6bae284..543cc5e 100644 --- a/go.mod +++ b/go.mod @@ -23,9 +23,10 @@ require ( ) retract ( - v0.4.7 // riddled - v0.4.6 // with - v0.4.5 // pre v1 + v0.5.0 // riddled + v0.4.7 // with + v0.4.6 // pre + v0.4.5 // v1 v0.4.4 // bugs // v0.4.3 // deadlock diff --git a/pogreb/pogreb.go b/pogreb/pogreb.go index bd9f2fe..c2fc674 100644 --- a/pogreb/pogreb.go +++ b/pogreb/pogreb.go @@ -53,8 +53,16 @@ type Store struct { metrics *pogreb.Metrics } +var nilBackend = &pogreb.DB{} + // Backend returns the underlying pogreb instance. func (pstore *Store) Backend() any { + if pstore == nil { + return nilBackend + } + if pstore.DB == nil { + return nilBackend + } return pstore.DB } @@ -300,6 +308,10 @@ func (db *DB) With(storeName string) database.Filer { } db.mu.RLock() d, ok := db.store[storeName] + if !ok { + db.mu.RUnlock() + return nil + } if ok { if d.closed == nil || d.DB == nil || d.closed.Load() { db.mu.RUnlock() @@ -317,6 +329,9 @@ func (db *DB) With(storeName string) database.Filer { return d } db.mu.RUnlock() + if d != nil && d.DB == nil { + d = nil + } return d } @@ -398,7 +413,7 @@ func (db *DB) withAll(action withAllAction) error { var errs = make([]error, len(db.store)) for name, store := range db.store { var err error - if store == nil || store.Backend().(*pogreb.DB) == nil { + if store == nil || store.Backend() == nilBackend || store.Backend().(*pogreb.DB) == nil { errs = append(errs, namedErr(name, ErrBogusStore)) continue } diff --git a/pogreb/pogreb_test.go b/pogreb/pogreb_test.go index 176e470..cae87dd 100644 --- a/pogreb/pogreb_test.go +++ b/pogreb/pogreb_test.go @@ -130,15 +130,15 @@ func TestDB_Init(t *testing.T) { //nolint:funlen,gocognit,cyclop } }) t.Run("withNewStoreDoesntExist", func(t *testing.T) { - if nope := db.WithNew("asdfqwerty"); nope.Backend() == nil { + if nope := db.WithNew("asdfqwerty"); nope.Backend() == nil || nope.Backend() == nilBackend { t.Fatalf("[FAIL] got nil result for nonexistent store when it should have made itself: %T, %v", nope, nope) } else { - t.Logf("[SUCCESS] got nil Value for store that doesn't exist") + t.Logf("[SUCCESS] got new store with valid backend when calling WithNew for store that doesn't exist") } }) t.Run("withStoreDoesntExist", func(t *testing.T) { nope := db.With(c.RandStr(10)) - if nope != nil { + if nope != nil && nope.Backend() != nilBackend { t.Fatalf("[FAIL] got non nil result for nonexistent store: %T, %v", nope.Backend(), nope.Backend()) } else { t.Logf("[SUCCESS] got nil Value for store that doesn't exist")