@@ -19,6 +19,7 @@ type FileSet struct {
19
19
Package string // package name
20
20
Specs map [string ]ast.Expr // type specs in file
21
21
Identities map [string ]gen.Elem // processed from specs
22
+ Aliased map [string ]string // Aliased types.
22
23
Directives []string // raw preprocessor directives
23
24
Imports []* ast.ImportSpec // imports
24
25
CompactFloats bool // Use smaller floats when feasible
@@ -121,6 +122,22 @@ func (fs *FileSet) applyDirectives() {
121
122
}
122
123
}
123
124
}
125
+ // Apply aliases last, so we don't overrule any manually specified replace directives.
126
+ for _ , d := range fs .Aliased {
127
+ chunks := strings .Split (d , " " )
128
+ if len (chunks ) > 0 {
129
+ if fn , ok := directives [chunks [0 ]]; ok {
130
+ pushstate (chunks [0 ])
131
+ err := fn (chunks , fs )
132
+ if err != nil {
133
+ warnf ("directive error: %s" , err )
134
+ }
135
+ popstate ()
136
+ } else {
137
+ newdirs = append (newdirs , d )
138
+ }
139
+ }
140
+ }
124
141
fs .Directives = newdirs
125
142
}
126
143
@@ -318,6 +335,15 @@ func (fs *FileSet) getTypeSpecs(f *ast.File) {
318
335
for _ , s := range g .Specs {
319
336
// for ast.TypeSpecs....
320
337
if ts , ok := s .(* ast.TypeSpec ); ok {
338
+ // Handle type aliases, by adding a "replace" directive.
339
+ if ts .Assign != 0 {
340
+ if fs .Aliased == nil {
341
+ fs .Aliased = make (map [string ]string )
342
+ }
343
+ fs .Aliased [ts .Name .Name ] = fmt .Sprintf ("replace %s with:%s" , ts .Name .Name , stringify (ts .Type ))
344
+ continue
345
+ }
346
+
321
347
switch ts .Type .(type ) {
322
348
// this is the list of parse-able
323
349
// type specs
@@ -589,7 +615,7 @@ func (fs *FileSet) parseExpr(e ast.Expr) gen.Elem {
589
615
// can be done later, once we've resolved
590
616
// everything else.
591
617
if b .Value == gen .IDENT {
592
- if _ , ok := fs .Specs [e .Name ]; ! ok {
618
+ if _ , ok := fs .Specs [e .Name ]; ! ok && fs . Aliased [ e . Name ] == "" {
593
619
warnf ("possible non-local identifier: %s\n " , e .Name )
594
620
}
595
621
}
0 commit comments