@@ -103,6 +103,46 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
103103type goColumn struct {
104104 id int
105105 * plugin.Column
106+ embed * goEmbed
107+ }
108+
109+ type goEmbed struct {
110+ modelType string
111+ modelName string
112+ fields []string
113+ }
114+
115+ // look through all the structs and attempt to find a matching one to embed
116+ // We need the name of the struct and its field names.
117+ func newGoEmbed (embed * plugin.Identifier , structs []Struct ) * goEmbed {
118+ if embed == nil {
119+ return nil
120+ }
121+
122+ for _ , s := range structs {
123+ embedSchema := "public"
124+ if embed .Schema != "" {
125+ embedSchema = embed .Schema
126+ }
127+
128+ // compare the other attributes
129+ if embed .Catalog != s .Table .Catalog || embed .Name != s .Table .Name || embedSchema != s .Table .Schema {
130+ continue
131+ }
132+
133+ fields := make ([]string , len (s .Fields ))
134+ for i , f := range s .Fields {
135+ fields [i ] = f .Name
136+ }
137+
138+ return & goEmbed {
139+ modelType : s .Name ,
140+ modelName : s .Name ,
141+ fields : fields ,
142+ }
143+ }
144+
145+ return nil
106146}
107147
108148func columnName (c * plugin.Column , pos int ) string {
@@ -192,7 +232,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
192232 }
193233 }
194234
195- if len (query .Columns ) == 1 {
235+ if len (query .Columns ) == 1 && query . Columns [ 0 ]. EmbedTable == nil {
196236 c := query .Columns [0 ]
197237 name := columnName (c , 0 )
198238 if c .IsFuncCall {
@@ -234,6 +274,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
234274 columns = append (columns , goColumn {
235275 id : i ,
236276 Column : c ,
277+ embed : newGoEmbed (c .EmbedTable , structs ),
237278 })
238279 }
239280 var err error
@@ -287,6 +328,13 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []goColumn
287328 for i , c := range columns {
288329 colName := columnName (c .Column , i )
289330 tagName := colName
331+
332+ // overide col/tag with expected model name
333+ if c .embed != nil {
334+ colName = c .embed .modelName
335+ tagName = SetCaseStyle (colName , "snake" )
336+ }
337+
290338 fieldName := StructName (colName , req .Settings )
291339 baseFieldName := fieldName
292340 // Track suffixes by the ID of the column, so that columns referring to the same numbered parameter can be
@@ -309,13 +357,20 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []goColumn
309357 if req .Settings .Go .EmitJsonTags {
310358 tags ["json" ] = JSONTagName (tagName , req .Settings )
311359 }
312- gs . Fields = append ( gs . Fields , Field {
360+ f := Field {
313361 Name : fieldName ,
314362 DBName : colName ,
315- Type : goType (req , c .Column ),
316363 Tags : tags ,
317364 Column : c .Column ,
318- })
365+ }
366+ if c .embed == nil {
367+ f .Type = goType (req , c .Column )
368+ } else {
369+ f .Type = c .embed .modelType
370+ f .EmbedFields = c .embed .fields
371+ }
372+
373+ gs .Fields = append (gs .Fields , f )
319374 if _ , found := seen [baseFieldName ]; ! found {
320375 seen [baseFieldName ] = []int {i }
321376 } else {
0 commit comments