forked from roblillack/mars
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mars-gen: Sort files by name when processing packages to get stable o…
…rder of registered controllers.
- Loading branch information
1 parent
ca906ea
commit c2fd5ed
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"go/ast" | ||
"sort" | ||
) | ||
|
||
type fInfo struct { | ||
Filename string | ||
File *ast.File | ||
} | ||
|
||
type byName []fInfo | ||
|
||
func (s byName) Len() int { | ||
return len(s) | ||
} | ||
func (s byName) Swap(i, j int) { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
func (s byName) Less(i, j int) bool { | ||
return s[i].Filename < s[j].Filename | ||
} | ||
|
||
func getSortedFiles(pkg *ast.Package) []*ast.File { | ||
entries := make([]fInfo, 0, len(pkg.Files)) | ||
for fn, f := range pkg.Files { | ||
entries = append(entries, fInfo{Filename: fn, File: f}) | ||
} | ||
|
||
sort.Sort(byName(entries)) | ||
|
||
slice := make([]*ast.File, len(entries)) | ||
for idx, i := range entries { | ||
slice[idx] = i.File | ||
} | ||
|
||
return slice | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters