@@ -46,6 +46,7 @@ type Field struct {
4646type templateData struct {
4747 HeaderComment string
4848 PackageName string
49+ Imports []string
4950 SourceName string
5051 OutputName string
5152 Fields []Field
@@ -100,6 +101,7 @@ func flattenOne(f *ast.File, cfg StructConfig, typeKinds map[string]string, scal
100101 data := templateData {
101102 HeaderComment : o .headerComment ,
102103 PackageName : o .packageName ,
104+ Imports : collectImports (fields ),
103105 SourceName : cfg .SourceName ,
104106 OutputName : cfg .OutputName ,
105107 Fields : fields ,
@@ -128,6 +130,38 @@ func flattenOne(f *ast.File, cfg StructConfig, typeKinds map[string]string, scal
128130 return nil
129131}
130132
133+ // collectImports extracts unique package import paths from field types that
134+ // contain a dot (e.g. "time.Time" -> "time"). Returns a sorted, deduplicated list.
135+ func collectImports (fields []Field ) []string {
136+ // Map well-known qualified type prefixes to their import paths.
137+ pkgToImport := map [string ]string {
138+ "time" : "time" ,
139+ "net" : "net" ,
140+ "url" : "net/url" ,
141+ "json" : "encoding/json" ,
142+ "uuid" : "github.com/google/uuid" ,
143+ }
144+
145+ seen := map [string ]bool {}
146+ for _ , f := range fields {
147+ typ := strings .TrimPrefix (f .Type , "*" )
148+ typ = strings .TrimPrefix (typ , "[]" )
149+ if dot := strings .IndexByte (typ , '.' ); dot > 0 {
150+ pkg := typ [:dot ]
151+ if imp , ok := pkgToImport [pkg ]; ok && ! seen [imp ] {
152+ seen [imp ] = true
153+ }
154+ }
155+ }
156+
157+ imports := make ([]string , 0 , len (seen ))
158+ for imp := range seen {
159+ imports = append (imports , imp )
160+ }
161+ slices .Sort (imports )
162+ return imports
163+ }
164+
131165// sortFields sorts fields with "ID" first, then alphabetically by name (case-insensitive).
132166func sortFields (fields []Field ) {
133167 slices .SortStableFunc (fields , func (a , b Field ) int {
0 commit comments