Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Fix ignored fields in ScanStruct
Browse files Browse the repository at this point in the history
ScanStruct should ignore fields with tag `redis:"-"`. Fix the bug and
add test for the same.

Fix similar bug in redisx package.
  • Loading branch information
garyburd committed Feb 6, 2013
1 parent 48f0174 commit 9c59bb9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
5 changes: 4 additions & 1 deletion redis/scan.go
Expand Up @@ -242,7 +242,10 @@ func compileStructSpec(t reflect.Type, depth map[string]int, index []int, ss *st
fs := &fieldSpec{name: f.Name}
tag := f.Tag.Get("redis")
p := strings.Split(tag, ",")
if len(p) > 0 && p[0] != "-" {
if len(p) > 0 {
if p[0] == "-" {
continue
}
if len(p[0]) > 0 {
fs.name = p[0]
}
Expand Down
32 changes: 20 additions & 12 deletions redis/scan_test.go
Expand Up @@ -134,24 +134,32 @@ func ExampleScan() {
// Red 5
}

type s0 struct {
X int
Y int `redis:"y"`
Bt bool
}

type s1 struct {
X int `redis:"-"`
I int `redis:"i"`
U uint `redis:"u"`
S string `redis:"s"`
P []byte `redis:"p"`
B bool `redis:"b"`
Bt bool
Bf bool
s0
}

var scanStructTests = []struct {
title string
reply []string
value interface{}
}{
{"basic",
[]string{"i", "-1234", "u", "5678", "s", "hello", "p", "world", "b", "f", "Bt", "1", "Bf", "0"},
&struct {
I int `redis:"i"`
U uint `redis:"u"`
S string `redis:"s"`
P []byte `redis:"p"`
B bool `redis:"b"`
Bt bool
Bf bool
}{
-1234, 5678, "hello", []byte("world"), false, true, false,
},
[]string{"i", "-1234", "u", "5678", "s", "hello", "p", "world", "b", "t", "Bt", "1", "Bf", "0", "X", "123", "y", "456"},
&s1{I: -1234, U: 5678, S: "hello", P: []byte("world"), B: true, Bt: true, Bf: false, s0: s0{X: 123, Y: 456}},
},
}

Expand Down
5 changes: 4 additions & 1 deletion redisx/util.go
Expand Up @@ -52,7 +52,10 @@ func compileStructSpec(t reflect.Type, depth map[string]int, index []int, ss *st
fs := &fieldSpec{name: f.Name}
tag := f.Tag.Get("redis")
p := strings.Split(tag, ",")
if len(p) > 0 && p[0] != "-" {
if len(p) > 0 {
if p[0] == "-" {
continue
}
if len(p[0]) > 0 {
fs.name = p[0]
}
Expand Down

0 comments on commit 9c59bb9

Please sign in to comment.