Skip to content

Commit

Permalink
mars-gen: Sort files by name when processing packages to get stable o…
Browse files Browse the repository at this point in the history
…rder of registered controllers.
  • Loading branch information
roblillack committed Oct 27, 2016
1 parent ca906ea commit c2fd5ed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions cmd/mars-gen/filesorting.go
@@ -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
}
2 changes: 1 addition & 1 deletion cmd/mars-gen/reflect.go
Expand Up @@ -149,7 +149,7 @@ func processPackage(fset *token.FileSet, pkgImportPath, pkgPath string, pkg *ast
)

// For each source file in the package...
for _, file := range pkg.Files {
for _, file := range getSortedFiles(pkg) {

// Imports maps the package key to the full import path.
// e.g. import "sample/app/models" => "models": "sample/app/models"
Expand Down

0 comments on commit c2fd5ed

Please sign in to comment.