Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
Switch order in box.find to lookup for directories (#87)
Browse files Browse the repository at this point in the history
* Fix order for checking directories in box.find

* Add godoc comments

* Add test cases & subfolder-file
  • Loading branch information
bastiankoetsier authored and markbates committed Aug 23, 2018
1 parent 3932e03 commit ee1318b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

var (
// ErrResOutsideBox gets returned in case of the requested resources being outside the box
ErrResOutsideBox = errors.New("Can't find a resource outside the box")
)

Expand Down Expand Up @@ -51,10 +52,12 @@ type Box struct {
directories map[string]bool
}

// AddString converts t to a byteslice and delegates to AddBytes to add to b.data
func (b Box) AddString(path string, t string) {
b.AddBytes(path, []byte(t))
}

// AddBytes sets t in b.data by the given path
func (b Box) AddBytes(path string, t []byte) {
b.data[path] = t
}
Expand Down Expand Up @@ -132,14 +135,14 @@ func (b Box) find(name string) (File, error) {
bb = b.decompress(bb)
return newVirtualFile(cleanName, bb), nil
}
if _, ok := b.directories[cleanName]; ok {
return newVirtualDir(cleanName), nil
}
if filepath.Ext(cleanName) != "" {
// The Handler created by http.FileSystem checks for those errors and
// returns http.StatusNotFound instead of http.StatusInternalServerError.
return nil, os.ErrNotExist
}
if _, ok := b.directories[cleanName]; ok {
return newVirtualDir(cleanName), nil
}
return nil, os.ErrNotExist
}

Expand Down
4 changes: 4 additions & 0 deletions box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func Test_Box_find(t *testing.T) {
{"assets/app.css", true},
{"assets\\app.css", onWindows},
{"foo/bar.baz", false},
{"bar", true},
{"bar/sub", true},
{"bar/foo", false},
{"bar/sub/sub.html", true},
}

for _, tt := range table {
Expand Down
Empty file added example/bar/sub/sub.html
Empty file.

0 comments on commit ee1318b

Please sign in to comment.