
Method-chaining in gorm is predicated on `search`'s `clone` method returning a copy of the `search` instance such that no change that is made to the copy affects the original. However, the original implementation copied the slices of the various slice properties (such as `whereConditions`), with the incorrect understanding that `append` would create a new slice each time that it was called. In some cases, this is true. Practically, go doubles the size of the slice once it gets full, so the following slice `append` calls would result in a new slice: * 0 -> 1 * 1 -> 2 * 2 -> 4 * 4 -> 8 * and so on. So, when the number of "where" conditions (or any other slice property) was 0, 1, 2, or 4, method-chaining would work as expected. However, when it was 3, 5, 6, or 7, modifying the copy would modify the original. The solution is simply to create new slices in `clone`.
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package gorm
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestCloneSearch(t *testing.T) {
|
|
s := new(search)
|
|
s.Where("name = ?", "jinzhu").Order("name").Attrs("name", "jinzhu").Select("name, age")
|
|
|
|
s1 := s.clone()
|
|
s1.Where("age = ?", 20).Order("age").Attrs("email", "a@e.org").Select("email")
|
|
|
|
if reflect.DeepEqual(s.whereConditions, s1.whereConditions) {
|
|
t.Errorf("Where should be copied")
|
|
}
|
|
|
|
if reflect.DeepEqual(s.orders, s1.orders) {
|
|
t.Errorf("Order should be copied")
|
|
}
|
|
|
|
if reflect.DeepEqual(s.initAttrs, s1.initAttrs) {
|
|
t.Errorf("InitAttrs should be copied")
|
|
}
|
|
|
|
if reflect.DeepEqual(s.Select, s1.Select) {
|
|
t.Errorf("selectStr should be copied")
|
|
}
|
|
}
|
|
|
|
func TestWhereCloneCorruption(t *testing.T) {
|
|
for whereCount := 1; whereCount <= 8; whereCount++ {
|
|
t.Run(fmt.Sprintf("w=%d", whereCount), func(t *testing.T) {
|
|
s := new(search)
|
|
for w := 0; w < whereCount; w++ {
|
|
s = s.clone().Where(fmt.Sprintf("w%d = ?", w), fmt.Sprintf("value%d", w))
|
|
}
|
|
if len(s.whereConditions) != whereCount {
|
|
t.Errorf("s: where count should be %d", whereCount)
|
|
}
|
|
|
|
q1 := s.clone().Where("finalThing = ?", "THING1")
|
|
q2 := s.clone().Where("finalThing = ?", "THING2")
|
|
|
|
if reflect.DeepEqual(q1.whereConditions, q2.whereConditions) {
|
|
t.Errorf("Where conditions should be different")
|
|
}
|
|
})
|
|
}
|
|
}
|