bug: cached columns are returned based on the first use
max-stytch opened this issue · comments
scan.Columns
does not take into account the excluded columns when checking values from the cache.
This means that if it is called on the same struct with different exclusion conditions, the first set of columns is returned for all additional calls.
type thingy struct {
StrCol string `db:"str_col"`
IntCol int `db:"int_col"`
}
func TestExcludedBehavior(t *testing.T) {
thing := &thingy{}
cols1, err := scan.Columns(thing)
assert.NoError(t, err)
assert.Equal(t, []string{"str_col", "int_col"}, cols1)
cols2, err := scan.Columns(thing, "int_col")
assert.NoError(t, err)
assert.Equal(t, []string{"str_col"}, cols2)
}
=== RUN TestExcludedBehavior
sql_test.go:21:
Error Trace: sql_test.go:21
Error: Not equal:
expected: []string{"str_col"}
actual : []string{"str_col", "int_col"}
Diff:
--- Expected
+++ Actual
@@ -1,3 +1,4 @@
-([]string) (len=1) {
- (string) (len=7) "str_col"
+([]string) (len=2) {
+ (string) (len=7) "str_col",
+ (string) (len=7) "int_col"
}
Test: TestExcludedBehavior
--- FAIL: TestExcludedBehavior (0.00s)
Expected :[]string{"str_col"}
Actual :[]string{"str_col", "int_col"}
<Click to see difference>
FAIL
Process finished with the exit code 1
Suggest changing the cache key to be a struct { value reflect.Value, cols string }
where cols is strings.Join(expected, " ")
.
I can open up a PR later this week.
Thanks for the bug report. I added two new tests and fixed the bug.