Skip to content

Commit de9a794

Browse files
authored
feat(AddFlat): Add support for Anonymous field pointers (gomodule#490)
Add support to AddFlat for Anonymous fields which are pointers. Fixes gomodule#487
1 parent 1b2b891 commit de9a794

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

redis/scan.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,14 @@ func compileStructSpec(t reflect.Type, depth map[string]int, index []int, ss *st
358358
case f.PkgPath != "" && !f.Anonymous:
359359
// Ignore unexported fields.
360360
case f.Anonymous:
361-
// TODO: Handle pointers. Requires change to decoder and
362-
// protection against infinite recursion.
363-
if f.Type.Kind() == reflect.Struct {
361+
switch f.Type.Kind() {
362+
case reflect.Struct:
364363
compileStructSpec(f.Type, depth, append(index, i), ss)
364+
case reflect.Ptr:
365+
// TODO(steve): Protect against infinite recursion.
366+
if f.Type.Elem().Kind() == reflect.Struct {
367+
compileStructSpec(f.Type.Elem(), depth, append(index, i), ss)
368+
}
365369
}
366370
default:
367371
fs := &fieldSpec{name: f.Name}

redis/scan_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,14 @@ func ExampleScanSlice() {
421421

422422
var now = time.Now()
423423

424+
type Ed struct {
425+
EdI int `redis:"edi"`
426+
}
427+
428+
type Edp struct {
429+
EdpI int `redis:"edpi"`
430+
}
431+
424432
var argsTests = []struct {
425433
title string
426434
actual redis.Args
@@ -470,6 +478,16 @@ var argsTests = []struct {
470478
}),
471479
redis.Args{},
472480
},
481+
{"struct-anonymous",
482+
redis.Args{}.AddFlat(struct {
483+
Ed
484+
*Edp
485+
}{
486+
Ed{EdI: 2},
487+
&Edp{EdpI: 3},
488+
}),
489+
redis.Args{"edi", 2, "edpi", 3},
490+
},
473491
}
474492

475493
func TestArgs(t *testing.T) {

0 commit comments

Comments
 (0)