1
1
package golang
2
2
3
3
import (
4
+ "fmt"
4
5
"sort"
5
6
"strings"
6
7
@@ -9,35 +10,51 @@ import (
9
10
)
10
11
11
12
type fileImports struct {
12
- Std []string
13
- Dep []string
13
+ Std []ImportSpec
14
+ Dep []ImportSpec
14
15
}
15
16
16
- func mergeImports (imps ... fileImports ) [][]string {
17
+ type ImportSpec struct {
18
+ ID string
19
+ Path string
20
+ }
21
+
22
+ func (s ImportSpec ) String () string {
23
+ if s .ID != "" {
24
+ return fmt .Sprintf ("%s \" %s\" " , s .ID , s .Path )
25
+ } else {
26
+ return fmt .Sprintf ("\" %s\" " , s .Path )
27
+ }
28
+ }
29
+
30
+ func mergeImports (imps ... fileImports ) [][]ImportSpec {
17
31
if len (imps ) == 1 {
18
- return [][]string {imps [0 ].Std , imps [0 ].Dep }
32
+ return [][]ImportSpec {
33
+ imps [0 ].Std ,
34
+ imps [0 ].Dep ,
35
+ }
19
36
}
20
37
21
- var stds , pkgs []string
38
+ var stds , pkgs []ImportSpec
22
39
seenStd := map [string ]struct {}{}
23
40
seenPkg := map [string ]struct {}{}
24
41
for i := range imps {
25
- for _ , std := range imps [i ].Std {
26
- if _ , ok := seenStd [std ]; ok {
42
+ for _ , spec := range imps [i ].Std {
43
+ if _ , ok := seenStd [spec . Path ]; ok {
27
44
continue
28
45
}
29
- stds = append (stds , std )
30
- seenStd [std ] = struct {}{}
46
+ stds = append (stds , spec )
47
+ seenStd [spec . Path ] = struct {}{}
31
48
}
32
- for _ , pkg := range imps [i ].Dep {
33
- if _ , ok := seenPkg [pkg ]; ok {
49
+ for _ , spec := range imps [i ].Dep {
50
+ if _ , ok := seenPkg [spec . Path ]; ok {
34
51
continue
35
52
}
36
- pkgs = append (pkgs , pkg )
37
- seenPkg [pkg ] = struct {}{}
53
+ pkgs = append (pkgs , spec )
54
+ seenPkg [spec . Path ] = struct {}{}
38
55
}
39
56
}
40
- return [][]string {stds , pkgs }
57
+ return [][]ImportSpec {stds , pkgs }
41
58
}
42
59
43
60
type importer struct {
@@ -70,7 +87,7 @@ func (i *importer) usesArrays() bool {
70
87
return false
71
88
}
72
89
73
- func (i * importer ) Imports (filename string ) [][]string {
90
+ func (i * importer ) Imports (filename string ) [][]ImportSpec {
74
91
switch filename {
75
92
case "db.go" :
76
93
return mergeImports (i .dbImports ())
@@ -84,9 +101,12 @@ func (i *importer) Imports(filename string) [][]string {
84
101
}
85
102
86
103
func (i * importer ) dbImports () fileImports {
87
- std := []string {"context" , "database/sql" }
104
+ std := []ImportSpec {
105
+ {Path : "context" },
106
+ {Path : "database/sql" },
107
+ }
88
108
if i .Settings .Go .EmitPreparedQueries {
89
- std = append (std , "fmt" )
109
+ std = append (std , ImportSpec { Path : "fmt" } )
90
110
}
91
111
return fileImports {Std : std }
92
112
}
@@ -132,43 +152,48 @@ func (i *importer) interfaceImports() fileImports {
132
152
std ["net" ] = struct {}{}
133
153
}
134
154
135
- pkg := make (map [string ]struct {})
155
+ pkg := make (map [ImportSpec ]struct {})
136
156
overrideTypes := map [string ]string {}
137
157
for _ , o := range i .Settings .Overrides {
138
158
if o .GoBasicType {
139
159
continue
140
160
}
141
- overrideTypes [o .GoTypeName ] = o .GoPackage
161
+ overrideTypes [o .GoTypeName ] = o .GoImportPath
142
162
}
143
163
144
164
_ , overrideNullTime := overrideTypes ["pq.NullTime" ]
145
165
if uses ("pq.NullTime" ) && ! overrideNullTime {
146
- pkg ["github.com/lib/pq" ] = struct {}{}
166
+ pkg [ImportSpec { Path : "github.com/lib/pq" } ] = struct {}{}
147
167
}
148
168
_ , overrideUUID := overrideTypes ["uuid.UUID" ]
149
169
if uses ("uuid.UUID" ) && ! overrideUUID {
150
- pkg ["github.com/google/uuid" ] = struct {}{}
170
+ pkg [ImportSpec { Path : "github.com/google/uuid" } ] = struct {}{}
151
171
}
152
172
153
173
// Custom imports
154
- for goType , importPath := range overrideTypes {
155
- if _ , ok := std [importPath ]; ! ok && uses (goType ) {
156
- pkg [importPath ] = struct {}{}
174
+ for _ , o := range i .Settings .Overrides {
175
+ if o .GoBasicType {
176
+ continue
177
+ }
178
+ _ , alreadyImported := std [o .GoImportPath ]
179
+ hasPackageAlias := o .GoPackage != ""
180
+ if (! alreadyImported || hasPackageAlias ) && uses (o .GoTypeName ) {
181
+ pkg [ImportSpec {Path : o .GoImportPath , ID : o .GoPackage }] = struct {}{}
157
182
}
158
183
}
159
184
160
- pkgs := make ([]string , 0 , len (pkg ))
161
- for p , _ := range pkg {
162
- pkgs = append (pkgs , p )
185
+ pkgs := make ([]ImportSpec , 0 , len (pkg ))
186
+ for spec , _ := range pkg {
187
+ pkgs = append (pkgs , spec )
163
188
}
164
189
165
- stds := make ([]string , 0 , len (std ))
166
- for s , _ := range std {
167
- stds = append (stds , s )
190
+ stds := make ([]ImportSpec , 0 , len (std ))
191
+ for path , _ := range std {
192
+ stds = append (stds , ImportSpec { Path : path } )
168
193
}
169
194
170
- sort .Strings (stds )
171
- sort .Strings (pkgs )
195
+ sort .Slice (stds , func ( i , j int ) bool { return stds [ i ]. Path < stds [ j ]. Path } )
196
+ sort .Slice (pkgs , func ( i , j int ) bool { return pkgs [ i ]. Path < pkgs [ j ]. Path } )
172
197
return fileImports {stds , pkgs }
173
198
}
174
199
@@ -194,43 +219,48 @@ func (i *importer) modelImports() fileImports {
194
219
}
195
220
196
221
// Custom imports
197
- pkg := make (map [string ]struct {})
222
+ pkg := make (map [ImportSpec ]struct {})
198
223
overrideTypes := map [string ]string {}
199
224
for _ , o := range i .Settings .Overrides {
200
225
if o .GoBasicType {
201
226
continue
202
227
}
203
- overrideTypes [o .GoTypeName ] = o .GoPackage
228
+ overrideTypes [o .GoTypeName ] = o .GoImportPath
204
229
}
205
230
206
231
_ , overrideNullTime := overrideTypes ["pq.NullTime" ]
207
232
if i .usesType ("pq.NullTime" ) && ! overrideNullTime {
208
- pkg ["github.com/lib/pq" ] = struct {}{}
233
+ pkg [ImportSpec { Path : "github.com/lib/pq" } ] = struct {}{}
209
234
}
210
235
211
236
_ , overrideUUID := overrideTypes ["uuid.UUID" ]
212
237
if i .usesType ("uuid.UUID" ) && ! overrideUUID {
213
- pkg ["github.com/google/uuid" ] = struct {}{}
238
+ pkg [ImportSpec { Path : "github.com/google/uuid" } ] = struct {}{}
214
239
}
215
240
216
- for goType , importPath := range overrideTypes {
217
- if _ , ok := std [importPath ]; ! ok && i .usesType (goType ) {
218
- pkg [importPath ] = struct {}{}
241
+ for _ , o := range i .Settings .Overrides {
242
+ if o .GoBasicType {
243
+ continue
244
+ }
245
+ _ , alreadyImported := std [o .GoImportPath ]
246
+ hasPackageAlias := o .GoPackage != ""
247
+ if (! alreadyImported || hasPackageAlias ) && i .usesType (o .GoTypeName ) {
248
+ pkg [ImportSpec {Path : o .GoImportPath , ID : o .GoPackage }] = struct {}{}
219
249
}
220
250
}
221
251
222
- pkgs := make ([]string , 0 , len (pkg ))
223
- for p , _ := range pkg {
224
- pkgs = append (pkgs , p )
252
+ pkgs := make ([]ImportSpec , 0 , len (pkg ))
253
+ for spec , _ := range pkg {
254
+ pkgs = append (pkgs , spec )
225
255
}
226
256
227
- stds := make ([]string , 0 , len (std ))
228
- for s , _ := range std {
229
- stds = append (stds , s )
257
+ stds := make ([]ImportSpec , 0 , len (std ))
258
+ for path , _ := range std {
259
+ stds = append (stds , ImportSpec { Path : path } )
230
260
}
231
261
232
- sort .Strings (stds )
233
- sort .Strings (pkgs )
262
+ sort .Slice (stds , func ( i , j int ) bool { return stds [ i ]. Path < stds [ j ]. Path } )
263
+ sort .Slice (pkgs , func ( i , j int ) bool { return pkgs [ i ]. Path < pkgs [ j ]. Path } )
234
264
return fileImports {stds , pkgs }
235
265
}
236
266
@@ -327,45 +357,50 @@ func (i *importer) queryImports(filename string) fileImports {
327
357
std ["net" ] = struct {}{}
328
358
}
329
359
330
- pkg := make (map [string ]struct {})
360
+ pkg := make (map [ImportSpec ]struct {})
331
361
overrideTypes := map [string ]string {}
332
362
for _ , o := range i .Settings .Overrides {
333
363
if o .GoBasicType {
334
364
continue
335
365
}
336
- overrideTypes [o .GoTypeName ] = o .GoPackage
366
+ overrideTypes [o .GoTypeName ] = o .GoImportPath
337
367
}
338
368
339
369
if sliceScan () {
340
- pkg ["github.com/lib/pq" ] = struct {}{}
370
+ pkg [ImportSpec { Path : "github.com/lib/pq" } ] = struct {}{}
341
371
}
342
372
_ , overrideNullTime := overrideTypes ["pq.NullTime" ]
343
373
if uses ("pq.NullTime" ) && ! overrideNullTime {
344
- pkg ["github.com/lib/pq" ] = struct {}{}
374
+ pkg [ImportSpec { Path : "github.com/lib/pq" } ] = struct {}{}
345
375
}
346
376
_ , overrideUUID := overrideTypes ["uuid.UUID" ]
347
377
if uses ("uuid.UUID" ) && ! overrideUUID {
348
- pkg ["github.com/google/uuid" ] = struct {}{}
378
+ pkg [ImportSpec { Path : "github.com/google/uuid" } ] = struct {}{}
349
379
}
350
380
351
381
// Custom imports
352
- for goType , importPath := range overrideTypes {
353
- if _ , ok := std [importPath ]; ! ok && uses (goType ) {
354
- pkg [importPath ] = struct {}{}
382
+ for _ , o := range i .Settings .Overrides {
383
+ if o .GoBasicType {
384
+ continue
385
+ }
386
+ _ , alreadyImported := std [o .GoImportPath ]
387
+ hasPackageAlias := o .GoPackage != ""
388
+ if (! alreadyImported || hasPackageAlias ) && uses (o .GoTypeName ) {
389
+ pkg [ImportSpec {Path : o .GoImportPath , ID : o .GoPackage }] = struct {}{}
355
390
}
356
391
}
357
392
358
- pkgs := make ([]string , 0 , len (pkg ))
359
- for p , _ := range pkg {
360
- pkgs = append (pkgs , p )
393
+ pkgs := make ([]ImportSpec , 0 , len (pkg ))
394
+ for spec , _ := range pkg {
395
+ pkgs = append (pkgs , spec )
361
396
}
362
397
363
- stds := make ([]string , 0 , len (std ))
364
- for s , _ := range std {
365
- stds = append (stds , s )
398
+ stds := make ([]ImportSpec , 0 , len (std ))
399
+ for path , _ := range std {
400
+ stds = append (stds , ImportSpec { Path : path } )
366
401
}
367
402
368
- sort .Strings (stds )
369
- sort .Strings (pkgs )
403
+ sort .Slice (stds , func ( i , j int ) bool { return stds [ i ]. Path < stds [ j ]. Path } )
404
+ sort .Slice (pkgs , func ( i , j int ) bool { return pkgs [ i ]. Path < pkgs [ j ]. Path } )
370
405
return fileImports {stds , pkgs }
371
406
}
0 commit comments