From 9301f51db30cbf87a645fb6282fa520fcfcc7320 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Thu, 18 Aug 2016 13:21:46 -0700 Subject: [PATCH] fixed set return value --- buntdb.go | 4 ++-- buntdb_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/buntdb.go b/buntdb.go index ca72209..4c5926d 100644 --- a/buntdb.go +++ b/buntdb.go @@ -1152,8 +1152,8 @@ func (tx *Tx) Set(key, value string, opts *SetOptions) (previousValue string, if _, ok := tx.rollbacks[key]; !ok { tx.rollbacks[key] = prev } - if !item.expired() { - previousValue, replaced = item.val, true + if !prev.expired() { + previousValue, replaced = prev.val, true } } // For commits we simply assign the item to the map. We use this map to diff --git a/buntdb_test.go b/buntdb_test.go index 1343687..1678b2e 100644 --- a/buntdb_test.go +++ b/buntdb_test.go @@ -1717,6 +1717,31 @@ func TestCoverShrinkShrink(t *testing.T) { } } +func TestPreviousItem(t *testing.T) { + db := testOpen(t) + defer testClose(db) + err := db.Update(func(tx *Tx) error { + _, _, err := tx.Set("hello", "world", nil) + if err != nil { + return err + } + prev, replaced, err := tx.Set("hello", "planet", nil) + if err != nil { + return err + } + if !replaced { + t.Fatal("should be replaced") + } + if prev != "world" { + t.Fatalf("expecting '%v', got '%v'", "world", prev) + } + return nil + }) + if err != nil { + t.Fatal(err) + } +} + func TestJSONIndex(t *testing.T) { db := testOpen(t) defer testClose(db)