You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When implementing a custom Storer on a type, I have found that during an Upsert, if the value already exists, the reflection logic ends up passing an **Item instead of *Item to the index function. This inconsistent type seems to be handled fine in the anonStorer reflection logic, but breaks a custom index function that expects to always get the same type. I'm currently working around this by type checking the value in each index function for **Item and dereferencing it once as needed to cope with the delete.
Here is a simple reproduction of the panic. Am I doing something wrong here?
typeItemstruct{ Namestring }
func (i*Item) Type() string { return"Item" }
func (i*Item) Indexes() map[string]bh.Index {
returnmap[string]bh.Index{
"Name": func(_string, valueinterface{}) ([]byte, error) {
// If the upsert wants to delete an existing value first,// value could be a **Item instead of *Item// panic: interface conversion: interface {} is **Item, not *Itemv:=value.(*Item).Namereturn []byte(v), nil
},
}
}
func (i*Item) SliceIndexes() map[string]bh.SliceIndex {
returnmap[string]bh.SliceIndex{}
}
funcTestStorerUpsert(t*testing.T) {
path:="/tmp/bolthold_upsert_test.db"os.Remove(path)
db, err:=bh.Open(path, 0666, nil)
iferr!=nil {
t.Fatal(err)
}
deferdb.Close()
item:=&Item{"Name"}
fori:=0; i<2; i++ {
err=db.Upsert("key", item)
iferr!=nil {
t.Fatal(err)
}
}
}
When implementing a custom
Storer
on a type, I have found that during an Upsert, if the value already exists, the reflection logic ends up passing an**Item
instead of*Item
to the index function. This inconsistent type seems to be handled fine in theanonStorer
reflection logic, but breaks a custom index function that expects to always get the same type. I'm currently working around this by type checking thevalue
in each index function for**Item
and dereferencing it once as needed to cope with the delete.Here is a simple reproduction of the panic. Am I doing something wrong here?
Originally mentioned in #64
The text was updated successfully, but these errors were encountered: