File tree Expand file tree Collapse file tree 2 files changed +29
-4
lines changed
Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -13,10 +13,20 @@ with "encoding/json" package.
1313
1414For 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 }
Original file line number Diff line number Diff 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
2934type 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+
7085func TestColumnsAliased (t * testing.T ) {
7186 var t1 testType
7287 var t2 testType2
You can’t perform that action at this time.
0 commit comments