mirror of
https://github.com/timshannon/bolthold.git
synced 2026-04-23 00:27:06 +08:00
add setting key value in struct for ForEach queries
This commit is contained in:
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user