timshannon / badgerhold

BadgerHold is an embeddable NoSQL store for querying Go types built on Badger
MIT License
515 stars 52 forks source link

No ID Returned in Get #30

Closed wolveix closed 4 years ago

wolveix commented 4 years ago

I'm not sure if I'm missing something, but it seems like .Get doesn't return the ID within the struct that gets returned. I tested the same with BoltHold, which produced exactly what I was expecting, so I suspect that this is a bug with BadgerHold. If you run the linked code example, you'll get the following response:

Testing badger... {johnsmith@domain.tld 0} {johnsmith2@domain.tld 1} [{johnsmith2@domain.tld 1}] {johnsmith2@domain.tld 0} < Should be 1 Testing bolt... {johnsmith@domain.tld 1} {johnsmith2@domain.tld 2} [{johnsmith2@domain.tld 2}] {johnsmith@domain.tld 2}

Badger example:

type User struct {
    Email    string `badgerhold:"index"`
    ID       uint64 `badgerhold:"key"`
}
user := User{
    Email:    "johnsmith@domain.tld",
}

if err := s.Insert(badgerhold.NextSequence(), &user); err != nil {
    log.Fatal(err)
}

fmt.Println(user)
// OUTPUT: &{johnsmith@domain.tld 0} CORRECT
user2 := User{
    Email:    "johnsmith2@domain.tld",
}

if err := s.Insert(badgerhold.NextSequence(), &user2); err != nil {
    log.Fatal(err)
}

fmt.Println(user2)
// OUTPUT: &{johnsmith2@domain.tld 1} CORRECT
var userResults []User

if err := s.Find(&userResults, badgerhold.Where("Email").Eq("johnsmith2@domain.tld").Index("Email")); err != nil {
    log.Fatal(err)
}

fmt.Println(userResults)
// OUTPUT: [{johnsmith2@domain.tld 1}] CORRECT
var userReturned User

if err := s.Get(uint64(1), &userReturned); err != nil {
    log.Fatal(err)
}

fmt.Println(userReturned)
// OUTPUT: &{johnsmith@domain.tld 0} INCORRECT
// EXPECTED: &{johnsmith@domain.tld 1}
timshannon commented 4 years ago

Should be fixed in master now.

wolveix commented 4 years ago

Nice one, thanks! :D