Skip to content

Commit f39c372

Browse files
committed
Allow recursion using ...
1 parent 5f3e10d commit f39c372

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

sqlstruct.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@ with "encoding/json" package.
1313
1414
For example:
1515
16+
type T1 struct {
17+
F4 string `sql:"field4"`
18+
}
19+
20+
type T2 struct {
21+
F5 string `sql:"field5"`
22+
}
23+
1624
type T struct {
17-
F1 string
18-
F2 string `sql:"field2"`
19-
F3 string `sql:"-"`
25+
F1 string
26+
F2 string `sql:"field2"`
27+
F3 string `sql:"-"`
28+
fieldT1 T1 `sql:"..."`
29+
T2
2030
}
2131
2232
rows, err := db.Query(fmt.Sprintf("SELECT %s FROM tablename", sqlstruct.Columns(T{})))
@@ -146,7 +156,7 @@ func getFieldInfo(typ reflect.Type) fieldInfo {
146156
}
147157

148158
// Handle embedded structs
149-
if f.Anonymous && f.Type.Kind() == reflect.Struct {
159+
if (f.Anonymous || tag == "...") && f.Type.Kind() == reflect.Struct {
150160
for k, v := range getFieldInfo(f.Type) {
151161
finfo[k] = append([]int{i}, v...)
152162
}

sqlstruct_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ type testType2 struct {
2525
FieldSec string `sql:"field_sec"`
2626
}
2727

28+
type testType3 struct {
29+
FieldA string `sql:"field_a"`
30+
EmbeddedType EmbeddedType `sql:"..."`
31+
}
32+
2833
// testRows is a mock version of sql.Rows which can only scan strings
2934
type testRows struct {
3035
columns []string
@@ -67,6 +72,16 @@ func TestColumns(t *testing.T) {
6772
}
6873
}
6974

75+
func TestColumnDeep(t *testing.T) {
76+
var v testType3
77+
e := "field_a, field_e"
78+
c := Columns(v)
79+
80+
if c != e {
81+
t.Errorf("expected %q got %q", e, c)
82+
}
83+
}
84+
7085
func TestColumnsAliased(t *testing.T) {
7186
var t1 testType
7287
var t2 testType2

0 commit comments

Comments
 (0)