add setting key value in struct for ForEach queries

This commit is contained in:
Josh
2020-08-15 15:27:55 -05:00
parent 010bd25d1a
commit 8dfa0b60db
2 changed files with 80 additions and 0 deletions
+58
View File
@@ -87,3 +87,61 @@ func TestForEachInBucket(t *testing.T) {
}
})
}
func TestForEachKeyStructTag(t *testing.T) {
testWrap(t, func(store *bolthold.Store, t *testing.T) {
type KeyTest struct {
Key int `boltholdKey:"Key"`
Value string
}
key := 3
err := store.Insert(key, &KeyTest{
Value: "test value",
})
if err != nil {
t.Fatalf("Error inserting KeyTest struct for Key struct tag testing. Error: %s", err)
}
err = store.ForEach(bolthold.Where(bolthold.Key).Eq(key), func(result *KeyTest) error {
if result.Key != key {
t.Fatalf("Key struct tag was not set correctly. Expected %d, got %d", key, result.Key)
}
return nil
})
if err != nil {
t.Fatalf("Error running ForEach in TestKeyStructTag. ERROR: %s", err)
}
})
}
func TestForEachKeyStructTagIntoPtr(t *testing.T) {
testWrap(t, func(store *bolthold.Store, t *testing.T) {
type KeyTest struct {
Key *int `boltholdKey:"Key"`
Value string
}
key := 3
err := store.Insert(&key, &KeyTest{
Value: "test value",
})
if err != nil {
t.Fatalf("Error inserting KeyTest struct for Key struct tag testing. Error: %s", err)
}
err = store.ForEach(bolthold.Where(bolthold.Key).Eq(key), func(result *KeyTest) error {
if result.Key == nil || *result.Key != key {
t.Fatalf("Key struct tag was not set correctly. Expected %d, got %d", key, result.Key)
}
return nil
})
if err != nil {
t.Fatalf("Error running ForEach in TestKeyStructTag. ERROR: %s", err)
}
})
}
+22
View File
@@ -562,7 +562,29 @@ func (s *Store) forEach(source BucketSource, query *Query, fn interface{}) error
dataType := reflect.New(argType).Interface()
var keyType reflect.Type
var keyField string
for i := 0; i < argType.NumField(); i++ {
if strings.Contains(string(argType.Field(i).Tag), BoltholdKeyTag) {
keyType = argType.Field(i).Type
keyField = argType.Field(i).Name
break
}
}
return s.runQuery(source, dataType, query, nil, query.skip, func(r *record) error {
if keyType != nil {
rowKey := r.value
for rowKey.Kind() == reflect.Ptr {
rowKey = rowKey.Elem()
}
err := s.decode(r.key, rowKey.FieldByName(keyField).Addr().Interface())
if err != nil {
return err
}
}
out := fnVal.Call([]reflect.Value{r.value})
if len(out) != 1 {
return fmt.Errorf("foreach function does not return an error")