Skip to content

Commit

Permalink
Ensuring the order of children by ranging through a slice.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradylill committed Jun 21, 2018
1 parent 76bb4d6 commit 409951c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions filetree/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (n Node) marshalLeaf() (interface{}, error) {
}

buf, err := ioutil.ReadFile(n.FullPath)

if err != nil {
return content, err
}
Expand All @@ -130,7 +131,10 @@ func dotfolder(info os.FileInfo) bool {
func NewTree(root string, specialCase func(path string) bool) (*Node, error) {
SpecialCase = specialCase
parents := make(map[string]*Node)
var result *Node
var (
result *Node
pathKeys []string
)

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -148,6 +152,8 @@ func NewTree(root string, specialCase func(path string) bool) (*Node, error) {
if err != nil {
return err
}

pathKeys = append(pathKeys, path)
parents[path] = &Node{
FullPath: fp,
Info: info,
Expand All @@ -160,7 +166,8 @@ func NewTree(root string, specialCase func(path string) bool) (*Node, error) {
return nil, err
}

for path, node := range parents {
for _, path := range pathKeys {
node := parents[path]
parentPath := filepath.Dir(path)
parent, exists := parents[parentPath]
if exists {
Expand Down

0 comments on commit 409951c

Please sign in to comment.