@@ -17,6 +17,11 @@ import (
1717
1818type TableOptions func (* TableDefinition )
1919
20+ type structFieldWithPath struct {
21+ * reflect.StructField
22+ path string
23+ }
24+
2025//go:embed templates/*.go.tpl
2126var TemplatesFS embed.FS
2227
@@ -122,28 +127,34 @@ func shouldUnwrapField(t *TableDefinition, field reflect.StructField) bool {
122127 return isFieldStruct (field .Type ) && (t .unwrapAllEmbeddedStructFields && field .Anonymous || sliceContains (t .structFieldsToUnwrap , field .Name ))
123128}
124129
125- func getEmbeddedFields (t * TableDefinition , reflectType reflect.Type ) []reflect.StructField {
130+ func getEmbeddedFields (t * TableDefinition , field reflect.StructField ) []structFieldWithPath {
131+ reflectType := field .Type
126132 if reflectType .Kind () == reflect .Ptr {
127133 reflectType = reflectType .Elem ()
128134 }
129135
130- fields := make ([]reflect. StructField , 0 )
136+ fields := make ([]structFieldWithPath , 0 )
131137 for i := 0 ; i < reflectType .NumField (); i ++ {
132138 sf := reflectType .Field (i )
133- if ignoreField (t , sf ) {
139+ if ignoreField (t , & sf ) {
134140 continue
135141 }
136- fields = append (fields , sf )
142+ path := sf .Name
143+ // For non embedded structs we need to add the parent field name to the path
144+ if ! field .Anonymous {
145+ path = field .Name + "." + path
146+ }
147+ fields = append (fields , structFieldWithPath {StructField : & sf , path : path })
137148 }
138149 return fields
139150}
140151
141- func ignoreField (t * TableDefinition , field reflect.StructField ) bool {
152+ func ignoreField (t * TableDefinition , field * reflect.StructField ) bool {
142153 return len (field .Name ) == 0 || unicode .IsLower (rune (field .Name [0 ])) || sliceContains (t .skipFields , field .Name )
143154}
144155
145- func addColumnFromField (t * TableDefinition , field reflect. StructField , comments map [string ]string ) {
146- if ignoreField (t , field ) {
156+ func addColumnFromField (t * TableDefinition , field structFieldWithPath , comments map [string ]string ) {
157+ if ignoreField (t , field . StructField ) {
147158 return
148159 }
149160
@@ -161,7 +172,7 @@ func addColumnFromField(t *TableDefinition, field reflect.StructField, comments
161172 }
162173
163174 // generate a PathResolver to use by default
164- pathResolver := fmt .Sprintf ("schema.PathResolver(%q)" , field .Name )
175+ pathResolver := fmt .Sprintf ("schema.PathResolver(%q)" , field .path )
165176 column := ColumnDefinition {
166177 Name : t .nameTransformer (field .Name ),
167178 Type : columnType ,
@@ -207,12 +218,12 @@ func NewTableFromStruct(name string, obj interface{}, opts ...TableOptions) (*Ta
207218 field := e .Type ().Field (i )
208219
209220 if shouldUnwrapField (& t , field ) {
210- embeddedFields := getEmbeddedFields (& t , field . Type )
221+ embeddedFields := getEmbeddedFields (& t , field )
211222 for _ , f := range embeddedFields {
212223 addColumnFromField (& t , f , make (map [string ]string ))
213224 }
214225 } else {
215- addColumnFromField (& t , field , comments )
226+ addColumnFromField (& t , structFieldWithPath { StructField : & field , path : field . Name } , comments )
216227 }
217228 }
218229
0 commit comments